Skip to content

Commit f8f529b

Browse files
committed
update benchmarks
1 parent 4eed960 commit f8f529b

8 files changed

+104
-36
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ test: deps
2424
build: deps
2525
@poetry build
2626

27+
benchmark: deps
28+
python benchmark/main.py
29+
2730
ci: check test

benchmark/__init__.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import faker
12
import MySQLdb
23
import pymysql
34

@@ -6,5 +7,18 @@
67
)
78
conn_mysqlclient = MySQLdb.connect(**connection_kwargs)
89
conn_pymysql = pymysql.connect(**connection_kwargs)
9-
COUNT = 10000
10-
INSERT_COUNT = COUNT * 10
10+
COUNT = 50000
11+
faker = faker.Faker()
12+
13+
data = [
14+
(
15+
1,
16+
faker.date_time().date(),
17+
faker.date_time(),
18+
1,
19+
faker.name(),
20+
1,
21+
)
22+
for _ in range(COUNT)
23+
]
24+
sql = """INSERT INTO test.asyncmy(`decimal`, `date`, `datetime`, `float`, `string`, `tinyint`) VALUES (%s,%s,%s,%s,%s,%s)"""

benchmark/benchmark_delete.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,52 @@
66

77
import asyncmy
88
from benchmark import COUNT, connection_kwargs
9+
from benchmark.decorators import cleanup, fill_data
910

1011

12+
@cleanup
13+
@fill_data
1114
async def delete_asyncmy():
1215
conn = await asyncmy.connect(**connection_kwargs)
1316
async with conn.cursor() as cur:
1417
t = time.time()
1518
for i in range(COUNT):
16-
ret = await cur.execute("delete from test.asyncmy limit 1")
19+
ret = await cur.execute("delete from test.asyncmy where `id`=%s", (i + 1,))
1720
assert ret == 1
1821
return time.time() - t
1922

2023

24+
@cleanup
25+
@fill_data
2126
async def delete_aiomysql():
2227
conn = await aiomysql.connect(**connection_kwargs)
2328
async with conn.cursor() as cur:
2429
t = time.time()
2530
for i in range(COUNT):
26-
ret = await cur.execute("delete from test.asyncmy limit 1")
31+
ret = await cur.execute("delete from test.asyncmy where `id`=%s", (i + 1,))
2732
assert ret == 1
2833
return time.time() - t
2934

3035

36+
@cleanup
37+
@fill_data
3138
def delete_mysqlclient():
3239
conn = MySQLdb.connect(**connection_kwargs)
3340
cur = conn.cursor()
3441
t = time.time()
3542
for i in range(COUNT):
36-
ret = cur.execute("delete from test.asyncmy limit 1")
43+
ret = cur.execute("delete from test.asyncmy where `id`=%s", (i + 1,))
3744
assert ret == 1
3845
return time.time() - t
3946

4047

48+
@cleanup
49+
@fill_data
4150
def delete_pymysql():
4251
conn = pymysql.connect(**connection_kwargs)
4352
cur = conn.cursor()
4453
t = time.time()
4554
for i in range(COUNT):
46-
ret = cur.execute("delete from test.asyncmy limit 1")
55+
ret = cur.execute("delete from test.asyncmy where `id`=%s", (i + 1,))
4756
assert ret == 1
4857
return time.time() - t

benchmark/benchmark_insert.py

+10-20
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,45 @@
11
import time
22

33
import aiomysql
4-
import faker
54

65
import asyncmy
7-
from benchmark import INSERT_COUNT, conn_mysqlclient, conn_pymysql, connection_kwargs
8-
9-
faker = faker.Faker()
10-
data = [
11-
(
12-
1,
13-
faker.date_time().date(),
14-
faker.date_time(),
15-
1,
16-
faker.name(),
17-
1,
18-
)
19-
for _ in range(INSERT_COUNT)
20-
]
21-
sql = """INSERT INTO test.asyncmy(`decimal`, `date`, `datetime`, `float`, `string`, `tinyint`) VALUES (%s,%s,%s,%s,%s,%s)"""
6+
from benchmark import COUNT, conn_mysqlclient, conn_pymysql, connection_kwargs, data, sql
7+
from benchmark.decorators import cleanup
228

239

10+
@cleanup
2411
async def insert_asyncmy():
2512
conn = await asyncmy.connect(**connection_kwargs)
2613
async with conn.cursor() as cur:
2714
t = time.time()
2815
ret = await cur.executemany(sql, data)
29-
assert ret == INSERT_COUNT
16+
assert ret == COUNT
3017
return time.time() - t
3118

3219

20+
@cleanup
3321
async def insert_aiomysql():
3422
conn = await aiomysql.connect(**connection_kwargs)
3523
async with conn.cursor() as cur:
3624
t = time.time()
3725
ret = await cur.executemany(sql, data)
38-
assert ret == INSERT_COUNT
26+
assert ret == COUNT
3927
return time.time() - t
4028

4129

30+
@cleanup
4231
def insert_mysqlclient():
4332
cur = conn_mysqlclient.cursor()
4433
t = time.time()
4534
ret = cur.executemany(sql, data)
46-
assert ret == INSERT_COUNT
35+
assert ret == COUNT
4736
return time.time() - t
4837

4938

39+
@cleanup
5040
def insert_pymysql():
5141
cur = conn_pymysql.cursor()
5242
t = time.time()
5343
ret = cur.executemany(sql, data)
54-
assert ret == INSERT_COUNT
44+
assert ret == COUNT
5545
return time.time() - t

benchmark/benchmark_select.py

+9
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
import asyncmy
66
from benchmark import COUNT, conn_mysqlclient, conn_pymysql, connection_kwargs
7+
from benchmark.decorators import cleanup, fill_data
78

89

10+
@cleanup
11+
@fill_data
912
async def select_asyncmy():
1013
conn = await asyncmy.connect(**connection_kwargs)
1114
async with conn.cursor() as cur:
@@ -17,6 +20,8 @@ async def select_asyncmy():
1720
return time.time() - t
1821

1922

23+
@cleanup
24+
@fill_data
2025
async def select_aiomysql():
2126
conn = await aiomysql.connect(**connection_kwargs)
2227
async with conn.cursor() as cur:
@@ -28,6 +33,8 @@ async def select_aiomysql():
2833
return time.time() - t
2934

3035

36+
@cleanup
37+
@fill_data
3138
def select_mysqlclient():
3239
cur = conn_mysqlclient.cursor()
3340
t = time.time()
@@ -38,6 +45,8 @@ def select_mysqlclient():
3845
return time.time() - t
3946

4047

48+
@cleanup
49+
@fill_data
4150
def select_pymysql():
4251
cur = conn_pymysql.cursor()
4352
t = time.time()

benchmark/benchmark_update.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,72 @@
66

77
import asyncmy
88
from benchmark import COUNT, connection_kwargs
9+
from benchmark.decorators import cleanup, fill_data
910

1011

12+
@cleanup
13+
@fill_data
1114
async def update_asyncmy():
1215
conn = await asyncmy.connect(**connection_kwargs)
1316
async with conn.cursor() as cur:
1417
t = time.time()
1518
for i in range(COUNT):
1619
await cur.execute(
17-
"update test.asyncmy set `string`=%s where `string` != %s limit 1",
20+
"update test.asyncmy set `string`=%s where `id` = %s",
1821
(
1922
"update",
20-
"update",
23+
i + 1,
2124
),
2225
)
2326
return time.time() - t
2427

2528

29+
@cleanup
30+
@fill_data
2631
async def update_aiomysql():
2732
conn = await aiomysql.connect(**connection_kwargs)
2833
async with conn.cursor() as cur:
2934
t = time.time()
3035
for i in range(COUNT):
3136
await cur.execute(
32-
"update test.asyncmy set `string`=%s where `string` != %s limit 1",
37+
"update test.asyncmy set `string`=%s where `id` = %s",
3338
(
3439
"update",
35-
"update",
40+
i + 1,
3641
),
3742
)
3843
return time.time() - t
3944

4045

46+
@cleanup
47+
@fill_data
4148
def update_mysqlclient():
4249
conn = MySQLdb.connect(**connection_kwargs)
4350
cur = conn.cursor()
4451
t = time.time()
4552
for i in range(COUNT):
4653
cur.execute(
47-
"update test.asyncmy set `string`=%s where `string` != %s limit 1",
54+
"update test.asyncmy set `string`=%s where `id` = %s",
4855
(
4956
"update",
50-
"update",
57+
i + 1,
5158
),
5259
)
5360
return time.time() - t
5461

5562

63+
@cleanup
64+
@fill_data
5665
def update_pymysql():
5766
conn = pymysql.connect(**connection_kwargs)
5867
cur = conn.cursor()
5968
t = time.time()
6069
for i in range(COUNT):
6170
cur.execute(
62-
"update test.asyncmy set `string`=%s where `string` != %s limit 1",
71+
"update test.asyncmy set `string`=%s where `id` = %s",
6372
(
6473
"update",
65-
"update",
74+
i + 1,
6675
),
6776
)
6877
return time.time() - t

benchmark/decorators.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import asyncio
2+
import functools
3+
from asyncio import iscoroutine
4+
5+
from benchmark import conn_mysqlclient, data, sql
6+
7+
8+
def cleanup(f):
9+
@functools.wraps(f)
10+
def decorator(*args, **kwargs):
11+
cur = conn_mysqlclient.cursor()
12+
cur.execute("truncate table test.asyncmy")
13+
if iscoroutine(f):
14+
return asyncio.get_event_loop().run_until_complete(f(*args, **kwargs))
15+
else:
16+
return f(*args, **kwargs)
17+
18+
return decorator
19+
20+
21+
def fill_data(f):
22+
@functools.wraps(f)
23+
def decorator(*args, **kwargs):
24+
cur = conn_mysqlclient.cursor()
25+
cur.executemany(sql, data)
26+
if iscoroutine(f):
27+
return asyncio.get_event_loop().run_until_complete(f(*args, **kwargs))
28+
else:
29+
return f(*args, **kwargs)
30+
31+
return decorator

benchmark/main.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343
`float` float DEFAULT NULL,
4444
`string` varchar(200) DEFAULT NULL,
4545
`tinyint` tinyint DEFAULT NULL,
46-
PRIMARY KEY (`id`),
47-
KEY `asyncmy_string_index` (`string`)
46+
PRIMARY KEY (`id`)
4847
) ENGINE=InnoDB AUTO_INCREMENT=400001 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci"""
4948
)
5049
cur.execute("truncate table test.asyncmy")
@@ -53,21 +52,25 @@
5352
insert_asyncmy_ret = loop.run_until_complete(insert_asyncmy())
5453
insert_pymysql_ret = insert_pymysql()
5554
insert_aiomysql_ret = loop.run_until_complete(insert_aiomysql())
55+
pprint("insert finish!")
5656

5757
select_mysqlclient_ret = select_mysqlclient()
5858
select_asyncmy_ret = loop.run_until_complete(select_asyncmy())
5959
select_pymysql_ret = select_pymysql()
6060
select_aiomysql_ret = loop.run_until_complete(select_aiomysql())
61+
pprint("select finish!")
6162

6263
update_mysqlclient_ret = update_mysqlclient()
6364
update_asyncmy_ret = loop.run_until_complete(update_asyncmy())
6465
update_pymysql_ret = update_pymysql()
6566
update_aiomysql_ret = loop.run_until_complete(update_aiomysql())
67+
pprint("update finish!")
6668

6769
delete_mysqlclient_ret = delete_mysqlclient()
6870
delete_asyncmy_ret = loop.run_until_complete(delete_asyncmy())
6971
delete_pymysql_ret = delete_pymysql()
7072
delete_aiomysql_ret = loop.run_until_complete(delete_aiomysql())
73+
pprint("delete finish!")
7174

7275
ret = {
7376
"select": sorted(

0 commit comments

Comments
 (0)