-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathbenchmarks.py
194 lines (171 loc) · 6.47 KB
/
benchmarks.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""
This module is used for testing speed of aiochclient after
any changes in its code. It is useful for comparing its
results with older versions speed. Results mostly depends
on the part which makes serialize and deserialize work.
Pay attention to that fact, that this benchmark is just one
task (all queries go one by one) without async staff.
So you can get much more speed from parallelling all those
IO with asyncio instruments.
=== Last Results ============================================
== Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 03:13:28) ==
= Pure Python ===============================================
AIOCHCLIENT
= Pure Python ==============================================
selects
- Avg time for selecting 10000 rows from 100 runs: 0.0614057207107544 sec. Total: 6.1405720710754395
Speed: 162851 rows/sec
selects with decoding
- Avg time for selecting 10000 rows from 100 runs: 0.2676021885871887 sec (with decoding). Total: 26.760218858718872
Speed: 37368 rows/sec
inserts
- Avg time for inserting 10000 rows from 100 runs: 0.2058545708656311 sec. Total: 20.58545708656311
Speed: 48577 rows/sec
= With Cython ext ===========================================
selects
- Avg time for selecting 10000 rows from 100 runs: 0.0589641809463501 sec. Total: 5.89641809463501
Speed: 169594 rows/sec
selects with decoding
- Avg time for selecting 10000 rows from 100 runs: 0.14500005006790162 sec (with decoding). Total: 14.500005006790161
Speed: 68965 rows/sec
inserts
- Avg time for inserting 10000 rows from 100 runs: 0.13569785118103028 sec. Total: 13.569785118103027
Speed: 73693 rows/sec
AIOCH
selects with decoding
- Avg time for selecting 10000 rows from 100 runs: 0.32889273166656496 sec. Total: 32.889273166656494
Speed: 30405 rows/sec
"""
import asyncio
import datetime as dt
import time
import uuid
import uvloop
from aioch import Client
from aiohttp import ClientSession
from aiochclient import ChClient
def row_data():
return (
1,
2,
3.14,
"hello",
"world world \nman",
dt.date.today(),
dt.datetime.utcnow(),
"hello",
None,
["q", "w", "e", "r"],
uuid.uuid4(),
)
async def prepare_db(client):
await client.execute("DROP TABLE IF EXISTS benchmark_tbl")
await client.execute(
"""
CREATE TABLE benchmark_tbl (
a UInt16,
b Int16,
c Float32,
d String,
e FixedString(16),
f Date,
g DateTime,
h Enum16('hello' = 1, 'world' = 2),
j Nullable(Int8),
k Array(String),
u UUID
) ENGINE = Memory
"""
)
async def insert_rows(client, test_data, number):
await client.execute(
"INSERT INTO benchmark_tbl VALUES", *(test_data for _ in range(number))
)
async def bench_selects(*, retries: int, rows: int):
print("AIOCHCLIENT selects")
async with ClientSession() as s:
client = ChClient(s)
# prepare environment
await prepare_db(client)
await insert_rows(client, row_data(), rows)
# actual testing
start_time = time.time()
for _ in range(retries):
await client.fetch("SELECT * FROM benchmark_tbl")
total_time = time.time() - start_time
avg_time = total_time / retries
speed = int(1 / avg_time * rows)
print(
f"- Avg time for selecting {rows} rows from {retries} runs: {avg_time} sec. Total: {total_time}"
)
print(f" Speed: {speed} rows/sec")
async def bench_selects_with_decoding(*, retries: int, rows: int):
print("AIOCHCLIENT selects with decoding")
async with ClientSession() as s:
client = ChClient(s, compress_response=True)
# prepare environment
await prepare_db(client)
await insert_rows(client, row_data(), rows)
# actual testing
start_time = time.time()
for _ in range(retries):
selected_rows = await client.fetch("SELECT * FROM benchmark_tbl")
# decoding:
selected_rows = [row[0] for row in selected_rows]
total_time = time.time() - start_time
avg_time = total_time / retries
speed = int(1 / avg_time * rows)
print(
f"- Avg time for selecting {rows} rows from {retries} runs: {avg_time} sec (with decoding). Total: {total_time}"
)
print(f" Speed: {speed} rows/sec")
async def bench_inserts(*, retries: int, rows: int):
print("AIOCHCLIENT inserts")
async with ClientSession() as s:
client = ChClient(s, compress_response=True)
# prepare environment
await prepare_db(client)
# actual testing
one_row = row_data()
start_time = time.time()
for _ in range(retries):
await client.execute(
"INSERT INTO benchmark_tbl VALUES", *(one_row for _ in range(rows))
)
total_time = time.time() - start_time
avg_time = total_time / retries
speed = int(1 / avg_time * rows)
print(
f"- Avg time for inserting {rows} rows from {retries} runs: {avg_time} sec. Total: {total_time}"
)
print(f" Speed: {speed} rows/sec")
async def bench_selects_aioch_with_decoding(*, retries: int, rows: int):
print("AIOCH selects with decoding")
client = Client(host='localhost')
# prepare environment
await prepare_db(client)
await client.execute(
"INSERT INTO benchmark_tbl VALUES", list(row_data() for _ in range(rows))
)
# actual testing
start_time = time.time()
for _ in range(retries):
selected_rows = await client.execute("SELECT * FROM benchmark_tbl")
selected_rows = [row[0] for row in selected_rows]
total_time = time.time() - start_time
avg_time = total_time / retries
speed = int(1 / avg_time * rows)
print(
f"- Avg time for selecting {rows} rows from {retries} runs: {avg_time} sec. Total: {total_time}"
)
print(f" Speed: {speed} rows/sec")
async def main():
await bench_selects(retries=100, rows=10000)
await bench_selects_with_decoding(retries=100, rows=10000)
await bench_inserts(retries=100, rows=10000)
await bench_selects_aioch_with_decoding(retries=100, rows=10000)
if __name__ == "__main__":
uvloop.install()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()