forked from long2ice/asyncmy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_insert.py
75 lines (61 loc) · 1.7 KB
/
benchmark_insert.py
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
import asyncio
import time
from rich.pretty import pprint
import aiomysql
import asyncmy
from benchmark import (
COUNT,
conn_mysqlclient,
conn_pymysql,
connection_kwargs,
data,
sql,
)
from benchmark.decorators import cleanup
@cleanup
async def insert_asyncmy():
conn = await asyncmy.connect(**connection_kwargs)
async with conn.cursor() as cur:
t = time.time()
ret = await cur.executemany(sql, data)
assert ret == COUNT
return time.time() - t
@cleanup
async def insert_aiomysql():
conn = await aiomysql.connect(**connection_kwargs)
async with conn.cursor() as cur:
t = time.time()
ret = await cur.executemany(sql, data)
assert ret == COUNT
return time.time() - t
@cleanup
def insert_mysqlclient():
cur = conn_mysqlclient.cursor()
t = time.time()
ret = cur.executemany(sql, data)
assert ret == COUNT
return time.time() - t
@cleanup
def insert_pymysql():
cur = conn_pymysql.cursor()
t = time.time()
ret = cur.executemany(sql, data)
assert ret == COUNT
return time.time() - t
def benchmark_insert():
loop = asyncio.get_event_loop()
insert_mysqlclient_ret = insert_mysqlclient()
insert_asyncmy_ret = loop.run_until_complete(insert_asyncmy())
insert_pymysql_ret = insert_pymysql()
insert_aiomysql_ret = loop.run_until_complete(insert_aiomysql())
return sorted(
{
"mysqlclient": insert_mysqlclient_ret,
"asyncmy": insert_asyncmy_ret,
"pymysql": insert_pymysql_ret,
"aiomysql": insert_aiomysql_ret,
}.items(),
key=lambda x: x[1],
)
if __name__ == "__main__":
pprint(benchmark_insert())