-
Notifications
You must be signed in to change notification settings - Fork 40
/
load_test_runs.py
390 lines (341 loc) · 12.4 KB
/
load_test_runs.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
from __future__ import annotations
import typing
import flood
from flood import user_io
from flood import spec
from . import vegeta
if typing.TYPE_CHECKING:
import multiprocessing
def run_load_tests(
*,
node: spec.NodeShorthand | None = None,
nodes: spec.NodesShorthand | None = None,
test: spec.LoadTest | spec.TestGenerationParameters | None = None,
tests: typing.Mapping[str, spec.LoadTest | spec.TestGenerationParameters]
| None = None,
verbose: bool | int = False,
include_deep_output: typing.Sequence[spec.DeepOutput] | None = None,
) -> typing.Mapping[str, spec.LoadTestOutput]:
"""run multiple load tests"""
# parse user_io
if (node is None) == (nodes is None):
raise Exception('must specify either node or nodes')
if (test is None) == (tests is None):
raise Exception('must specify either test or tests')
if node is not None:
node = user_io.parse_node(node)
if nodes is not None:
nodes = user_io.parse_nodes(nodes, request_metadata=False)
tqdm = user_io.outputs._get_tqdm()
pbar = {
'leave': False,
'desc': 'nodes',
'position': 0,
'colour': flood.user_io.styles['content'],
# 'disable': not verbose,
'disable': True,
}
results = {}
# case: single node and single test
if node is not None and test is not None:
results[node['name']] = schedule_load_test(
node=node,
test=test,
include_deep_output=include_deep_output,
)
# case: single node and multiple tests
elif node is not None and tests is not None:
for name, each_test in tqdm.tqdm(tests.items(), **pbar):
results[name] = schedule_load_test(
node=node,
verbose=verbose,
test=each_test,
include_deep_output=include_deep_output,
)
# case: multiple nodes and single tests
elif nodes is not None and test is not None:
for name, nd in tqdm.tqdm(nodes.items(), **pbar):
results[name] = schedule_load_test(
node=nd,
verbose=verbose,
test=test,
include_deep_output=include_deep_output,
)
# case: multiple nodes and multiple tests
elif nodes is not None and tests is not None:
for node_name, node in nodes.items():
for test_name, test in tests.items():
results[node_name + '__' + test_name] = schedule_load_test(
node=node,
verbose=verbose,
test=test,
include_deep_output=include_deep_output,
)
# case: invalid input
else:
raise Exception('invalid user_io')
# join any multiprocessing results
joined = {}
for name, result in results.items():
if isinstance(result, dict):
joined[name] = result
elif isinstance(result, tuple):
process, queue = result
process.join()
if process.exitcode != 0:
import sys
sys.exit()
results_path = queue.get()
with open(results_path, 'r') as f:
import json
test_results: spec.SingleRunResultsPayload = json.load(f)
joined[name] = test_results['results'][name]
else:
raise Exception('invalid result type')
return joined
def schedule_load_test(
*,
node: spec.NodeShorthand,
test: spec.LoadTest | spec.TestGenerationParameters,
verbose: bool | int = False,
include_deep_output: typing.Sequence[spec.DeepOutput] | None = None,
_pbar_kwargs: typing.Mapping[str, typing.Any] | None = None,
) -> (
spec.LoadTestOutput
| str
| tuple[multiprocessing.Process, multiprocessing.Queue[str]]
):
"""runs local tests synchronously, remote tests asynchronously"""
node = user_io.parse_node(node)
if node['remote'] is not None:
import multiprocessing
queue: multiprocessing.Queue[str] = multiprocessing.Queue()
process = multiprocessing.Process(
target=run_load_test,
kwargs=dict(
node=node,
test=test,
verbose=verbose,
include_deep_output=include_deep_output,
_pbar_kwargs=_pbar_kwargs,
_container=queue,
),
)
process.start()
return (process, queue)
else:
return run_load_test(
node=node,
test=test,
verbose=verbose,
include_deep_output=include_deep_output,
_pbar_kwargs=_pbar_kwargs,
)
def run_load_test(
*,
node: spec.NodeShorthand,
test: spec.LoadTest | spec.TestGenerationParameters,
verbose: bool | int = False,
_pbar_kwargs: typing.Mapping[str, typing.Any] | None = None,
_container: multiprocessing.Queue[str] | None = None,
include_deep_output: typing.Sequence[spec.DeepOutput] | None = None,
) -> spec.LoadTestOutput | str:
"""run a load test against a single node"""
# parse user_io
node = user_io.parse_node(node)
# run tests
if node.get('remote') is None:
result: spec.LoadTestOutput | str = _run_load_test_locally(
node=node,
test=test,
verbose=verbose,
_pbar_kwargs=_pbar_kwargs,
include_deep_output=include_deep_output,
)
else:
result = _run_load_test_remotely(
node=node,
test=test,
verbose=verbose,
_pbar_kwargs=_pbar_kwargs,
include_deep_output=include_deep_output,
)
if _container is not None:
if not isinstance(result, str):
raise Exception('_container only supportted for remote testing')
_container.put(result)
return result
def _run_load_test_locally(
*,
node: spec.Node,
test: spec.LoadTest | spec.TestGenerationParameters,
verbose: bool | int = False,
_pbar_kwargs: typing.Mapping[str, typing.Any] | None = None,
include_deep_output: typing.Sequence[spec.DeepOutput] | None = None,
) -> spec.LoadTestOutput:
"""run a load test from local node"""
if verbose:
flood.user_io.print_timestamped('Running load test for ' + node['name'])
# construct progress bar
if _pbar_kwargs is None:
_pbar_kwargs = {}
tqdm = user_io.outputs._get_tqdm()
tqdm_kwargs = dict(
leave=False,
desc='samples',
position=1,
colour=flood.user_io.styles['content'],
# disable=not verbose,
disable=True,
**_pbar_kwargs,
)
use_test: spec.LoadTest
if 'attacks' in test:
use_test = test # type: ignore
else:
use_test = flood.generate_test(**test)
# perform tests
results = []
for attack in tqdm.tqdm(use_test['attacks'], **tqdm_kwargs):
if verbose:
flood.user_io.print_timestamped(
'Running attack at rate = ' + str(attack['rate']) + ' rps'
)
result = vegeta.run_vegeta_attack(
url=node['url'],
calls=attack['calls'],
duration=attack['duration'],
rate=attack['rate'],
vegeta_args=attack['vegeta_args'],
verbose=verbose >= 2,
include_deep_output=include_deep_output,
)
results.append(result)
if verbose >= 2:
print()
# format output
output_data: spec.LoadTestOutput = _list_of_maps_to_map_of_lists(results) # type: ignore # noqa: E501
# format deep output
if include_deep_output is None or len(include_deep_output) == 0:
for key in list(output_data.keys()):
if key.startswith('deep_'):
output_data[key] = None # type: ignore
else:
output_data['deep_rpc_error_pairs'] = [
result['deep_rpc_error_pairs'] for result in results
]
# convert list of map of map into map of map of list
categories: list[spec.ResponseCategory] = list(
results[0]['deep_metrics'].keys() # type: ignore
)
deep_metrics = {}
for category in categories:
deep_metrics[category] = _list_of_maps_to_map_of_lists(
[result['deep_metrics'][category] for result in results] # type: ignore # noqa: E501
)
output_data['deep_metrics'] = deep_metrics # type: ignore
return output_data
def _list_of_maps_to_map_of_lists(
list_of_maps: typing.Sequence[typing.Mapping[typing.Any, typing.Any]]
) -> typing.Mapping[typing.Any, typing.Sequence[typing.Any]]:
keys = list_of_maps[0].keys()
return {key: [m[key] for m in list_of_maps] for key in keys}
def _run_load_test_remotely(
*,
node: spec.Node,
test: spec.LoadTest | spec.TestGenerationParameters,
verbose: bool | int = False,
_pbar_kwargs: typing.Mapping[str, typing.Any] | None = None,
include_deep_output: typing.Sequence[spec.DeepOutput] | None = None,
) -> str:
"""run a load test from local node"""
import os
import subprocess
import sys
import uuid
import toolstr
# parse node specification
remote = node['remote']
if remote is None:
raise Exception('not a remote node')
# check remote installation
local_installation = flood.get_local_installation()
remote_installation = flood.get_remote_installation(remote)
local_flood_version = local_installation['flood_version']
remote_flood_version = remote_installation['flood_version']
remote_vegeta_path = remote_installation['vegeta_path']
if remote_flood_version is None:
raise Exception(
'could not find flood installation on remote host ' + node['name']
)
sys.exit()
if remote_vegeta_path is None:
raise Exception(
'could not find vegeta installation on remote host ' + node['name']
)
sys.exit()
if local_flood_version != remote_flood_version:
raise Exception(
'local version of flood '
+ str(local_flood_version)
+ ' does not match remote version of flood '
+ remote_flood_version
)
# save call data, saving methods to preserve ordering in json
job_id = str(uuid.uuid4())
tempdir = '/tmp/flood__' + job_id
os.makedirs(tempdir)
test_parameters: flood.TestGenerationParameters
if 'test_parameters' in test:
test_parameters = test['test_parameters'] # type: ignore
else:
test_parameters = test
flood.runners.single_runner.single_runner_io._save_single_run_test(
test_name='',
test_parameters=test_parameters,
output_dir=tempdir,
)
# send call data to remote server
if verbose:
styles = flood.user_io.styles
node_name = (
toolstr.add_style('\[', styles['content'])
+ toolstr.add_style('node', styles['metavar'])
+ toolstr.add_style('=', styles['content'])
+ node['name']
+ toolstr.add_style(']', styles['content'])
)
flood.user_io.print_timestamped(
node_name + ' Sending tests to remote node'
)
cmd = 'rsync -r ' + tempdir + ' ' + remote + ':' + os.path.dirname(tempdir)
subprocess.call(cmd.split(' '), stderr=subprocess.DEVNULL)
# initiate benchmarks
if verbose:
flood.user_io.print_timestamped(
node_name + ' Executing test on remote node'
)
cmd_template = "ssh {host} bash -c 'source ~/.profile; python3 -m flood {test} {name}={url} --output {output} --no-figures {extra_kwargs}'" # noqa: E501
extra_kwargs = ''
if include_deep_output is not None:
if 'raw' in include_deep_output:
extra_kwargs += ' --save-raw-output'
if 'metrics' in include_deep_output:
extra_kwargs += ' --deep-check'
cmd = cmd_template.format(
host=remote,
name=node['name'],
url=node['url'],
test=tempdir,
output=tempdir,
extra_kwargs=extra_kwargs.lstrip(),
)
cmd = cmd.strip()
subprocess.check_output(cmd.split(' '), stderr=subprocess.DEVNULL)
# retrieve benchmark results
if verbose:
flood.user_io.print_timestamped(node_name + ' Retrieving results')
results_path = os.path.join(tempdir, 'results.json')
cmd = 'rsync ' + remote + ':' + results_path + ' ' + results_path
subprocess.call(cmd.split(' '), stderr=subprocess.DEVNULL)
return results_path