Skip to content

Commit

Permalink
Add auto-profiler to complex test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Erotemic committed Aug 16, 2023
1 parent fe5a0b5 commit 9e52e05
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
8 changes: 7 additions & 1 deletion line_profiler/autoprofile/ast_profle_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ def visit_FunctionDef(self, node):
(_ast.FunctionDef): node
function/method with profiling decorator
"""
if self._profiler_name not in (d.id for d in node.decorator_list):
decor_ids = set()
for decor in node.decorator_list:
try:
decor_ids.add(decor.id)
except AttributeError:
...
if self._profiler_name not in decor_ids:
node.decorator_list.append(ast.Name(id=self._profiler_name, ctx=ast.Load()))
return self.generic_visit(node)

Expand Down
2 changes: 0 additions & 2 deletions tests/complex_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ def fib_only_called_by_process(n):
a, b = 0, 1
while a < n:
a, b = b, a + b
# FIXME: having two functions with the EXACT same code can cause issues
a = 'no longer exactly the same'


@profile
Expand Down
43 changes: 33 additions & 10 deletions tests/test_complex_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,52 @@ def test_varied_complex_invocations():
'outpath': outpath,
})

# Add case for auto-profile
# FIXME: this runs, but doesn't quite work.
cases.append({
'runner': 'kernprof',
'kern_flags': '-l --prof-mod complex_example.py',
'env_line_profile': '0',
'profile_type': 'none',
'outpath': 'complex_example.py.lprof',
'ignore_checks': True,
})

if 0:
# FIXME: this does not run with prof-imports
cases.append({
'runner': 'kernprof',
'kern_flags': '-l --prof-imports --prof-mod complex_example.py',
'env_line_profile': '0',
'profile_type': 'none',
'outpath': 'complex_example.py.lprof',
})

complex_fpath = get_complex_example_fpath()

results = []

for item in cases:
for case in cases:
temp_dpath = tempfile.mkdtemp()
with ub.ChDir(temp_dpath):
env = {}

outpath = item['outpath']
outpath = case['outpath']
if outpath:
outpath = ub.Path(outpath)

# Construct the invocation for each case
if item['runner'] == 'kernprof':
kern_flags = item['kern_flags']
if case['runner'] == 'kernprof':
kern_flags = case['kern_flags']
# FIXME:
# Note: kernprof doesn't seem to play well with multiprocessing
prog_flags = ' --process_size=0'
runner = f'{sys.executable} -m kernprof {kern_flags}'
else:
env['LINE_PROFILE'] = item["env_line_profile"]
env['LINE_PROFILE'] = case["env_line_profile"]
runner = f'{sys.executable}'
prog_flags = ''
env['PROFILE_TYPE'] = item["profile_type"]
env['PROFILE_TYPE'] = case["profile_type"]
command = f'{runner} {complex_fpath}' + prog_flags

HAS_SHELL = LINUX
Expand All @@ -101,7 +123,7 @@ def test_varied_complex_invocations():

info.check_returncode()

result = item.copy()
result = case.copy()
if outpath:
result['outsize'] = outpath.stat().st_size
else:
Expand All @@ -120,6 +142,7 @@ def test_varied_complex_invocations():
rich.print(table)

# Ensure the scripts that produced output produced non-trivial output
for result in results:
if result['outpath'] is not None:
assert result['outsize'] > 100
if not case.get('ignore_checks', False):
for result in results:
if result['outpath'] is not None:
assert result['outsize'] > 100

0 comments on commit 9e52e05

Please sign in to comment.