This repository has been archived by the owner on Jun 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run-benchmarks.py
executable file
·354 lines (309 loc) · 12.9 KB
/
run-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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/python3
import collections
import csv
import itertools
import json
import os
import random
import string
import subprocess
import tempfile
import time
import requests
pgo_data_dir = "pgo-data"
LOAD_MODE_HANDSHAKE = "handshake"
LOAD_MODE_REQUEST = "request"
HaystackSize = collections.namedtuple("HaystackSize", ("count", "length"))
DEFAULT_DIMENSIONS = {
"protocol": ['http', 'https1', 'https2'],
"native": [False, True],
"tcnative": [False, True],
"epoll": [False], # epoll has little impact
"json": ["jackson"], # no serde for now
"micronaut": ["3.8", "4.0"],
"java": ["17"],
"haystack_size": [HaystackSize(6, 6), HaystackSize(6, 1000), HaystackSize(6, 100_000), HaystackSize(1000, 6), HaystackSize(100_000, 6)],
"load_mode": [LOAD_MODE_REQUEST]
}
COMMON_DIMENSIONS = ("protocol", "haystack_size", "load_mode", "native")
COMMAND_DIMENSIONS = ("tcnative", "epoll", "json", "micronaut", "java")
RunParameters = collections.namedtuple("RunParameters", DEFAULT_DIMENSIONS.keys())
GatlingResult = collections.namedtuple("GatlingResult", ("ko", "ok_times"))
BOLD = '\033[1m'
RED = '\033[91m'
DEFAULT = '\033[0m'
def build_parameters(dimensions):
combs = [
RunParameters(*combination)
for combination in itertools.product(*dimensions.values())
]
return [
c for c in combs
# disable epoll/tcnative for native image, because they don't work anyway.
if not c.native or (not c.tcnative and not c.epoll)
]
def run(cmd):
print(BOLD + cmd + DEFAULT)
subprocess.run(cmd, shell=True, check=True)
def run_gatling(protocol: str, duration_per_test: int):
report_dir = "load-generator-gatling/build/reports/gatling"
os.makedirs(report_dir, exist_ok=True)
old_reports = set(os.listdir(report_dir))
run(f"./gradlew :load-generator-gatling:gatlingRun -Dprotocol={protocol} -Dduration={duration_per_test}")
new_reports = set(os.listdir(report_dir)) - old_reports
if len(new_reports) != 1:
raise Exception("Could not find gatling report")
simulation_log = os.path.join(report_dir, list(new_reports)[0], "simulation.log")
ok_times = []
ko = 0
with open(simulation_log) as f:
reader = csv.reader(f, delimiter='\t')
for row in reader:
if row[0] == "REQUEST":
time = int(row[4]) - int(row[3])
ok = row[5] == "OK"
if ok:
ok_times.append(time)
else:
ko += 1
return GatlingResult(
ko=ko,
ok_times=ok_times
)
def parse_h2load_time(time: str) -> float:
if time.endswith("ns"):
return float(time[:-2].lstrip())
if time.endswith("µs") or time.endswith("us"):
return float(time[:-2].lstrip()) * 1000
if time.endswith("ms"):
return float(time[:-2].lstrip()) * 1000_000
if time.endswith("s"):
return float(time[:-1].lstrip()) * 1000_000_000
raise ValueError(f"Cannot parse time: {time}")
SummaryStats = collections.namedtuple("SummaryStats", ("min", "max", "mean", "sd", "dsd"))
def parse_h2load_metric(line: str):
line = line[len("time for request: "):] # remove caption
parts = line.strip().split()
return SummaryStats(
min=parse_h2load_time(parts[0]),
max=parse_h2load_time(parts[1]),
mean=parse_h2load_time(parts[2]),
sd=parse_h2load_time(parts[3]),
dsd=float(parts[4].lstrip().removesuffix("%")) / 100,
)
H2loadResultAggregate = collections.namedtuple("H2loadResultAggregate", ("time_for_request", "time_for_connect", "time_to_1st_byte"))
H2loadResultIndividual = collections.namedtuple("H2loadResultIndividual", ("times_for_requests",))
def random_string(length):
return ''.join(random.choices(string.ascii_lowercase, k=length))
def run_h2load(params: RunParameters, duration_per_test: int, _warmup: bool = True):
# could use a temp file, but this allows running the tests outside of this script
body_file_name = f"test-body-{params.haystack_size.count}-{params.haystack_size.length}.json"
with open(body_file_name, mode="w") as body_file:
haystack = [random_string(params.haystack_size.length) for _ in range(params.haystack_size.count)]
offset = random.randrange(0, params.haystack_size.length - 3)
json.dump({
"haystack": haystack,
"needle": random.choice(haystack)[offset:offset + 3]
}, body_file)
cmd = [
"h2load",
"--no-tls-proto=http/1.1", # for http, always use http/1.1
"--npn-list=" + ('http/1.1' if params.protocol == 'https1' else 'h2'), # for https, set the right protocol
"-d", body_file.name,
"-H", "Content-Type: application/json"
]
uri = ("http://localhost:8080" if params.protocol == "http" else "https://localhost:8443") + "/search/find"
if params.load_mode == LOAD_MODE_HANDSHAKE:
if _warmup:
run_h2load(params, 10, _warmup=False)
conn_per_s = 10
total_conns = conn_per_s * duration_per_test
cmd += [
f"--clients={total_conns}",
f"--requests={total_conns}",
f"--rate={conn_per_s}",
"--threads=8",
uri,
]
print(BOLD + " ".join(cmd) + DEFAULT)
output = subprocess.run(cmd, check=True, stdout=subprocess.PIPE).stdout.decode("utf-8")
print(output)
tfr = None
tfc = None
ttfb = None
for line in output.split("\n"):
if line.startswith("time for request:"):
tfr = parse_h2load_metric(line)
elif line.startswith("time for connect:"):
tfc = parse_h2load_metric(line)
elif line.startswith("time to 1st byte:"):
ttfb = parse_h2load_metric(line)
if not tfr or not tfc or not ttfb:
raise Exception("No h2load result")
return H2loadResultAggregate(tfr, tfc, ttfb)
elif params.load_mode == LOAD_MODE_REQUEST:
with tempfile.NamedTemporaryFile(mode="r") as log:
cmd += [
f"--duration={duration_per_test}",
f"--warm-up-time={duration_per_test//2}",
"--clients=8",
"--threads=8",
f"--log-file={log.name}",
uri,
]
print(BOLD + " ".join(cmd) + DEFAULT)
subprocess.run(cmd, check=True)
times = []
for line in log:
(start, status, duration) = line.split("\t")
times.append(int(duration.strip()) * 1000) # µs -> ns
return H2loadResultIndividual(times)
def build_server_command(params: RunParameters):
combination_string = build_combination_string(params)
if params.native:
return [f"build/native-images/{combination_string}"]
else:
return ["java", "-Xmx1G", "-jar", f"build/libs/{combination_string}-all.jar"]
def build_combination_string(params):
combination_string = "-".join([
k + "-" + (params[i] if type(params[i]) is str else ("on" if params[i] else "off"))
for i, k in enumerate(DEFAULT_DIMENSIONS.keys())
if k in COMMAND_DIMENSIONS
])
return combination_string
def compile_standard_artifacts():
run(f"./gradlew shadowJar nativeCompile -DpgoDataDirectory={os.path.abspath(pgo_data_dir)}")
def benchmark(duration_per_test, parameters):
compile_standard_artifacts()
results = {}
for i, params in enumerate(parameters):
print(BOLD + f"Running test {i + 1}/{len(parameters)}")
server_cmd = build_server_command(params)
print(BOLD + " ".join(server_cmd) + DEFAULT)
server_proc = subprocess.Popen(server_cmd)
try:
time.sleep(1 if params.native else 4) # wait for startup
results[params] = run_h2load(params, duration_per_test)
finally:
server_proc.terminate()
server_proc.wait()
with open("results.json", "w") as f:
json.dump([
{
"parameters": param._asdict(),
**{
k: v._asdict() if type(v) is SummaryStats else v
for k, v
in result._asdict().items()
}
}
for param, result in results.items()
], f)
for param, result in results.items():
if type(result) is GatlingResult:
result.ok_times.sort()
print(
param,
len(result.ok_times),
result.ko,
result.ok_times[int(len(result.ok_times) * 0.5)],
result.ok_times[int(len(result.ok_times) * 0.95)],
max(result.ok_times),
sum(result.ok_times) / len(result.ok_times)
)
elif type(result) is H2loadResultAggregate:
print(
param,
f"{result.time_to_1st_byte.mean / 1000000}ms"
)
def prepare_pgo(duration_per_test, parameters):
run("./gradlew nativeCompile -DpgoInstrument")
os.makedirs(pgo_data_dir, exist_ok=True)
to_run = [
p
for p in parameters
if p.protocol == DEFAULT_DIMENSIONS["protocol"][0] # one profile run for all protocols
if p.native
]
for i, params in enumerate(to_run):
print(BOLD + f"Building profile for {i + 1}/{len(to_run)}")
server_cmd = build_server_command(params)
print(BOLD + " ".join(server_cmd) + DEFAULT)
server_proc = subprocess.Popen(server_cmd)
try:
time.sleep(1) # wait for startup
for protocol in DEFAULT_DIMENSIONS["protocol"]:
for haystack_size in DEFAULT_DIMENSIONS["haystack_size"]:
pd = params._asdict()
pd["protocol"] = protocol
pd["haystack_size"] = haystack_size
run_h2load(RunParameters(**pd), duration_per_test)
finally:
server_proc.terminate()
server_proc.wait()
os.rename("default.iprof", os.path.join(pgo_data_dir, build_combination_string(params)))
def verify_features(parameters):
compile_standard_artifacts()
to_run = [
p
for p in parameters
if p.protocol == DEFAULT_DIMENSIONS["protocol"][0] # one profile run for all protocols
]
results = {}
for i, params in enumerate(to_run):
print(BOLD + f"Checking features {i + 1}/{len(to_run)}")
server_cmd = build_server_command(params)
print(BOLD + " ".join(server_cmd) + DEFAULT)
server_proc = subprocess.Popen(server_cmd)
try:
time.sleep(1 if params.native else 2) # wait for startup
results[params] = requests.get("http://localhost:8080/status").json()
finally:
server_proc.terminate()
server_proc.wait()
for param, status in results.items():
line = [str(param)]
channel_implementation: str = status["serverSocketChannelImplementation"]
ssl_provider: str = status["sslProvider"]
json_implementation: str = status["jsonMapperImplementation"]
if param.tcnative and ssl_provider == "JDK":
line.append(RED + ssl_provider + DEFAULT)
else:
line.append(ssl_provider)
if param.epoll and channel_implementation != "io.netty.channel.epoll.EpollServerSocketChannel":
line.append(RED + channel_implementation + DEFAULT)
else:
line.append(channel_implementation)
if param.json == "serde" and not json_implementation.startswith("io.micronaut.serde"):
line.append(RED + json_implementation + DEFAULT)
else:
line.append(json_implementation)
print(" ".join(line))
def main():
import argparse
parser = argparse.ArgumentParser(prog="run-benchmarks.py")
parser.add_argument("--duration-per-test", help="Duration for each gatling test", type=int)
parser.add_argument("--test-run", help="Fast test run with reduced test matrix", action='store_true')
parser.add_argument("--prepare-pgo", help="Prepare profile-guided optimization", action='store_true')
parser.add_argument("--verify-features", help="Verify that the right features are supported by the artifacts", action='store_true')
args = parser.parse_args()
duration_per_test = 60
dimensions = dict(DEFAULT_DIMENSIONS)
if args.test_run:
dimensions["tcnative"] = [True]
dimensions["epoll"] = [True]
dimensions["native"] = [False]
dimensions["json"] = ["jackson"]
dimensions["protocol"] = [DEFAULT_DIMENSIONS["protocol"][0]]
duration_per_test = 10
if args.duration_per_test is not None:
duration_per_test = args.duration_per_test
parameters = build_parameters(dimensions)
if args.prepare_pgo:
prepare_pgo(duration_per_test, parameters)
elif args.verify_features:
verify_features(parameters)
else:
benchmark(duration_per_test, parameters)
if __name__ == '__main__':
main()