Skip to content

Commit a249d35

Browse files
committed
more cython code
1 parent 972907d commit a249d35

12 files changed

+133
-104
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## 0.2
44

5+
### 0.2.1
6+
7+
- More cython code.
8+
59
### 0.2.0
610

711
- Fix `cursor.close`.

asyncmy/connection.pyx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -997,10 +997,12 @@ class Connection:
997997

998998
cdef class MySQLResult:
999999
cdef:
1000-
public connection, message, description, rows, has_next, unbuffered_active
1001-
public int affected_rows, warning_count, field_count, server_status
1000+
public connection
1001+
public bytes message
1002+
public int affected_rows, warning_count, field_count, server_status, unbuffered_active, has_next
10021003
public list fields, converters
10031004
public unsigned long insert_id
1005+
public tuple rows, description
10041006

10051007
def __init__(self, connection: Connection):
10061008
self.connection = connection
@@ -1012,7 +1014,7 @@ cdef class MySQLResult:
10121014
self.field_count = 0
10131015
self.description = None
10141016
self.rows = None
1015-
self.has_next = None
1017+
self.has_next = False
10161018
self.unbuffered_active = False
10171019

10181020
def __del__(self):

asyncmy/cursors.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ cdef class Cursor:
8181
else:
8282
raise StopAsyncIteration # noqa
8383

84-
cpdef _get_db(self):
84+
def _get_db(self):
8585
if not self.connection:
8686
raise errors.ProgrammingError("Cursor closed")
8787
return self.connection

asyncmy/protocol.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ cdef class MysqlPacket:
118118
self._position = end_pos + 1
119119
return result
120120

121-
cpdef read_length_encoded_integer(self):
121+
cpdef unsigned long read_length_encoded_integer(self):
122122
"""
123123
Read a 'Length Coded Binary' number from the data buffer.
124124
@@ -127,7 +127,7 @@ cdef class MysqlPacket:
127127
"""
128128
cdef int c = self.read_uint8()
129129
if c == NULL_COLUMN:
130-
return None
130+
return 0
131131
if c < UNSIGNED_CHAR_COLUMN:
132132
return c
133133
elif c == UNSIGNED_SHORT_COLUMN:
@@ -137,7 +137,7 @@ cdef class MysqlPacket:
137137
elif c == UNSIGNED_INT64_COLUMN:
138138
return self.read_uint64()
139139

140-
def read_length_coded_string(self):
140+
cpdef bytes read_length_coded_string(self):
141141
"""
142142
Read a 'Length Coded String' from the data buffer.
143143
@@ -147,7 +147,7 @@ cdef class MysqlPacket:
147147
"""
148148
length = self.read_length_encoded_integer()
149149
if length is None:
150-
return None
150+
return b""
151151
return self.read(length)
152152

153153
cpdef tuple read_struct(self, str fmt):

asyncmy/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__VERSION__ = "0.2.0"
1+
__VERSION__ = "0.2.1"

benchmark/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import MySQLdb
22
import pymysql
3+
import uvloop
4+
5+
uvloop.install()
36

47
connection_kwargs = dict(
58
host="127.0.0.1", port=3306, user="root", password="123456", autocommit=True

benchmark/benchmark_delete.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import asyncio
12
import time
23

34
import aiomysql
5+
import asyncmy
46
import MySQLdb
57
import pymysql
68

7-
import asyncmy
89
from benchmark import COUNT, connection_kwargs
910
from benchmark.decorators import cleanup, fill_data
1011

@@ -57,3 +58,24 @@ def delete_pymysql():
5758
ret = cur.execute("delete from test.asyncmy where `id`=%s", (i + 1,))
5859
assert ret == 1
5960
return time.time() - t
61+
62+
63+
def benchmark_delete():
64+
loop = asyncio.get_event_loop()
65+
delete_asyncmy_ret = loop.run_until_complete(delete_asyncmy())
66+
delete_mysqlclient_ret = delete_mysqlclient()
67+
delete_pymysql_ret = delete_pymysql()
68+
delete_aiomysql_ret = loop.run_until_complete(delete_aiomysql())
69+
return sorted(
70+
{
71+
"mysqlclient": delete_mysqlclient_ret,
72+
"asyncmy": delete_asyncmy_ret,
73+
"pymysql": delete_pymysql_ret,
74+
"aiomysql": delete_aiomysql_ret,
75+
}.items(),
76+
key=lambda x: x[1],
77+
)
78+
79+
80+
if __name__ == "__main__":
81+
print(benchmark_delete())

benchmark/benchmark_insert.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
import asyncio
12
import time
23

34
import aiomysql
4-
55
import asyncmy
6-
from benchmark import COUNT, conn_mysqlclient, conn_pymysql, connection_kwargs, data, sql
6+
7+
from benchmark import (
8+
COUNT,
9+
conn_mysqlclient,
10+
conn_pymysql,
11+
connection_kwargs,
12+
data,
13+
sql,
14+
)
715
from benchmark.decorators import cleanup
816

917

@@ -43,3 +51,24 @@ def insert_pymysql():
4351
ret = cur.executemany(sql, data)
4452
assert ret == COUNT
4553
return time.time() - t
54+
55+
56+
def benchmark_insert():
57+
loop = asyncio.get_event_loop()
58+
insert_mysqlclient_ret = insert_mysqlclient()
59+
insert_asyncmy_ret = loop.run_until_complete(insert_asyncmy())
60+
insert_pymysql_ret = insert_pymysql()
61+
insert_aiomysql_ret = loop.run_until_complete(insert_aiomysql())
62+
return sorted(
63+
{
64+
"mysqlclient": insert_mysqlclient_ret,
65+
"asyncmy": insert_asyncmy_ret,
66+
"pymysql": insert_pymysql_ret,
67+
"aiomysql": insert_aiomysql_ret,
68+
}.items(),
69+
key=lambda x: x[1],
70+
)
71+
72+
73+
if __name__ == "__main__":
74+
print(benchmark_insert())

benchmark/benchmark_select.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import asyncio
12
import time
23

34
import aiomysql
4-
55
import asyncmy
6-
from benchmark import COUNT, conn_mysqlclient, conn_pymysql, connection_kwargs
6+
7+
from benchmark import (
8+
COUNT,
9+
conn_mysqlclient,
10+
conn_pymysql,
11+
connection_kwargs,
12+
)
713
from benchmark.decorators import cleanup, fill_data
814

915

@@ -55,3 +61,24 @@ def select_pymysql():
5561
res = cur.fetchall()
5662
assert len(res) == 1
5763
return time.time() - t
64+
65+
66+
def benchmark_select():
67+
loop = asyncio.get_event_loop()
68+
select_mysqlclient_ret = select_mysqlclient()
69+
select_asyncmy_ret = loop.run_until_complete(select_asyncmy())
70+
select_pymysql_ret = select_pymysql()
71+
select_aiomysql_ret = loop.run_until_complete(select_aiomysql())
72+
return sorted(
73+
{
74+
"mysqlclient": select_mysqlclient_ret,
75+
"asyncmy": select_asyncmy_ret,
76+
"pymysql": select_pymysql_ret,
77+
"aiomysql": select_aiomysql_ret,
78+
}.items(),
79+
key=lambda x: x[1],
80+
)
81+
82+
83+
if __name__ == "__main__":
84+
print(benchmark_select())

benchmark/benchmark_update.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import asyncio
12
import time
23

34
import aiomysql
5+
import asyncmy
46
import MySQLdb
57
import pymysql
68

7-
import asyncmy
89
from benchmark import COUNT, connection_kwargs
910
from benchmark.decorators import cleanup, fill_data
1011

@@ -77,3 +78,24 @@ def update_pymysql():
7778
),
7879
)
7980
return time.time() - t
81+
82+
83+
def benchmark_update():
84+
loop = asyncio.get_event_loop()
85+
update_mysqlclient_ret = update_mysqlclient()
86+
update_asyncmy_ret = loop.run_until_complete(update_asyncmy())
87+
update_pymysql_ret = update_pymysql()
88+
update_aiomysql_ret = loop.run_until_complete(update_aiomysql())
89+
return sorted(
90+
{
91+
"mysqlclient": update_mysqlclient_ret,
92+
"asyncmy": update_asyncmy_ret,
93+
"pymysql": update_pymysql_ret,
94+
"aiomysql": update_aiomysql_ret,
95+
}.items(),
96+
key=lambda x: x[1],
97+
)
98+
99+
100+
if __name__ == "__main__":
101+
print(benchmark_update())

benchmark/main.py

Lines changed: 8 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,11 @@
11
import asyncio
2-
3-
import uvloop
42
from rich.pretty import pprint
3+
from benchmark.benchmark_delete import benchmark_delete
4+
from benchmark.benchmark_insert import benchmark_insert, conn_mysqlclient
5+
from benchmark.benchmark_select import benchmark_select
6+
from benchmark.benchmark_update import benchmark_update
57

6-
from benchmark import conn_mysqlclient
7-
from benchmark.benchmark_delete import (
8-
delete_aiomysql,
9-
delete_asyncmy,
10-
delete_mysqlclient,
11-
delete_pymysql,
12-
)
13-
from benchmark.benchmark_insert import (
14-
insert_aiomysql,
15-
insert_asyncmy,
16-
insert_mysqlclient,
17-
insert_pymysql,
18-
)
19-
from benchmark.benchmark_select import (
20-
select_aiomysql,
21-
select_asyncmy,
22-
select_mysqlclient,
23-
select_pymysql,
24-
)
25-
from benchmark.benchmark_update import (
26-
update_aiomysql,
27-
update_asyncmy,
28-
update_mysqlclient,
29-
update_pymysql,
30-
)
318

32-
uvloop.install()
339
loop = asyncio.get_event_loop()
3410

3511
if __name__ == "__main__":
@@ -48,66 +24,10 @@
4824
)
4925
cur.execute("truncate table test.asyncmy")
5026

51-
insert_mysqlclient_ret = insert_mysqlclient()
52-
insert_asyncmy_ret = loop.run_until_complete(insert_asyncmy())
53-
insert_pymysql_ret = insert_pymysql()
54-
insert_aiomysql_ret = loop.run_until_complete(insert_aiomysql())
55-
pprint("insert finish!")
56-
57-
select_mysqlclient_ret = select_mysqlclient()
58-
select_asyncmy_ret = loop.run_until_complete(select_asyncmy())
59-
select_pymysql_ret = select_pymysql()
60-
select_aiomysql_ret = loop.run_until_complete(select_aiomysql())
61-
pprint("select finish!")
62-
63-
update_mysqlclient_ret = update_mysqlclient()
64-
update_asyncmy_ret = loop.run_until_complete(update_asyncmy())
65-
update_pymysql_ret = update_pymysql()
66-
update_aiomysql_ret = loop.run_until_complete(update_aiomysql())
67-
pprint("update finish!")
68-
69-
delete_mysqlclient_ret = delete_mysqlclient()
70-
delete_asyncmy_ret = loop.run_until_complete(delete_asyncmy())
71-
delete_pymysql_ret = delete_pymysql()
72-
delete_aiomysql_ret = loop.run_until_complete(delete_aiomysql())
73-
pprint("delete finish!")
74-
7527
ret = {
76-
"select": sorted(
77-
{
78-
"mysqlclient": select_mysqlclient_ret,
79-
"asyncmy": select_asyncmy_ret,
80-
"pymysql": select_pymysql_ret,
81-
"aiomysql": select_aiomysql_ret,
82-
}.items(),
83-
key=lambda x: x[1],
84-
),
85-
"insert": sorted(
86-
{
87-
"mysqlclient": insert_mysqlclient_ret,
88-
"asyncmy": insert_asyncmy_ret,
89-
"pymysql": insert_pymysql_ret,
90-
"aiomysql": insert_aiomysql_ret,
91-
}.items(),
92-
key=lambda x: x[1],
93-
),
94-
"update": sorted(
95-
{
96-
"mysqlclient": update_mysqlclient_ret,
97-
"asyncmy": update_asyncmy_ret,
98-
"pymysql": update_pymysql_ret,
99-
"aiomysql": update_aiomysql_ret,
100-
}.items(),
101-
key=lambda x: x[1],
102-
),
103-
"delete": sorted(
104-
{
105-
"mysqlclient": delete_mysqlclient_ret,
106-
"asyncmy": delete_asyncmy_ret,
107-
"pymysql": delete_pymysql_ret,
108-
"aiomysql": delete_aiomysql_ret,
109-
}.items(),
110-
key=lambda x: x[1],
111-
),
28+
"select": benchmark_select(),
29+
"insert": benchmark_insert(),
30+
"update": benchmark_update(),
31+
"delete": benchmark_delete(),
11232
}
11333
pprint(ret)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "asyncmy"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
description = "A fast asyncio MySQL driver"
55
authors = ["long2ice <[email protected]>"]
66
license = "Apache-2.0"

0 commit comments

Comments
 (0)