forked from edgedb/imdbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_go.py
executable file
·181 lines (140 loc) · 4.94 KB
/
bench_go.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
#!/usr/bin/env python3
#
# Copyright (c) 2019 MagicStack Inc.
# All rights reserved.
#
# See LICENSE for details.
##
import json
import pathlib
import subprocess
import typing
import numpy as np
import _shared
class Result(typing.NamedTuple):
benchmark: str
queryname: str
nqueries: int
duration: int
min_latency: int
avg_latency: int
max_latency: int
latency_stats: typing.List[int]
samples: typing.List[str]
def print_result(ctx, result: Result):
print(f'== {result.benchmark} : {result.queryname} ==')
print(f'queries:\t{result.nqueries}')
print(f'qps:\t\t{result.nqueries // ctx.duration} q/s')
print(f'min latency:\t{result.min_latency / 100:.2f}ms')
print(f'avg latency:\t{result.avg_latency / 100:.2f}ms')
print(f'max latency:\t{result.max_latency / 100:.2f}ms')
print()
def run_query(ctx, benchmark, queryname, querydata, port):
dirn = pathlib.Path(_shared.IMPLEMENTATIONS[benchmark].module.__file__)
exe = dirn.parent.parent / 'gobench'
# Hasura needs a special GraphQL API path, otherwise it should be ignored
if 'hasura' in benchmark:
path = '/v1/graphql'
elif 'edgedb_go_graphql' == benchmark:
path = "/db/edgedb/graphql"
elif 'edgedb_go_http' == benchmark:
path = "/db/edgedb/edgeql"
else:
path = "/"
int_ids = 'edgedb' not in benchmark
cmd = [exe, '--concurrency', ctx.concurrency, '--duration', ctx.duration,
'--timeout', ctx.timeout, '--warmup-time', ctx.warmup_time,
'--output-format', 'json', '--host', ctx.db_host,
'--port', port, '--path', path, '--ids-are-ints', int_ids,
'--nsamples', '10', '--benchmark', benchmark,
'--queryname', queryname, '--', '-']
cmd = [str(c) for c in cmd]
try:
proc = subprocess.run(
cmd, input=json.dumps(querydata), text=True,
capture_output=True, check=True,
)
except subprocess.CalledProcessError as e:
print(e.stderr)
raise
data = json.loads(proc.stdout)
avg_latency = np.average(
np.arange(len(data['latency_stats'])),
weights=data['latency_stats'])
return Result(
benchmark=benchmark,
queryname=queryname,
nqueries=data['nqueries'],
duration=data['duration'],
min_latency=data['min_latency'],
avg_latency=avg_latency,
max_latency=data['max_latency'],
latency_stats=data['latency_stats'],
samples=data['samples'],
)
def run_bench(ctx, benchmark, queries_mod):
results = []
queries = queries_mod.get_queries(ctx)
port = queries_mod.get_port(ctx)
for queryname in ctx.queries:
querydata = queries[queryname]
# Potentially setup the benchmark state
conn = queries_mod.connect(ctx)
queries_mod.setup(ctx, conn, queryname)
queries_mod.close(ctx, conn)
res = run_query(ctx, benchmark, queryname, querydata, port)
results.append(res)
print_result(ctx, res)
# Potentially clean up after the benchmarks
conn = queries_mod.connect(ctx)
queries_mod.cleanup(ctx, conn, queryname)
queries_mod.close(ctx, conn)
return results
def main():
ctx, _ = _shared.parse_args(
prog_desc='EdgeDB Databases Benchmark (Go drivers)',
out_to_json=True)
print('============ Go ============')
print(f'concurrency:\t{ctx.concurrency}')
print(f'warmup time:\t{ctx.warmup_time} seconds')
print(f'duration:\t{ctx.duration} seconds')
print(f'queries:\t{", ".join(q for q in ctx.queries)}')
print(f'benchmarks:\t{", ".join(b for b in ctx.benchmarks)}')
print()
data = []
for benchmark in ctx.benchmarks:
bench_desc = _shared.IMPLEMENTATIONS[benchmark]
if bench_desc.language != 'go':
continue
queries_mod = _shared.IMPLEMENTATIONS[benchmark].module
res = run_bench(ctx, benchmark, queries_mod)
data.append(res)
if ctx.json:
json_data = []
for results in data:
json_results = []
for r in results:
json_results.append({
'queryname': r.queryname,
'nqueries': r.nqueries,
'min_latency': r.min_latency,
'max_latency': r.max_latency,
'latency_stats': [int(i) for i in r.latency_stats],
'samples': r.samples,
})
json_data.append({
'benchmark': results[0].benchmark,
'duration': results[0].duration,
'queries': json_results,
})
data = json.dumps({
'language': 'go',
'concurrency': ctx.concurrency,
'warmup_time': ctx.warmup_time,
'duration': ctx.duration,
'data': json_data,
})
with open(ctx.json, 'wt') as f:
f.write(data)
if __name__ == '__main__':
main()