1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
import asyncio
import time
from rich.pretty import pprint
import aiomysql
import asyncmy
import MySQLdb
import pymysql
from benchmark import COUNT, connection_kwargs
from benchmark.decorators import cleanup, fill_data
count = int(COUNT / 5)
@cleanup
@fill_data
async def delete_asyncmy():
conn = await asyncmy.connect(**connection_kwargs)
async with conn.cursor() as cur:
t = time.time()
for i in range(count):
ret = await cur.execute("delete from test.asyncmy where `id`=%s", (i + 1,))
assert ret == 1
return time.time() - t
@cleanup
@fill_data
async def delete_aiomysql():
conn = await aiomysql.connect(**connection_kwargs)
async with conn.cursor() as cur:
t = time.time()
for i in range(count):
ret = await cur.execute("delete from test.asyncmy where `id`=%s", (i + 1,))
assert ret == 1
return time.time() - t
@cleanup
@fill_data
def delete_mysqlclient():
conn = MySQLdb.connect(**connection_kwargs)
cur = conn.cursor()
t = time.time()
for i in range(count):
ret = cur.execute("delete from test.asyncmy where `id`=%s", (i + 1,))
assert ret == 1
return time.time() - t
@cleanup
@fill_data
def delete_pymysql():
conn = pymysql.connect(**connection_kwargs)
cur = conn.cursor()
t = time.time()
for i in range(count):
ret = cur.execute("delete from test.asyncmy where `id`=%s", (i + 1,))
assert ret == 1
return time.time() - t
def benchmark_delete():
loop = asyncio.get_event_loop()
delete_asyncmy_ret = loop.run_until_complete(delete_asyncmy())
delete_mysqlclient_ret = delete_mysqlclient()
delete_pymysql_ret = delete_pymysql()
delete_aiomysql_ret = loop.run_until_complete(delete_aiomysql())
return sorted(
{
"mysqlclient": delete_mysqlclient_ret,
"asyncmy": delete_asyncmy_ret,
"pymysql": delete_pymysql_ret,
"aiomysql": delete_aiomysql_ret,
}.items(),
key=lambda x: x[1],
)
if __name__ == "__main__":
pprint(benchmark_delete())
|