Created
September 27, 2018 12:43
-
-
Save ods/4fd92af20d7020f0a92d99ab12407887 to your computer and use it in GitHub Desktop.
Benchmark for PR to serialize execution of SQL in asyncpg: https://github.com/MagicStack/asyncpg/pull/367
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Benchmark for PR to serialize execution of SQL in asyncpg. | |
See: https://github.com/MagicStack/asyncpg/pull/367 | |
""" | |
import asyncio | |
from asyncio import Lock | |
class _Atomic: | |
__slots__ = ('_acquired',) | |
def __init__(self): | |
self._acquired = 0 | |
def __enter__(self): | |
if self._acquired: | |
raise Exception() | |
self._acquired = 1 | |
def __exit__(self, t, e, tb): | |
self._acquired = 0 | |
class _AsyncAtomic: | |
__slots__ = ('_acquired',) | |
def __init__(self): | |
self._acquired = 0 | |
async def __aenter__(self): | |
if self._acquired: | |
raise Exception() | |
self._acquired = 1 | |
async def __aexit__(self, t, e, tb): | |
self._acquired = 0 | |
async def nothing(): | |
pass | |
async def atomic(): | |
with _Atomic(): | |
pass | |
async def async_atomic(): | |
async with _AsyncAtomic(): | |
pass | |
async def lock(): | |
async with Lock(): | |
pass | |
def bench(f, c=100000): | |
async def wrapper(): | |
for i in range(c): | |
await f() | |
asyncio.run(wrapper()) | |
if __name__ == '__main__': | |
from timeit import timeit | |
for fn in ['nothing', 'atomic', 'async_atomic', 'lock']: | |
t = timeit(stmt=f'bench({fn})', number=10, globals=globals()) | |
print(f'{fn:12s}: {t:.4f} usec') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment