Skip to content

Instantly share code, notes, and snippets.

@tOlorun
Forked from nhumrich/asyncpgsa_benchmark.py
Created February 17, 2020 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tOlorun/2df211b041732b4e4ab2a686a5d9cb50 to your computer and use it in GitHub Desktop.
Save tOlorun/2df211b041732b4e4ab2a686a5d9cb50 to your computer and use it in GitHub Desktop.
asyncpgsa benchmark against aiopg.sa
import asyncio
import aiopg
import aiopg.sa
import asyncpg
import asyncpgsa
import sqlalchemy as sa
pg_tables = sa.Table(
'pg_tables', sa.MetaData(),
sa.Column('schemaname'),
sa.Column('tablename'),
sa.Column('tableowner'),
sa.Column('tablespace'),
sa.Column('hasindexes')
)
query = pg_tables.select().where(pg_tables.c.schemaname == 'pg_catalog')
loop = asyncio.get_event_loop()
async def aio_coro():
await connection.execute(query)
async def apg_coro():
await pgconn.execute(query)
run_aiopg = lambda: loop.run_until_complete(aio_coro())
run_asyncpgsa = lambda: loop.run_until_complete(apg_coro())
pgconn = asyncpg.connect(user='postgres', host='127.0.0.1', password='password', database='postgres', connection_class=asyncpgsa.connection.SAConnection)
pgconn = loop.run_until_complete(pgconn)
engine = loop.run_until_complete(aiopg.sa.create_engine(user='postgres', password='password', host='127.0.0.1', database='postgres'))
connection = loop.run_until_complete(engine.acquire())
run_asyncpgsa()
run_aiopg()
from timeit import timeit
print('aiopg.sa:', timeit(run_aiopg, number=10000))
print('asyncpgsa', timeit(run_asyncpgsa, number=10000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment