-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_profilings.py
201 lines (163 loc) · 4.8 KB
/
generate_profilings.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
195
196
197
198
199
200
201
import cProfile
import pstats
from asyncio import open_connection, run
from secrets import token_hex
from protocol.application import MySQL
from protocol.async_support import create_stream_reader, create_stream_writer
async def setup(mysql: MySQL):
await mysql.query('DROP DATABASE IF EXISTS garbage')
await mysql.query('CREATE DATABASE garbage')
await mysql.query(
'CREATE TABLE garbage.garbage ('
' id INTEGER AUTO_INCREMENT PRIMARY KEY,'
' value TEXT'
')'
)
async def teardown(mysql: MySQL):
await mysql.query('DROP DATABASE IF EXISTS garbage')
async def run_profile_select(mysql: MySQL, n: int, sort: str):
perf = cProfile.Profile()
perf.enable()
for _ in range(n):
await mysql.query('SELECT * FROM garbage.garbage limit 10000')
perf.disable()
stats = pstats.Stats(perf).strip_dirs().sort_stats(sort)
stats.print_stats()
return stats.total_calls, stats.total_tt
async def run_profile_insert(mysql: MySQL, n: int, sort: str):
inserts = [
'INSERT INTO garbage.garbage (value) VALUES '
+ ','.join(f"('{token_hex(1024)}')" for _ in range(10000))
for _ in range(n)
]
perf = cProfile.Profile()
perf.enable()
for stmt in inserts:
await mysql.query(stmt)
perf.disable()
stats = pstats.Stats(perf).strip_dirs().sort_stats(sort)
stats.print_stats()
return stats.total_calls, round(stats.total_tt, 2)
async def create_connection(**kwargs):
reader, writer = await open_connection(
host='127.0.0.1',
port=3306,
)
total_bytes = {
'in': 0,
'out': 0,
}
writer_s = create_stream_writer(writer, 2)
reader_s = create_stream_reader(reader, 2)
async def read(n: int):
total_bytes['in'] += n
return await reader_s(n)
async def drain(data: bytes):
total_bytes['out'] += len(data)
await writer_s(data)
mysql = MySQL(drain, read)
await mysql.connect(
username='root',
password='local',
**kwargs,
)
return mysql, writer.close, total_bytes
async def run_tests(
n: int,
sort: str,
label: str,
output: dict,
**kwargs,
):
mysql, close, totals = await create_connection(**kwargs)
await setup(mysql)
try:
out = {
'insert': {
'calls': 0,
'time': 0,
'in': 0,
'out': 0,
},
'select': {
'calls': 0,
'time': 0,
'in': 0,
'out': 0,
},
}
print(f'\n\nINSERT ({label})')
n_calls, t_time = await run_profile_insert(mysql, n, sort)
out['insert']['calls'] = n_calls
out['insert']['time'] = t_time
out['insert']['in'] = totals['in']
out['insert']['out'] = totals['out']
print(f'\n\nSELECT ({label})')
n_calls, t_time = await run_profile_select(mysql, n, sort)
out['select']['calls'] = n_calls
out['select']['time'] = t_time
out['select']['in'] = totals['in'] - out['insert']['in']
out['select']['out'] = totals['out'] - out['insert']['out']
print('Totals')
print('- IN: ', totals['in'] / (1024 ** 2), 'MB')
print('- OUT: ', totals['out'] / (1024 ** 2), 'MB')
output[label] = out
finally:
await teardown(mysql)
def print_results(baseline: str, values: dict):
baseline_values = values[baseline]
units = {
'time': 's',
'in': 'kb',
'out': 'kb',
'calls': '',
}
for label, dataset in values.items():
print('DATASET:', label)
for key in ['select', 'insert']:
for option in ['time', 'in', 'out', 'calls']:
bl = baseline_values[key][option]
cv = dataset[key][option]
c = (cv - bl) / bl * 100
if option in ['in', 'out']:
cv = cv / 1024
print(
' -',
key,
option.ljust(5, ' '),
f'({"+" if c > 0 else ""}{c:.2f}%)',
cv,
units[option],
)
async def main(
n: int = 1,
sort: str = 'time',
):
output = {}
await run_tests(
n,
sort,
'plain',
output,
use_compression=False,
)
await run_tests(
n,
sort,
'compressed',
output,
use_compression=True,
compression_level=1,
)
await run_tests(
n,
sort,
'compressed, level=9',
output,
use_compression=True,
compression_level=9,
)
print('\n\n')
print_results('plain', output)
if __name__ == '__main__':
run(main())