File size: 24,655 Bytes
476455e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from __future__ import absolute_import

import pytest
import re
import time


from sagemaker.debugger.profiler_config import ProfilerConfig, FrameworkProfile

from sagemaker.debugger.metrics_config import (
    StepRange,
    TimeRange,
    DetailedProfilingConfig,
    DataloaderProfilingConfig,
    PythonProfilingConfig,
    HorovodProfilingConfig,
    SMDataParallelProfilingConfig,
)
from sagemaker.debugger.profiler_constants import (
    BASE_FOLDER_DEFAULT,
    CLOSE_FILE_INTERVAL_DEFAULT,
    FILE_OPEN_FAIL_THRESHOLD_DEFAULT,
    MAX_FILE_SIZE_DEFAULT,
    DATALOADER_PROFILING_CONFIG_NAME,
    DATALOADER_PROFILING_START_STEP_DEFAULT,
    DETAILED_PROFILING_CONFIG_NAME,
    DETAILED_PROFILING_START_STEP_DEFAULT,
    SMDATAPARALLEL_PROFILING_CONFIG_NAME,
    SMDATAPARALLEL_PROFILING_START_STEP_DEFAULT,
    HOROVOD_PROFILING_CONFIG_NAME,
    HOROVOD_PROFILING_START_STEP_DEFAULT,
    PROFILING_NUM_STEPS_DEFAULT,
    PYTHON_PROFILING_CONFIG_NAME,
    PYTHON_PROFILING_NUM_STEPS_DEFAULT,
    PYTHON_PROFILING_START_STEP_DEFAULT,
    START_STEP_DEFAULT,
)
from sagemaker.debugger.utils import PythonProfiler, cProfileTimer, ErrorMessages


@pytest.fixture
def custom_local_path():
    return "/tmp/test"


@pytest.fixture
def custom_file_max_size():
    return MAX_FILE_SIZE_DEFAULT * 2


@pytest.fixture
def custom_file_close_interval():
    return CLOSE_FILE_INTERVAL_DEFAULT / 2


@pytest.fixture
def custom_file_open_fail_threshold():
    return FILE_OPEN_FAIL_THRESHOLD_DEFAULT + 10


@pytest.fixture
def custom_start_step():
    return 3


@pytest.fixture
def custom_num_steps():
    return 5


@pytest.fixture
def custom_start_unix_time():
    return int(time.time())


@pytest.fixture
def custom_duration():
    return 30


@pytest.fixture
def custom_metrics_regex():
    return "Dataset::Iterator::GetNext"


@pytest.fixture
def custom_python_profiler():
    return PythonProfiler.PYINSTRUMENT


@pytest.fixture
def custom_cprofile_timer():
    return cProfileTimer.CPU_TIME


@pytest.fixture
def default_framework_profile():
    return FrameworkProfile()


@pytest.fixture
def default_profiler_config():
    return ProfilerConfig()


@pytest.fixture
def framework_profile_with_custom_trace_file_fields(
    custom_local_path,
    custom_file_max_size,
    custom_file_close_interval,
    custom_file_open_fail_threshold,
):
    return FrameworkProfile(
        local_path=custom_local_path,
        file_max_size=custom_file_max_size,
        file_close_interval=custom_file_close_interval,
        file_open_fail_threshold=custom_file_open_fail_threshold,
    )


@pytest.fixture
def custom_detailed_profiling_config(custom_start_step, custom_num_steps):
    return DetailedProfilingConfig(start_step=custom_start_step, num_steps=custom_num_steps)


@pytest.fixture
def custom_dataloader_profiling_config(custom_start_step, custom_metrics_regex):
    return DataloaderProfilingConfig(
        start_step=custom_start_step, metrics_regex=custom_metrics_regex
    )


@pytest.fixture
def custom_python_profiling_config(custom_num_steps, custom_python_profiler):
    return PythonProfilingConfig(num_steps=custom_num_steps, python_profiler=custom_python_profiler)


@pytest.fixture
def custom_python_profiling_config_2(
    custom_start_unix_time, custom_duration, custom_cprofile_timer
):
    return PythonProfilingConfig(
        start_unix_time=custom_start_unix_time,
        duration=custom_duration,
        cprofile_timer=custom_cprofile_timer,
    )


@pytest.fixture
def custom_horovod_profiling_config(custom_start_unix_time):
    return HorovodProfilingConfig(start_unix_time=custom_start_unix_time)


@pytest.fixture
def custom_smdataparallel_profiling_config(custom_duration):
    return SMDataParallelProfilingConfig(duration=custom_duration)


@pytest.fixture
def framework_profile_with_custom_metrics_configs(
    custom_detailed_profiling_config,
    custom_dataloader_profiling_config,
    custom_python_profiling_config,
    custom_horovod_profiling_config,
    custom_smdataparallel_profiling_config,
):
    return FrameworkProfile(
        detailed_profiling_config=custom_detailed_profiling_config,
        dataloader_profiling_config=custom_dataloader_profiling_config,
        python_profiling_config=custom_python_profiling_config,
        horovod_profiling_config=custom_horovod_profiling_config,
        smdataparallel_profiling_config=custom_smdataparallel_profiling_config,
    )


@pytest.fixture
def framework_profile_with_only_custom_python_profiling_config(custom_python_profiling_config_2):
    return FrameworkProfile(python_profiling_config=custom_python_profiling_config_2)


@pytest.fixture
def framework_profile_with_custom_step_range(custom_start_step, custom_num_steps):
    return FrameworkProfile(start_step=custom_start_step, num_steps=custom_num_steps)


@pytest.fixture
def framework_profile_with_custom_time_range(custom_start_unix_time, custom_duration):
    return FrameworkProfile(start_unix_time=custom_start_unix_time, duration=custom_duration)


def _validate_profiling_parameter_conditions(profiling_parameters):
    regex = re.compile(".*")

    for key, val in profiling_parameters.items():
        assert isinstance(key, str)
        assert len(key) <= 256
        assert regex.match(key) is not None
        assert isinstance(val, str)
        assert len(val) <= 256
        assert regex.match(val) is not None


def test_default_profiler_config():
    profiler_config = ProfilerConfig()
    request_dict = profiler_config._to_request_dict()
    assert request_dict.get("S3OutputPath") is None
    assert "ProfilingParameters" not in request_dict


def test_profiler_config_with_default_framework_profile(default_framework_profile):
    profiler_config = ProfilerConfig(framework_profile_params=default_framework_profile)
    request_dict = profiler_config._to_request_dict()
    profiling_parameters = request_dict["ProfilingParameters"]

    _validate_profiling_parameter_conditions(profiling_parameters)
    assert profiling_parameters["LocalPath"] == BASE_FOLDER_DEFAULT
    assert int(profiling_parameters["RotateMaxFileSizeInBytes"]) == MAX_FILE_SIZE_DEFAULT
    assert (
        float(profiling_parameters["RotateFileCloseIntervalInSeconds"])
        == CLOSE_FILE_INTERVAL_DEFAULT
    )
    assert int(profiling_parameters["FileOpenFailThreshold"]) == FILE_OPEN_FAIL_THRESHOLD_DEFAULT

    detailed_profiling_config = eval(profiling_parameters[DETAILED_PROFILING_CONFIG_NAME])
    assert detailed_profiling_config == {
        "StartStep": DETAILED_PROFILING_START_STEP_DEFAULT,
        "NumSteps": PROFILING_NUM_STEPS_DEFAULT,
    }

    dataloader_profiling_config = eval(profiling_parameters[DATALOADER_PROFILING_CONFIG_NAME])
    assert dataloader_profiling_config == {
        "StartStep": DATALOADER_PROFILING_START_STEP_DEFAULT,
        "NumSteps": PROFILING_NUM_STEPS_DEFAULT,
        "MetricsRegex": ".*",
    }

    python_profiling_config = eval(profiling_parameters[PYTHON_PROFILING_CONFIG_NAME])
    assert python_profiling_config == {
        "StartStep": PYTHON_PROFILING_START_STEP_DEFAULT,
        "NumSteps": PYTHON_PROFILING_NUM_STEPS_DEFAULT,
        "ProfilerName": PythonProfiler.CPROFILE.value,
        "cProfileTimer": cProfileTimer.DEFAULT.value,
    }

    horovod_profiling_config = eval(profiling_parameters[HOROVOD_PROFILING_CONFIG_NAME])
    assert horovod_profiling_config == {
        "StartStep": HOROVOD_PROFILING_START_STEP_DEFAULT,
        "NumSteps": PROFILING_NUM_STEPS_DEFAULT,
    }

    smdataparallel_profiling_config = eval(
        profiling_parameters[SMDATAPARALLEL_PROFILING_CONFIG_NAME]
    )
    assert smdataparallel_profiling_config == {
        "StartStep": SMDATAPARALLEL_PROFILING_START_STEP_DEFAULT,
        "NumSteps": PROFILING_NUM_STEPS_DEFAULT,
    }


def test_default_detailed_profiling_config():
    detailed_profiling_config = DetailedProfilingConfig(profile_default_steps=True)
    assert isinstance(detailed_profiling_config.range, StepRange)
    assert detailed_profiling_config.range.start_step == DETAILED_PROFILING_START_STEP_DEFAULT
    assert detailed_profiling_config.range.num_steps == PROFILING_NUM_STEPS_DEFAULT


def test_default_dataloader_metrics_config():
    dataloader_metrics_config = DataloaderProfilingConfig(profile_default_steps=True)
    assert isinstance(dataloader_metrics_config.range, StepRange)
    assert dataloader_metrics_config.range.start_step == DATALOADER_PROFILING_START_STEP_DEFAULT
    assert dataloader_metrics_config.range.num_steps == PROFILING_NUM_STEPS_DEFAULT
    assert dataloader_metrics_config.metrics_regex == ".*"


def test_default_python_profiling_config():
    python_profiling_config = PythonProfilingConfig(profile_default_steps=True)
    assert isinstance(python_profiling_config.range, StepRange)
    assert python_profiling_config.range.start_step == PYTHON_PROFILING_START_STEP_DEFAULT
    assert python_profiling_config.range.num_steps == PYTHON_PROFILING_NUM_STEPS_DEFAULT
    assert python_profiling_config.python_profiler == PythonProfiler.CPROFILE
    assert python_profiling_config.cprofile_timer == cProfileTimer.DEFAULT


def test_default_horovod_profiling_config():
    horovod_profiling_config = HorovodProfilingConfig(profile_default_steps=True)
    assert isinstance(horovod_profiling_config.range, StepRange)
    assert horovod_profiling_config.range.start_step == HOROVOD_PROFILING_START_STEP_DEFAULT
    assert horovod_profiling_config.range.num_steps == PROFILING_NUM_STEPS_DEFAULT


def test_default_smdataparallel_profiling_config():
    smdataparallel_profiling_config = SMDataParallelProfilingConfig(profile_default_steps=True)
    assert isinstance(smdataparallel_profiling_config.range, StepRange)
    assert (
        smdataparallel_profiling_config.range.start_step
        == SMDATAPARALLEL_PROFILING_START_STEP_DEFAULT
    )
    assert smdataparallel_profiling_config.range.num_steps == PROFILING_NUM_STEPS_DEFAULT


def test_profiler_config_with_custom_trace_file_fields(
    framework_profile_with_custom_trace_file_fields,
    custom_local_path,
    custom_file_max_size,
    custom_file_close_interval,
    custom_file_open_fail_threshold,
):
    profiler_config = ProfilerConfig(
        framework_profile_params=framework_profile_with_custom_trace_file_fields
    )
    request_dict = profiler_config._to_request_dict()
    profiling_parameters = request_dict["ProfilingParameters"]

    _validate_profiling_parameter_conditions(profiling_parameters)
    assert profiling_parameters["LocalPath"] == custom_local_path
    assert int(profiling_parameters["RotateMaxFileSizeInBytes"]) == custom_file_max_size
    assert (
        float(profiling_parameters["RotateFileCloseIntervalInSeconds"])
        == custom_file_close_interval
    )
    assert int(profiling_parameters["FileOpenFailThreshold"]) == custom_file_open_fail_threshold

    detailed_profiling_config = eval(profiling_parameters[DETAILED_PROFILING_CONFIG_NAME])
    assert detailed_profiling_config == {
        "StartStep": DETAILED_PROFILING_START_STEP_DEFAULT,
        "NumSteps": PROFILING_NUM_STEPS_DEFAULT,
    }

    dataloader_profiling_config = eval(profiling_parameters[DATALOADER_PROFILING_CONFIG_NAME])
    assert dataloader_profiling_config == {
        "StartStep": DATALOADER_PROFILING_START_STEP_DEFAULT,
        "NumSteps": PROFILING_NUM_STEPS_DEFAULT,
        "MetricsRegex": ".*",
    }

    python_profiling_config = eval(profiling_parameters[PYTHON_PROFILING_CONFIG_NAME])
    assert python_profiling_config == {
        "StartStep": PYTHON_PROFILING_START_STEP_DEFAULT,
        "NumSteps": PYTHON_PROFILING_NUM_STEPS_DEFAULT,
        "ProfilerName": PythonProfiler.CPROFILE.value,
        "cProfileTimer": cProfileTimer.DEFAULT.value,
    }

    horovod_profiling_config = eval(profiling_parameters[HOROVOD_PROFILING_CONFIG_NAME])
    assert horovod_profiling_config == {
        "StartStep": HOROVOD_PROFILING_START_STEP_DEFAULT,
        "NumSteps": PROFILING_NUM_STEPS_DEFAULT,
    }

    smdataparallel_profiling_config = eval(
        profiling_parameters[SMDATAPARALLEL_PROFILING_CONFIG_NAME]
    )
    assert smdataparallel_profiling_config == {
        "StartStep": SMDATAPARALLEL_PROFILING_START_STEP_DEFAULT,
        "NumSteps": PROFILING_NUM_STEPS_DEFAULT,
    }


def test_profiler_config_with_custom_metrics_configs(
    framework_profile_with_custom_metrics_configs,
    framework_profile_with_only_custom_python_profiling_config,
    custom_start_step,
    custom_num_steps,
    custom_start_unix_time,
    custom_duration,
    custom_metrics_regex,
    custom_python_profiler,
    custom_cprofile_timer,
):
    profiler_config = ProfilerConfig(
        framework_profile_params=framework_profile_with_custom_metrics_configs
    )
    request_dict = profiler_config._to_request_dict()
    profiling_parameters = request_dict["ProfilingParameters"]

    _validate_profiling_parameter_conditions(profiling_parameters)
    assert profiling_parameters["LocalPath"] == BASE_FOLDER_DEFAULT
    assert int(profiling_parameters["RotateMaxFileSizeInBytes"]) == MAX_FILE_SIZE_DEFAULT
    assert (
        float(profiling_parameters["RotateFileCloseIntervalInSeconds"])
        == CLOSE_FILE_INTERVAL_DEFAULT
    )
    assert int(profiling_parameters["FileOpenFailThreshold"]) == FILE_OPEN_FAIL_THRESHOLD_DEFAULT

    detailed_profiling_config = eval(profiling_parameters[DETAILED_PROFILING_CONFIG_NAME])
    assert detailed_profiling_config == {
        "StartStep": custom_start_step,
        "NumSteps": custom_num_steps,
    }

    dataloader_profiling_config = eval(profiling_parameters[DATALOADER_PROFILING_CONFIG_NAME])
    assert dataloader_profiling_config == {
        "StartStep": custom_start_step,
        "NumSteps": PROFILING_NUM_STEPS_DEFAULT,
        "MetricsRegex": custom_metrics_regex,
    }

    python_profiling_config = eval(profiling_parameters[PYTHON_PROFILING_CONFIG_NAME])
    assert python_profiling_config == {
        "StartStep": START_STEP_DEFAULT,
        "NumSteps": custom_num_steps,
        "ProfilerName": custom_python_profiler.value,
    }

    horovod_profiling_config = eval(profiling_parameters[HOROVOD_PROFILING_CONFIG_NAME])
    assert horovod_profiling_config == {"StartTimeInSecSinceEpoch": custom_start_unix_time}

    smdataparallel_profiling_config = eval(
        profiling_parameters[SMDATAPARALLEL_PROFILING_CONFIG_NAME]
    )
    assert smdataparallel_profiling_config == {"Duration": custom_duration}

    profiler_config = ProfilerConfig(
        framework_profile_params=framework_profile_with_only_custom_python_profiling_config
    )
    request_dict = profiler_config._to_request_dict()
    profiling_parameters = request_dict["ProfilingParameters"]

    python_profiling_config = eval(profiling_parameters[PYTHON_PROFILING_CONFIG_NAME])
    assert python_profiling_config == {
        "StartTimeInSecSinceEpoch": custom_start_unix_time,
        "Duration": custom_duration,
        "ProfilerName": PythonProfiler.CPROFILE.value,
        "cProfileTimer": custom_cprofile_timer.value,
    }

    for config_name in [
        DETAILED_PROFILING_CONFIG_NAME,
        DATALOADER_PROFILING_CONFIG_NAME,
        HOROVOD_PROFILING_CONFIG_NAME,
        SMDATAPARALLEL_PROFILING_CONFIG_NAME,
    ]:
        assert config_name not in profiling_parameters


def test_custom_detailed_profiling_config(
    custom_detailed_profiling_config, custom_start_step, custom_num_steps
):
    assert isinstance(custom_detailed_profiling_config.range, StepRange)
    assert custom_detailed_profiling_config.range.start_step == custom_start_step
    assert custom_detailed_profiling_config.range.num_steps == custom_num_steps


def test_custom_dataloader_profiling_config(
    custom_dataloader_profiling_config, custom_start_step, custom_metrics_regex
):
    assert isinstance(custom_dataloader_profiling_config.range, StepRange)
    assert custom_dataloader_profiling_config.range.start_step == custom_start_step
    assert custom_dataloader_profiling_config.range.num_steps == PROFILING_NUM_STEPS_DEFAULT
    assert custom_dataloader_profiling_config.metrics_regex == custom_metrics_regex


def test_custom_python_profiling_config(
    custom_python_profiling_config, custom_num_steps, custom_python_profiler
):
    assert isinstance(custom_python_profiling_config.range, StepRange)
    assert custom_python_profiling_config.range.start_step == START_STEP_DEFAULT
    assert custom_python_profiling_config.range.num_steps == custom_num_steps
    assert custom_python_profiling_config.python_profiler == custom_python_profiler
    assert custom_python_profiling_config.cprofile_timer is None


def test_custom_python_profiling_config_2(
    custom_python_profiling_config_2, custom_start_unix_time, custom_duration, custom_cprofile_timer
):
    assert isinstance(custom_python_profiling_config_2.range, TimeRange)
    assert custom_python_profiling_config_2.range.start_unix_time == custom_start_unix_time
    assert custom_python_profiling_config_2.range.duration == custom_duration
    assert custom_python_profiling_config_2.python_profiler == PythonProfiler.CPROFILE
    assert custom_python_profiling_config_2.cprofile_timer == custom_cprofile_timer


def test_custom_horovod_profiling_config(custom_horovod_profiling_config, custom_start_unix_time):
    assert isinstance(custom_horovod_profiling_config.range, TimeRange)
    assert custom_horovod_profiling_config.range.start_unix_time == custom_start_unix_time
    assert custom_horovod_profiling_config.range.duration is None


def test_custom_smdataparallel_profiling_config(
    custom_smdataparallel_profiling_config, custom_duration
):
    assert isinstance(custom_smdataparallel_profiling_config.range, TimeRange)
    assert custom_smdataparallel_profiling_config.range.start_unix_time is None
    assert custom_smdataparallel_profiling_config.range.duration == custom_duration


def test_profiler_config_with_custom_step_range(custom_start_step, custom_num_steps):
    profiler_config = ProfilerConfig(
        framework_profile_params=FrameworkProfile(
            start_step=custom_start_step, num_steps=custom_num_steps
        )
    )
    request_dict = profiler_config._to_request_dict()
    profiling_parameters = request_dict["ProfilingParameters"]

    _validate_profiling_parameter_conditions(profiling_parameters)
    assert profiling_parameters["LocalPath"] == BASE_FOLDER_DEFAULT
    assert int(profiling_parameters["RotateMaxFileSizeInBytes"]) == MAX_FILE_SIZE_DEFAULT
    assert (
        float(profiling_parameters["RotateFileCloseIntervalInSeconds"])
        == CLOSE_FILE_INTERVAL_DEFAULT
    )
    assert int(profiling_parameters["FileOpenFailThreshold"]) == FILE_OPEN_FAIL_THRESHOLD_DEFAULT

    detailed_profiling_config = eval(profiling_parameters[DETAILED_PROFILING_CONFIG_NAME])
    assert detailed_profiling_config == {
        "StartStep": custom_start_step,
        "NumSteps": custom_num_steps,
    }

    dataloader_profiling_config = eval(profiling_parameters[DATALOADER_PROFILING_CONFIG_NAME])
    assert dataloader_profiling_config == {
        "StartStep": custom_start_step,
        "NumSteps": custom_num_steps,
        "MetricsRegex": ".*",
    }

    python_profiling_config = eval(profiling_parameters[PYTHON_PROFILING_CONFIG_NAME])
    assert python_profiling_config == {
        "StartStep": custom_start_step,
        "NumSteps": custom_num_steps,
        "ProfilerName": PythonProfiler.CPROFILE.value,
        "cProfileTimer": cProfileTimer.TOTAL_TIME.value,
    }

    horovod_profiling_config = eval(profiling_parameters[HOROVOD_PROFILING_CONFIG_NAME])
    assert horovod_profiling_config == {
        "StartStep": custom_start_step,
        "NumSteps": custom_num_steps,
    }

    smdataparallel_profiling_config = eval(
        profiling_parameters[SMDATAPARALLEL_PROFILING_CONFIG_NAME]
    )
    assert smdataparallel_profiling_config == {
        "StartStep": custom_start_step,
        "NumSteps": custom_num_steps,
    }


def test_profiler_config_with_custom_time_range(custom_start_unix_time, custom_duration):
    profiler_config = ProfilerConfig(
        framework_profile_params=FrameworkProfile(
            start_unix_time=custom_start_unix_time, duration=custom_duration
        )
    )
    request_dict = profiler_config._to_request_dict()
    profiling_parameters = request_dict["ProfilingParameters"]

    _validate_profiling_parameter_conditions(profiling_parameters)
    assert profiling_parameters["LocalPath"] == BASE_FOLDER_DEFAULT
    assert int(profiling_parameters["RotateMaxFileSizeInBytes"]) == MAX_FILE_SIZE_DEFAULT
    assert (
        float(profiling_parameters["RotateFileCloseIntervalInSeconds"])
        == CLOSE_FILE_INTERVAL_DEFAULT
    )
    assert int(profiling_parameters["FileOpenFailThreshold"]) == FILE_OPEN_FAIL_THRESHOLD_DEFAULT

    detailed_profiling_config = eval(profiling_parameters[DETAILED_PROFILING_CONFIG_NAME])
    assert detailed_profiling_config == {
        "StartTimeInSecSinceEpoch": custom_start_unix_time,
        "Duration": custom_duration,
    }

    dataloader_profiling_config = eval(profiling_parameters[DATALOADER_PROFILING_CONFIG_NAME])
    assert dataloader_profiling_config == {
        "StartTimeInSecSinceEpoch": custom_start_unix_time,
        "Duration": custom_duration,
        "MetricsRegex": ".*",
    }

    python_profiling_config = eval(profiling_parameters[PYTHON_PROFILING_CONFIG_NAME])
    assert python_profiling_config == {
        "StartTimeInSecSinceEpoch": custom_start_unix_time,
        "Duration": custom_duration,
        "ProfilerName": PythonProfiler.CPROFILE.value,
        "cProfileTimer": cProfileTimer.TOTAL_TIME.value,
    }

    horovod_profiling_config = eval(profiling_parameters[HOROVOD_PROFILING_CONFIG_NAME])
    assert horovod_profiling_config == {
        "StartTimeInSecSinceEpoch": custom_start_unix_time,
        "Duration": custom_duration,
    }

    smdataparallel_profiling_config = eval(
        profiling_parameters[SMDATAPARALLEL_PROFILING_CONFIG_NAME]
    )
    assert smdataparallel_profiling_config == {
        "StartTimeInSecSinceEpoch": custom_start_unix_time,
        "Duration": custom_duration,
    }


def test_validation():
    with pytest.raises(AssertionError, match=ErrorMessages.INVALID_LOCAL_PATH.value):
        FrameworkProfile(local_path=10)

    with pytest.raises(AssertionError, match=ErrorMessages.INVALID_FILE_MAX_SIZE.value):
        FrameworkProfile(file_max_size=100.5)

    with pytest.raises(AssertionError, match=ErrorMessages.INVALID_FILE_CLOSE_INTERVAL.value):
        FrameworkProfile(file_close_interval=-1)

    with pytest.raises(AssertionError, match=ErrorMessages.INVALID_FILE_OPEN_FAIL_THRESHOLD.value):
        FrameworkProfile(file_open_fail_threshold=2.3)

    with pytest.raises(AssertionError, match=ErrorMessages.INVALID_START_STEP.value):
        FrameworkProfile(start_step=6.8)

    with pytest.raises(AssertionError, match=ErrorMessages.INVALID_NUM_STEPS.value):
        FrameworkProfile(num_steps=0)

    with pytest.raises(AssertionError, match=ErrorMessages.INVALID_START_UNIX_TIME.value):
        FrameworkProfile(start_unix_time=99999999999999)

    with pytest.raises(AssertionError, match=ErrorMessages.INVALID_DURATION.value):
        FrameworkProfile(duration=0)

    with pytest.raises(AssertionError, match=ErrorMessages.FOUND_BOTH_STEP_AND_TIME_FIELDS.value):
        FrameworkProfile(start_step=5, duration=20)

    with pytest.raises(AssertionError, match=ErrorMessages.INVALID_METRICS_REGEX.value):
        DataloaderProfilingConfig(metrics_regex="*")

    with pytest.raises(AssertionError, match=ErrorMessages.INVALID_METRICS_REGEX.value):
        DataloaderProfilingConfig(metrics_regex=3)

    with pytest.raises(AssertionError, match=ErrorMessages.INVALID_PYTHON_PROFILER.value):
        PythonProfilingConfig(python_profiler="bad_python_profiler", profile_default_steps=True)

    with pytest.raises(AssertionError, match=ErrorMessages.INVALID_CPROFILE_TIMER.value):
        PythonProfilingConfig(cprofile_timer="bad_cprofile_timer")