Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bench_async_func() #124

Merged
merged 7 commits into from
Feb 3, 2022
Merged

Add bench_async_func() #124

merged 7 commits into from
Feb 3, 2022

Conversation

methane
Copy link
Contributor

@methane methane commented Feb 3, 2022

Ref #121.

doc/api.rst Show resolved Hide resolved
# use fast local variables
local_timer = time.perf_counter
local_func = func
loop = asyncio.get_event_loop()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to attempt reimplementing asyncio.run() here, but only measure run_until_complete() timing? I'm talking about creating a fresh event loop each time, and shutdown asynchronous generators and the default executor. Well, same things than asyncio.run().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use asyncio.run() instead of reimplement it.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a changelog entry in doc/changelog.rst? You need to add Version 2.3.1 section.

doc/examples/bench_async_func.py Show resolved Hide resolved
@vstinner
Copy link
Member

vstinner commented Feb 3, 2022

I tested the example on my Fedora 35 with Python 3.10. I get:

async_sleep: Mean +- std dev: 1.32 ms +- 0.02 ms

That's interesting :-) Using await asyncio.sleep(1e-6), I get:

async_sleep: Mean +- std dev: 13.0 us +- 0.6 us

@vstinner
Copy link
Member

vstinner commented Feb 3, 2022

Oh, sadly asyncio.run() doesn't exist in Python 3.6. Is it possible to reimplement asyncio.run() in pyperf on Python 3.6?

pyperf currently still supports Python 3.6: https://pyperf.readthedocs.io/en/latest/run_benchmark.html#install-pyperf

return dt

import asyncio
try:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to testing hasattr(asyncio, 'main'), since main() can raise AttributeError.

pyperf/_runner.py Show resolved Hide resolved
pyperf/_runner.py Show resolved Hide resolved
doc/api.rst Outdated

.. note::
If the ``func()`` leaves any tasks, it affects to next iteration.
So you should care about task leak for reliable benchmark.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This note is now outdated, since a new event loop is created at each iteration, no?

@vstinner
Copy link
Member

vstinner commented Feb 3, 2022

With the proposed implementation (measure time inside asyncio.run and not outside), I agree that the implementation increases the accuracy and so it's worth it to add a new method ;-)

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the updates!

@vstinner vstinner merged commit 67ea903 into psf:main Feb 3, 2022
@vstinner
Copy link
Member

vstinner commented Feb 3, 2022

I completed the changelog in commit 0814a4a.

@vstinner
Copy link
Member

vstinner commented Feb 3, 2022

This change is now part of the just released pyperf 2.3.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants