joerick/pyinstrument

async_mode=strict raises NotImplementedError

tonybaloney opened this issue · 2 comments

I'm running pyinstrument from pytest and just tried setting the async mode to strict:

@pytest.fixture(autouse=True)
def profile(request):
    # Turn profiling on
    profiler = Profiler(async_mode="strict")
    profiler.start()

    yield  # Run test

    profiler.stop()
    # Uncomment if you want to see on the CLI
    # profiler.print(show_all=True)
    (TESTS_ROOT / "benchmarks" / ".profiles").mkdir(exist_ok=True)
    results_file = TESTS_ROOT / "benchmarks" / ".profiles" / f"{request.node.name}.html"
    with open(results_file, "w", encoding="utf-8") as f:
        f.write(profiler.output_html())

This raising the following exception when trying to print the output file.

Traceback (most recent call last):
  File "/workspaces/azure-functions-python-worker/tests/benchmarks/conftest.py", line 26, in profile
    f.write(profiler.output_html())
  File "/home/vscode/.local/lib/python3.9/site-packages/pyinstrument/profiler.py", line 305, in output_html
    return self.output(renderer=renderers.HTMLRenderer(timeline=timeline))
  File "/home/vscode/.local/lib/python3.9/site-packages/pyinstrument/profiler.py", line 323, in output
    return renderer.render(session)
  File "/home/vscode/.local/lib/python3.9/site-packages/pyinstrument/renderers/html.py", line 42, in render
    session_json = self.render_json(session)
  File "/home/vscode/.local/lib/python3.9/site-packages/pyinstrument/renderers/html.py", line 90, in render_json
    return json_renderer.render(session)
  File "/home/vscode/.local/lib/python3.9/site-packages/pyinstrument/renderers/jsonrenderer.py", line 73, in render
    property_decls.append('"root_frame": %s' % self.render_frame(frame))
  File "/home/vscode/.local/lib/python3.9/site-packages/pyinstrument/renderers/jsonrenderer.py", line 45, in render_frame
    property_decls.append('"await_time": %f' % frame.await_time())
  File "/home/vscode/.local/lib/python3.9/site-packages/pyinstrument/frame.py", line 276, in await_time
    await_time += child.await_time()
  File "/home/vscode/.local/lib/python3.9/site-packages/pyinstrument/frame.py", line 276, in await_time
    await_time += child.await_time()
  File "/home/vscode/.local/lib/python3.9/site-packages/pyinstrument/frame.py", line 276, in await_time
    await_time += child.await_time()
  [Previous line repeated 41 more times]
  File "/home/vscode/.local/lib/python3.9/site-packages/pyinstrument/frame.py", line 84, in await_time
    raise NotImplementedError()
NotImplementedError

Any idea what I'm doing wrong?

Looks like a bug! I guess nobody has tried to use strict mode with html output before 😜.

My guess is that OutOfContextFrame needs to implement await_time and return zero. A unit test that calls await_time on the session root frame would confirm this. If you have time for a PR, that would be great, otherwise, I'll get to it in a couple days :)

Raised a PR, also quickly checked that there were no other 'abstract' methods that weren't implemented (there aren't)