Skip to content

Commit 8a649af

Browse files
committed
Updated tests
tests/test_autoprofile.py test_autoprofile_exec_package(), test_autoprofile_exec_module() Updated to reflect the new behavior of `kernprof` and to test the `--no-preimports` flag test_autoprofile_eager_preimports() Removed because the `--eager-preimports` flag is removed test_autoprofile_callable_wrapper_objects() Updated because the `--eager-preimports` flag is removed
1 parent 97ec0c0 commit 8a649af

File tree

1 file changed

+46
-84
lines changed

1 file changed

+46
-84
lines changed

tests/test_autoprofile.py

Lines changed: 46 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -432,19 +432,28 @@ def test_autoprofile_script_with_prof_imports():
432432

433433

434434
@pytest.mark.parametrize(
435-
['use_kernprof_exec', 'prof_mod', 'prof_imports',
435+
['use_kernprof_exec', 'prof_mod', 'no_preimports', 'prof_imports',
436436
'add_one', 'add_two', 'add_operator', 'main'],
437-
[(False, 'test_mod.submod1', False, True, False, False, False),
438-
(False, 'test_mod.submod2', True, False, True, True, False),
439-
(False, 'test_mod', True, True, True, True, True),
437+
[(False, 'test_mod.submod1', False, False,
438+
True, False, True, False),
439+
# By not using `--no-preimports`, the entirety of `.submod1` is
440+
# passed to `add_imported_function_or_module()`
441+
(False, 'test_mod.submod1', True, False,
442+
True, False, False, False),
443+
(False, 'test_mod.submod2', False, True,
444+
False, True, True, False),
445+
(False, 'test_mod', False, True,
446+
True, True, True, True),
440447
# Explicitly add all the modules via multiple `-p` flags, without
441448
# using the `--prof-imports` flag
442-
(False, ['test_mod', 'test_mod.submod1,test_mod.submod2'], False,
449+
(False, ['test_mod', 'test_mod.submod1,test_mod.submod2'], False, False,
443450
True, True, True, True),
444-
(False, None, True, False, False, False, False),
445-
(True, None, True, False, False, False, False)])
451+
(False, None, False, True,
452+
False, False, False, False),
453+
(True, None, False, True,
454+
False, False, False, False)])
446455
def test_autoprofile_exec_package(
447-
use_kernprof_exec, prof_mod, prof_imports,
456+
use_kernprof_exec, prof_mod, no_preimports, prof_imports,
448457
add_one, add_two, add_operator, main):
449458
"""
450459
Test the execution of a package.
@@ -461,6 +470,8 @@ def test_autoprofile_exec_package(
461470
prof_mod = [prof_mod]
462471
for pm in prof_mod:
463472
args.extend(['-p', pm])
473+
if no_preimports:
474+
args.append('--no-preimports')
464475
if prof_imports:
465476
args.append('--prof-imports')
466477
args.extend(['-l', '-m', 'test_mod', '1', '2', '3'])
@@ -484,16 +495,28 @@ def test_autoprofile_exec_package(
484495

485496

486497
@pytest.mark.parametrize(
487-
['use_kernprof_exec', 'prof_mod', 'prof_imports',
488-
'add_one', 'add_two', 'add_four', 'add_operator', 'main'],
489-
[(False, 'test_mod.submod2', False, False, True, False, False, False),
490-
(False, 'test_mod.submod1', False, True, False, False, True, False),
491-
(False, 'test_mod.subpkg.submod4', True, True, True, True, True, True),
492-
(False, None, True, False, False, False, False, False),
493-
(True, None, True, False, False, False, False, False)])
498+
['use_kernprof_exec', 'prof_mod', 'no_preimports', 'prof_imports',
499+
'add_one', 'add_two', 'add_three', 'add_four', 'add_operator', 'main'],
500+
[(False, 'test_mod.submod2,test_mod.subpkg.submod3.add_three', True, False,
501+
False, True, False, False, False, False),
502+
# By not using `--no-preimports`:
503+
# - The entirety of `.submod2` is passed to
504+
# `add_imported_function_or_module()`
505+
# - Despite not having been imported anywhere, `add_three()` is
506+
# still profiled
507+
(False, 'test_mod.submod2,test_mod.subpkg.submod3.add_three', False, False,
508+
False, True, True, False, True, False),
509+
(False, 'test_mod.submod1', False, False,
510+
True, False, False, False, True, False),
511+
(False, 'test_mod.subpkg.submod4', False, True,
512+
True, True, False, True, True, True),
513+
(False, None, False, True,
514+
False, False, False, False, False, False),
515+
(True, None, False, True,
516+
False, False, False, False, False, False)])
494517
def test_autoprofile_exec_module(
495-
use_kernprof_exec, prof_mod, prof_imports,
496-
add_one, add_two, add_four, add_operator, main):
518+
use_kernprof_exec, prof_mod, no_preimports, prof_imports,
519+
add_one, add_two, add_three, add_four, add_operator, main):
497520
"""
498521
Test the execution of a module.
499522
"""
@@ -506,6 +529,8 @@ def test_autoprofile_exec_module(
506529
args = [sys.executable, '-m', 'kernprof']
507530
if prof_mod is not None:
508531
args.extend(['-p', prof_mod])
532+
if no_preimports:
533+
args.append('--no-preimports')
509534
if prof_imports:
510535
args.append('--prof-imports')
511536
args.extend(['-l', '-m', 'test_mod.subpkg.submod4', '1', '2', '3'])
@@ -524,6 +549,7 @@ def test_autoprofile_exec_module(
524549

525550
assert ('Function: add_one' in raw_output) == add_one
526551
assert ('Function: add_two' in raw_output) == add_two
552+
assert ('Function: add_three' in raw_output) == add_three
527553
assert ('Function: add_four' in raw_output) == add_four
528554
assert ('Function: add_operator' in raw_output) == add_operator
529555
assert ('Function: _main' in raw_output) == main
@@ -625,78 +651,14 @@ def test_autoprofile_from_inlined_script(outfile, expected_outfile) -> None:
625651

626652

627653
@pytest.mark.parametrize(
628-
['prof_mod', 'eager_preimports',
629-
'add_one', 'add_two', 'add_three', 'add_four', 'add_operator', 'main'],
630-
# Test that `--eager-preimports` know to exclude the script run
631-
# (so as not to inadvertantly run it twice)
632-
[('script.py', None, False, False, False, False, False, True),
633-
(None, 'script.py', False, False, False, False, False, True),
634-
# Test explicitly passing targets to `--eager-preimports`
635-
(['test_mod.submod1,test_mod.submod2', 'test_mod.subpkg.submod4'], None,
636-
True, True, False, False, True, False),
637-
(['test_mod.submod1,test_mod.submod2'], ['test_mod.subpkg.submod4'],
638-
True, True, False, True, True, False),
639-
(None, ['test_mod.submod1,test_mod.submod2', 'test_mod.subpkg.submod4'],
640-
True, True, False, True, True, False),
641-
# Test implicitly passing targets to `--eager-preimports`
642-
(['test_mod.submod1,test_mod.submod2', 'test_mod.subpkg.submod4'], True,
643-
True, True, False, True, True, False)])
644-
def test_autoprofile_eager_preimports(
645-
prof_mod, eager_preimports,
646-
add_one, add_two, add_three, add_four, add_operator, main):
647-
"""
648-
Test eager imports with the `-e`/`--eager-preimports` flag.
649-
"""
650-
with tempfile.TemporaryDirectory() as tmpdir:
651-
temp_dpath = ub.Path(tmpdir)
652-
_write_demo_module(temp_dpath)
653-
654-
args = [sys.executable, '-m', 'kernprof']
655-
if prof_mod is not None:
656-
if isinstance(prof_mod, str):
657-
prof_mod = [prof_mod]
658-
for target in prof_mod:
659-
args.extend(['-p', target])
660-
if eager_preimports in (True,):
661-
args.append('-e')
662-
elif eager_preimports is not None:
663-
if isinstance(eager_preimports, str):
664-
eager_preimports = [eager_preimports]
665-
for target in eager_preimports:
666-
args.extend(['-e', target])
667-
args.extend(['-l', 'script.py'])
668-
proc = ub.cmd(args, cwd=temp_dpath, verbose=2)
669-
# Check that pre-imports don't accidentally run the code twice
670-
assert proc.stdout.count('7.9') == 1
671-
print(proc.stdout)
672-
print(proc.stderr)
673-
proc.check_returncode()
674-
675-
prof = temp_dpath / 'script.py.lprof'
676-
677-
args = [sys.executable, '-m', 'line_profiler', os.fspath(prof)]
678-
proc = ub.cmd(args, cwd=temp_dpath)
679-
raw_output = proc.stdout
680-
print(raw_output)
681-
proc.check_returncode()
682-
683-
assert ('Function: add_one' in raw_output) == add_one
684-
assert ('Function: add_two' in raw_output) == add_two
685-
assert ('Function: add_three' in raw_output) == add_three
686-
assert ('Function: add_four' in raw_output) == add_four
687-
assert ('Function: add_operator' in raw_output) == add_operator
688-
assert ('Function: main' in raw_output) == main
689-
690-
691-
@pytest.mark.parametrize(
692-
('eager_preimports, function, method, class_method, static_method, '
654+
('prof_mod, function, method, class_method, static_method, '
693655
'descriptor'),
694656
[('my_module', True, True, True, True, True),
695657
# `function()` included in profiling via `Class.partial_method()`
696658
('my_module.Class', True, True, True, True, True),
697659
('my_module.Class.descriptor', False, False, False, False, True)])
698660
def test_autoprofile_callable_wrapper_objects(
699-
eager_preimports, function, method, class_method, static_method,
661+
prof_mod, function, method, class_method, static_method,
700662
descriptor):
701663
"""
702664
Test that on-import profiling catches various callable-wrapper
@@ -747,7 +709,7 @@ def descriptor(self):
747709

748710
with ub.ChDir(temp_dpath):
749711
args = [sys.executable, '-m', 'kernprof',
750-
'-e', eager_preimports, '-lv', 'script.py']
712+
'-p', prof_mod, '-lv', 'script.py']
751713
python_path = os.environ.get('PYTHONPATH')
752714
if python_path:
753715
python_path = '{}:{}'.format(path, python_path)

0 commit comments

Comments
 (0)