-
Notifications
You must be signed in to change notification settings - Fork 67
/
sebs.py
executable file
·658 lines (551 loc) · 20.2 KB
/
sebs.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#!/usr/bin/env python3
import json
import logging
import functools
import os
import traceback
from typing import cast, Optional
import click
import sebs
from sebs import SeBS
from sebs.types import Storage as StorageTypes
from sebs.regression import regression_suite
from sebs.utils import update_nested_dict, catch_interrupt
from sebs.faas import System as FaaSSystem
from sebs.faas.function import Trigger
PROJECT_DIR = os.path.dirname(os.path.realpath(__file__))
deployment_client: Optional[FaaSSystem] = None
sebs_client: Optional[SeBS] = None
class ExceptionProcesser(click.Group):
def __call__(self, *args, **kwargs):
try:
return self.main(*args, **kwargs)
except Exception as e:
logging.error(e)
traceback.print_exc()
logging.info("# Experiments failed! See out.log for details")
finally:
# Close
if deployment_client is not None:
deployment_client.shutdown()
if sebs_client is not None:
sebs_client.shutdown()
def simplified_common_params(func):
@click.option(
"--config",
required=True,
type=click.Path(readable=True),
help="Location of experiment config.",
)
@click.option("--output-dir", default=os.path.curdir, help="Output directory for results.")
@click.option("--output-file", default="out.log", help="Output filename for logging.")
@click.option(
"--cache",
default=os.path.join(os.path.curdir, "cache"),
help="Location of experiments cache.",
)
@click.option("--verbose/--no-verbose", default=False, help="Verbose output.")
@click.option(
"--preserve-out/--no-preserve-out",
default=True,
help="Preserve current results in output directory.",
)
@click.option(
"--language",
default=None,
type=click.Choice(["python", "nodejs"]),
help="Benchmark language",
)
@click.option("--language-version", default=None, type=str, help="Benchmark language version")
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
def common_params(func):
@click.option(
"--update-code/--no-update-code",
default=False,
help="Update function code in cache and cloud deployment.",
)
@click.option(
"--update-storage/--no-update-storage",
default=False,
help="Update benchmark storage files in cloud deployment.",
)
@click.option(
"--deployment",
default=None,
type=click.Choice(["azure", "aws", "gcp", "local", "openwhisk"]),
help="Cloud deployment to use.",
)
@click.option(
"--architecture",
default=None,
type=click.Choice(["x64", "arm64"]),
help="Target architecture",
)
@click.option(
"--container-deployment/--no-container-deployment",
default=False,
help="Deploy functions as containers (AWS only). When enabled, functions are packaged as container images and pushed to Amazon ECR."
)
@click.option(
"--resource-prefix",
default=None,
type=str,
help="Resource prefix to look for.",
)
@simplified_common_params
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
def parse_common_params(
config,
output_dir,
output_file,
cache,
verbose,
preserve_out,
update_code,
update_storage,
deployment,
language,
language_version,
architecture,
container_deployment,
resource_prefix: Optional[str] = None,
initialize_deployment: bool = True,
ignore_cache: bool = False,
storage_configuration: Optional[str] = None
):
global sebs_client, deployment_client
config_obj = json.load(open(config, "r"))
os.makedirs(output_dir, exist_ok=True)
logging_filename = os.path.abspath(os.path.join(output_dir, output_file))
sebs_client = sebs.SeBS(cache, output_dir, verbose, logging_filename)
output_dir = sebs.utils.create_output(output_dir, preserve_out, verbose)
sebs_client.logging.info("Created experiment output at {}".format(output_dir))
# CLI overrides JSON options
update_nested_dict(config_obj, ["experiments", "runtime", "language"], language)
update_nested_dict(config_obj, ["experiments", "runtime", "version"], language_version)
update_nested_dict(config_obj, ["deployment", "name"], deployment)
update_nested_dict(config_obj, ["experiments", "update_code"], update_code)
update_nested_dict(config_obj, ["experiments", "update_storage"], update_storage)
update_nested_dict(config_obj, ["experiments", "architecture"], architecture)
update_nested_dict(config_obj, ["experiments", "container_deployment"], container_deployment)
# set the path the configuration was loaded from
update_nested_dict(config_obj, ["deployment", "local", "path"], config)
if storage_configuration:
cfg = json.load(open(storage_configuration, 'r'))
update_nested_dict(config_obj, ["deployment", deployment, "storage"], cfg)
if initialize_deployment:
deployment_client = sebs_client.get_deployment(
config_obj, logging_filename=logging_filename
)
deployment_client.initialize(resource_prefix=resource_prefix)
else:
deployment_client = None
if ignore_cache:
sebs_client.ignore_cache()
catch_interrupt()
return config_obj, output_dir, logging_filename, sebs_client, deployment_client
@click.group(cls=ExceptionProcesser)
def cli():
pass
@cli.group()
def benchmark():
pass
@benchmark.command()
@click.argument("benchmark", type=str) # , help="Benchmark to be used.")
@click.argument(
"benchmark-input-size", type=click.Choice(["test", "small", "large"])
) # help="Input test size")
@click.option("--repetitions", default=5, type=int, help="Number of experimental repetitions.")
@click.option(
"--trigger",
type=click.Choice(["library", "http"]),
default="http",
help="Function trigger to be used.",
)
@click.option(
"--memory",
default=None,
type=int,
help="Override default memory settings for the benchmark function.",
)
@click.option(
"--timeout",
default=None,
type=int,
help="Override default timeout settings for the benchmark function.",
)
@click.option(
"--function-name",
default=None,
type=str,
help="Override function name for random generation.",
)
@click.option(
"--image-tag-prefix",
default=None,
type=str,
help="Attach prefix to generated Docker image tag.",
)
@click.option("--storage-configuration", default=None, type=str, help="JSON configuration of deployed storage.")
@common_params
def invoke(
benchmark,
benchmark_input_size,
repetitions,
trigger,
memory,
timeout,
function_name,
image_tag_prefix,
**kwargs,
):
(
config,
output_dir,
logging_filename,
sebs_client,
deployment_client
) = parse_common_params(**kwargs)
if image_tag_prefix is not None:
sebs_client.config.image_tag_prefix = image_tag_prefix
experiment_config = sebs_client.get_experiment_config(config["experiments"])
update_nested_dict(config, ["experiments", "benchmark"], benchmark)
benchmark_obj = sebs_client.get_benchmark(
benchmark,
deployment_client,
experiment_config,
logging_filename=logging_filename,
)
if memory is not None:
benchmark_obj.benchmark_config.memory = memory
if timeout is not None:
benchmark_obj.benchmark_config.timeout = timeout
func = deployment_client.get_function(
benchmark_obj,
function_name if function_name else deployment_client.default_function_name(benchmark_obj),
)
storage = deployment_client.get_storage(replace_existing=experiment_config.update_storage)
input_config = benchmark_obj.prepare_input(storage=storage, size=benchmark_input_size)
result = sebs.experiments.ExperimentResult(experiment_config, deployment_client.config)
result.begin()
trigger_type = Trigger.TriggerType.get(trigger)
triggers = func.triggers(trigger_type)
if len(triggers) == 0:
trigger = deployment_client.create_trigger(func, trigger_type)
else:
trigger = triggers[0]
for i in range(repetitions):
sebs_client.logging.info(f"Beginning repetition {i+1}/{repetitions}")
ret = trigger.sync_invoke(input_config)
if ret.stats.failure:
sebs_client.logging.info(f"Failure on repetition {i+1}/{repetitions}")
# deployment_client.get_invocation_error(
# function_name=func.name, start_time=start_time, end_time=end_time
# )
result.add_invocation(func, ret)
result.end()
result_file = os.path.join(output_dir, "experiments.json")
with open(result_file, "w") as out_f:
out_f.write(sebs.utils.serialize(result))
sebs_client.logging.info("Save results to {}".format(os.path.abspath(result_file)))
@benchmark.command()
@common_params
def process(**kwargs):
(
config,
output_dir,
logging_filename,
sebs_client,
deployment_client,
) = parse_common_params(**kwargs)
result_file = os.path.join(output_dir, "experiments.json")
sebs_client.logging.info("Load results from {}".format(os.path.abspath(result_file)))
with open(result_file, "r") as in_f:
config = json.load(in_f)
experiments = sebs.experiments.ExperimentResult.deserialize(
config,
sebs_client.cache_client,
sebs_client.generate_logging_handlers(logging_filename),
)
for func in experiments.functions():
deployment_client.download_metrics(
func, *experiments.times(), experiments.invocations(func), experiments.metrics(func)
)
output_file = os.path.join(output_dir, "results.json")
with open(output_file, "w") as out_f:
out_f.write(sebs.utils.serialize(experiments))
sebs_client.logging.info("Save results to {}".format(output_file))
@benchmark.command()
@click.argument(
"benchmark-input-size", type=click.Choice(["test", "small", "large"])
) # help="Input test size")
@click.option(
"--benchmark-name",
default=None,
type=str,
help="Run only the selected benchmark.",
)
@common_params
@click.option(
"--cache",
default=os.path.join(os.path.curdir, "regression-cache"),
help="Location of experiments cache.",
)
@click.option(
"--output-dir",
default=os.path.join(os.path.curdir, "regression-output"),
help="Output directory for results.",
)
def regression(benchmark_input_size, benchmark_name, **kwargs):
# for regression, deployment client is initialized locally
# disable default initialization
(config, output_dir, logging_filename, sebs_client, _) = parse_common_params(
initialize_deployment=False, **kwargs
)
regression_suite(
sebs_client,
config["experiments"],
set((config["deployment"]["name"],)),
config,
benchmark_name,
)
@cli.group()
def storage():
pass
@storage.command("start")
@click.argument("storage", type=click.Choice([StorageTypes.MINIO]))
@click.option("--output-json", type=click.Path(dir_okay=False, writable=True), default=None)
@click.option("--port", type=int, default=9000)
def storage_start(storage, output_json, port):
import docker
sebs.utils.global_logging()
storage_type = sebs.SeBS.get_storage_implementation(StorageTypes(storage))
storage_config, storage_resources = sebs.SeBS.get_storage_config_implementation(
StorageTypes(storage)
)
config = storage_config()
resources = storage_resources()
storage_instance = storage_type(docker.from_env(), None, resources, True)
logging.info(f"Starting storage {str(storage)} on port {port}.")
storage_instance.start(port)
if output_json:
logging.info(f"Writing storage configuration to {output_json}.")
with open(output_json, "w") as f:
json.dump(storage_instance.serialize(), fp=f, indent=2)
else:
logging.info("Writing storage configuration to stdout.")
logging.info(json.dumps(storage_instance.serialize(), indent=2))
@storage.command("stop")
@click.argument("input-json", type=click.Path(exists=True, dir_okay=False, readable=True))
def storage_stop(input_json):
sebs.utils.global_logging()
with open(input_json, "r") as f:
cfg = json.load(f)
storage_type = cfg["type"]
storage_cfg, storage_resources = sebs.SeBS.get_storage_config_implementation(storage_type)
config = storage_cfg.deserialize(cfg)
if "resources" in cfg:
resources = storage_resources.deserialize(cfg["resources"])
else:
resources = storage_resources()
logging.info(f"Stopping storage deployment of {storage_type}.")
storage = sebs.SeBS.get_storage_implementation(storage_type).deserialize(
config, None, resources
)
storage.stop()
logging.info(f"Stopped storage deployment of {storage_type}.")
@cli.group()
def local():
pass
@local.command()
@click.argument("benchmark", type=str)
@click.argument("benchmark-input-size", type=click.Choice(["test", "small", "large"]))
@click.argument("output", type=str)
@click.option("--deployments", default=1, type=int, help="Number of deployed containers.")
@click.option("--storage-configuration", type=str, help="JSON configuration of deployed storage.")
@click.option(
"--measure-interval",
type=int,
default=-1,
help="Interval duration between memory measurements in ms.",
)
@click.option(
"--remove-containers/--no-remove-containers",
default=True,
help="Remove containers after stopping.",
)
@simplified_common_params
def start(
benchmark,
benchmark_input_size,
output,
deployments,
storage_configuration,
measure_interval,
remove_containers,
**kwargs,
):
"""
Start a given number of function instances and a storage instance.
"""
(config, output_dir, logging_filename, sebs_client, deployment_client) = parse_common_params(
update_code=False, update_storage=False,
deployment="local", storage_configuration=storage_configuration, **kwargs
)
deployment_client = cast(sebs.local.Local, deployment_client)
deployment_client.remove_containers = remove_containers
result = sebs.local.Deployment()
result.measurement_file = deployment_client.start_measurements(measure_interval)
experiment_config = sebs_client.get_experiment_config(config["experiments"])
benchmark_obj = sebs_client.get_benchmark(
benchmark,
deployment_client,
experiment_config,
logging_filename=logging_filename,
)
storage = deployment_client.get_storage(replace_existing=experiment_config.update_storage)
result.set_storage(storage)
input_config = benchmark_obj.prepare_input(storage=storage, size=benchmark_input_size)
result.add_input(input_config)
for i in range(deployments):
func = deployment_client.get_function(
benchmark_obj, deployment_client.default_function_name(benchmark_obj)
)
result.add_function(func)
# Disable shutdown of storage only after we succed
# Otherwise we want to clean up as much as possible
deployment_client.shutdown_storage = False
result.serialize(output)
sebs_client.logging.info(f"Save results to {os.path.abspath(output)}")
@local.command()
@click.argument("input-json", type=str)
@click.argument("output-json", type=str, default="memory_stats.json")
# @simplified_common_params
def stop(input_json, output_json, **kwargs):
"""
Stop function and storage containers.
"""
sebs.utils.global_logging()
logging.info(f"Stopping deployment from {os.path.abspath(input_json)}")
(config, output_dir, logging_filename, sebs_client, deployment_client) = parse_common_params(
update_code=False, update_storage=False,
deployment="local", **kwargs
)
deployment_client.res
deployment = sebs.local.Deployment.deserialize(input_json, None)
deployment.shutdown(output_json)
logging.info(f"Stopped deployment from {os.path.abspath(input_json)}")
@cli.group()
def experiment():
pass
@experiment.command("invoke")
@click.argument("experiment", type=str) # , help="Benchmark to be launched.")
@common_params
def experiment_invoke(experiment, **kwargs):
(
config,
output_dir,
logging_filename,
sebs_client,
deployment_client,
) = parse_common_params(**kwargs)
experiment = sebs_client.get_experiment(experiment, config["experiments"])
experiment.prepare(sebs_client, deployment_client)
experiment.run()
@experiment.command("process")
@click.argument("experiment", type=str) # , help="Benchmark to be launched.")
@click.option("--extend-time-interval", type=int, default=-1) # , help="Benchmark to be launched.")
@common_params
def experiment_process(experiment, extend_time_interval, **kwargs):
(
config,
output_dir,
logging_filename,
sebs_client,
deployment_client,
) = parse_common_params(**kwargs)
experiment = sebs_client.get_experiment(experiment, config["experiments"])
experiment.process(
sebs_client, deployment_client, output_dir, logging_filename, extend_time_interval
)
@cli.group()
def resources():
pass
@resources.command("list")
@click.argument("resource", type=click.Choice(["buckets", "resource-groups"]))
@common_params
def resources_list(resource, **kwargs):
(
config,
output_dir,
logging_filename,
sebs_client,
deployment_client,
) = parse_common_params(**kwargs)
if resource == "buckets":
storage_client = deployment_client.get_storage(False)
buckets = storage_client.list_buckets()
sebs_client.logging.info("Storage buckets:")
for idx, bucket in enumerate(buckets):
sebs_client.logging.info(f"({idx}) {bucket}")
elif resource == "resource-groups":
if deployment_client.name() != "azure":
sebs_client.logging.error("Resource groups are only supported on Azure!")
return
groups = deployment_client.config.resources.list_resource_groups(
deployment_client.cli_instance
)
sebs_client.logging.info("Resource grup:")
for idx, bucket in enumerate(groups):
sebs_client.logging.info(f"({idx}) {bucket}")
@resources.command("remove")
@click.argument("resource", type=click.Choice(["buckets", "resource-groups"]))
@click.argument("prefix", type=str)
@click.option("--wait/--no-wait", type=bool, default=True, help="Wait for completion of removal.")
@click.option(
"--dry-run/--no-dry-run",
type=bool,
default=False,
help="Simulate run without actual deletions.",
)
@common_params
def resources_remove(resource, prefix, wait, dry_run, **kwargs):
(
config,
output_dir,
logging_filename,
sebs_client,
deployment_client,
) = parse_common_params(**kwargs)
storage_client = deployment_client.get_storage(False)
if resource == "storage":
buckets = storage_client.list_buckets()
for idx, bucket in enumerate(buckets):
if len(prefix) > 0 and not bucket.startswith(prefix):
continue
sebs_client.logging.info(f"Removing bucket: {bucket}")
if not dry_run:
storage_client.clean_bucket(bucket)
storage_client.remove_bucket(bucket)
elif resource == "resource-groups":
if deployment_client.name() != "azure":
sebs_client.logging.error("Resource groups are only supported on Azure!")
return
groups = deployment_client.config.resources.list_resource_groups(
deployment_client.cli_instance
)
for idx, group in enumerate(groups):
if len(prefix) > 0 and not group.startswith(prefix):
continue
sebs_client.logging.info(f"Removing resource group: {group}")
deployment_client.config.resources.delete_resource_group(
deployment_client.cli_instance, group, wait
)
if __name__ == "__main__":
cli()