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

Overhead benchmark #6

Open
paulocheque opened this issue Jan 3, 2024 · 2 comments
Open

Overhead benchmark #6

paulocheque opened this issue Jan 3, 2024 · 2 comments

Comments

@paulocheque
Copy link

paulocheque commented Jan 3, 2024

Hi, very insteresting library!

Do you have any benchmark to calculate the overhead for async and for sync functions using the decorator?

Something like:

def sync_func(): pass
async def async_func(): pass

@async_to_sync_wraps
async def wrapped_async_func(): pass
timeit sync_func()
timeit await async_func()
timeit wrapped_async_func()
timeit await wrapped_async_func()
@MrNaif2018
Copy link
Member

Hi, thanks! I did not benchmark it before, but it's used in our production systems. But overall I think in native async case it should be almost a no-op and in sync case it will slow down depending on in which context we are running. I would appreciate any help in creating those benchmarks and I'll add them to our test suite

@paulocheque
Copy link
Author

paulocheque commented Jan 4, 2024

ps: you can add from universalasync import async_to_sync_wraps in README

import timeit
import asyncio

from universalasync import async_to_sync_wraps


def sync_func(): pass

async def async_func(): pass

@async_to_sync_wraps
async def wrapped_async_func(): pass

def run_async_func():
    asyncio.run(async_func())

def run_wrapped_async_func():
    asyncio.run(wrapped_async_func())

ITERATIONS = 10000

sync_time_1 = timeit.timeit(sync_func, number=ITERATIONS)
sync_time_2 = timeit.timeit(wrapped_async_func, number=ITERATIONS)

async_time_1 = timeit.timeit(run_async_func, number=ITERATIONS)
async_time_2 = timeit.timeit(run_wrapped_async_func, number=ITERATIONS)

print(f'Synchronous functions: {sync_time_1} vs {sync_time_2}')
print(f'Asynchronous functions: {async_time_1} vs {async_time_2}')

I got this error: ValueError: a coroutine was expected, got None
on the line
async_time_2 = timeit.timeit(run_wrapped_async_func, number=ITERATIONS)

Overhead for the sync function: 0.00020570901688188314 vs 0.28296741703525186
If I calculated correctly, it is an overhead of 137025%

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

No branches or pull requests

2 participants