-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharness.py
executable file
·1298 lines (1059 loc) · 43.6 KB
/
harness.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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import argparse
import config
import dataclasses
import datetime
import json
import multiprocessing
import os
import shlex
import subprocess
import sys
import traceback
import typing
from dataclasses import dataclass
from pathlib import Path
from typing import List, Tuple, Optional
WASI_SDK_BASE_PATH = Path("tools/wasi-sdk")
REPOS_PATH = Path("tools/external")
SPEC_REPO = "spec"
BINARYEN_REPO = "binaryen"
MIMALLOC_REPO = "mimalloc"
WASMTIME_REPO1 = "wasmtime1"
WASMTIME_REPO2 = "wasmtime2"
class HarnessError(Exception):
def __init__(self, msg):
super().__init__(msg)
def check(condition, msg):
if not condition:
raise HarnessError(msg)
logLevel = 0
def logProcessOutput(msg, sep=None):
now = datetime.datetime.now()
if logLevel > 1:
print(f"{now}: ", msg, sep=sep)
def logMsg(msg, sep=None):
now = datetime.datetime.now()
if logLevel > 0:
print(f"{now}: ", msg, sep=sep)
@dataclass
class Suite:
"""A suite is a collection of logically related benchmarks.
It is uniquely identified by the path to a subfolder within the benchfx
directory.
Each suite's benchmarks will be compared against each other when using the
'run' subcommand of the benchnmark harness.
"""
path: str
benchmarks: List["Benchmark"]
@dataclass
class Benchmark:
"""Definition of a benchmark.
Each benchmark within a `Suite` must have a unique `name`.
The subclasses of `Benchmark` represent how a benchmark is actually built
and run, by overriding the `prepare` method.
"""
name: str
def prepare(
self,
suite: Suite,
output_dir: Path,
config: "Config",
mimalloc: "Mimalloc",
wasi_sdk: "WasiSdk",
reference_interpreter: "ReferenceInterpreter",
binaryen: "Binaryen",
wasmtime: "Wasmtime",
) -> str:
"""Perform all necessary preparation work, then return a shell command for actually running the benchmark.
Some benchmarks may require some preparation work before actually being
executed, such as running a Makefile or performing any other kind of
compilation work specific to that particular benchmark.
All tools (binaryen, wasmtime, etc) are already compiled at this point
and may be used.
For each such "kind" of benchmark, we have a separate subclass of
`Benchmark`, whose main differentiation is their implementation of this
method.
Each call to `prepare` receives a fresh, empty `output_dir`, where built
artifacts (compiled wasm or cwasm files, etc) may be stored.
The function then returns a single shell command. This shell command
denotes how to run the benchmark, and will be fed into hyperfine
verbatim.
"""
raise HarnessError("Override me!")
def pseudoPath(self, enclosing_suite) -> Path:
"""Returns a "pseudo-path" for this benchmark for filtering purposes.
It is obtained by using the benchmark's name as a file name, appearing
in the suite's directory,
"""
return Path(enclosing_suite.path) / self.name
def matchesAnyFilter(self, enclosing_suite: Suite, filter_globs: List[str]) -> bool:
pseudo_path = self.pseudoPath(enclosing_suite)
return any(map(pseudo_path.match, filter_globs))
class MakeWasm(Benchmark):
"""Benchmarks that use the make.generic.config Makefile
Attributes
----------
file : str
The file in the suite folder to call the Makefile on, without its
extension
invoke : Optional[str]
If given, name of function in final module to invoke
flags: Optional[List[Tuple[str,str]]]
List of extra flags and their values to pass to make
"""
def __init__(
self,
file: str,
name: Optional[str] = None,
invoke: Optional[str] = None,
flags: Optional[List[Tuple[str, str]]] = None,
):
self.file = file
self.invoke = invoke
self.flags = flags or []
super().__init__(name or file)
def prepare(
self,
suite,
output_dir: Path,
config,
mimalloc,
wasi_sdk,
reference_interpreter,
binaryen,
wasmtime,
):
wasm_file = self.file + ".wasm"
wasm_make_target = Path("out") / wasm_file
cwasm_file = self.file + ".cwasm"
suite_path = Path(suite.path)
interpreter = Path(reference_interpreter.executablePath()).absolute()
wasi_cc = wasi_sdk.clangPath().absolute()
wasm_merge = binaryen.wasmMergeExecutablePath().absolute()
wasm_opt = binaryen.wasmOptExecutablePath().absolute()
runCheck("make clean", cwd=suite_path)
flag_args = list(map(lambda x: f"{x[0]}={x[1]}", self.flags))
runCheck(
["make", str(wasm_make_target)]
+ [
f"WASICC={wasi_cc}",
f"WASM_INTERP={interpreter}",
f"WASM_MERGE={wasm_merge}",
f"WASM_OPT={wasm_opt}",
]
+ flag_args,
cwd=suite_path,
)
wasmtime.compileWasm(suite_path / wasm_make_target, output_dir / cwasm_file)
run_command = wasmtime.shellCommandCwasmRun(output_dir / cwasm_file)
return (
mimalloc.addToShellCommmand(run_command)
if config.use_mimalloc
else run_command
)
class Wat(Benchmark):
"""Benchmarks that simply run a dedicated function in a wat file
Attributes
----------
file : str
The file in the suite folder to run, without its extension
invoke : Optional[str]
If given, name of function in final module to invoke
"""
def __init__(self, file, name=None, invoke=None):
self.file: str = file
self.invoke: Optional[str] = invoke
super().__init__(name or file)
def prepare(
self,
suite,
output_dir: Path,
config,
mimalloc,
wasi_sdk,
reference_interpreter,
binaryen,
wasmtime,
):
suite_path = Path(suite.path)
wat_path = suite_path / (self.file + ".wat")
cwasm_path = output_dir / (self.file + ".cwasm")
wasmtime.compileWasm(wat_path, cwasm_path)
run_command = wasmtime.shellCommandCwasmRun(
cwasm_path, invoke_function=self.invoke
)
return (
mimalloc.addToShellCommmand(run_command)
if config.use_mimalloc
else run_command
)
def run(cmd: str | List[str], cwd=None) -> subprocess.CompletedProcess:
if isinstance(cmd, list):
command = shlex.join(cmd)
else:
command = cmd
cwd_msg = f" in directory {cwd}" if cwd is not None else ""
logMsg(f"Running command{cwd_msg}:\n{command}")
res = subprocess.run(command, cwd=cwd, capture_output=True, shell=True, text=True)
if res.stdout:
logProcessOutput("STDOUT:\n" + res.stdout, sep="")
if res.stderr:
logProcessOutput("STDERR:\n" + res.stderr, sep="")
return res
# Like run, but checks that the command finished with non-zero exit code.
def runCheck(cmd, msg=None, cwd=None):
result = run(cmd, cwd)
msg = msg or f"Running {cmd} in {cwd or os.getcwd()} failed"
check(
result.returncode == 0,
msg
+ f"\nDetails:\nFailed command: {cmd}\nStdout:\n{result.stdout}\nStderr:\n{result.stderr}",
)
return result
@dataclass
class Binaryen:
path: Path
def _build_path(self):
return self.path / "build"
def build(self):
cpus = multiprocessing.cpu_count()
runCheck(
f"mkdir -p build",
cwd=self.path,
)
# TODO(frank-emrich) Build with clang until the following is fixed:
# https://github.com/WebAssembly/binaryen/issues/6779
runCheck(
"cmake -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -S .. -B .",
msg="cmake for binaryen failed",
cwd=self._build_path(),
)
runCheck(
f"make -j {cpus}", msg="building binaryen failed", cwd=self._build_path()
)
def wasmMergeExecutablePath(self) -> Path:
return self._build_path() / "bin" / "wasm-merge"
def wasmOptExecutablePath(self) -> Path:
return self._build_path() / "bin" / "wasm-opt"
class Mimalloc:
def __init__(self, path: Path):
self.path = path
def build(self):
cpus = multiprocessing.cpu_count()
runCheck("mkdir -p out", cwd=self.path)
out_dir = self.path / "out"
runCheck("cmake ..", msg="cmake for mimalloc failed", cwd=out_dir)
runCheck(f"make -j {cpus}", msg="building mimalloc failed", cwd=out_dir)
def libmimallocPath(self) -> Path:
return self.path / "out" / "libmimalloc.so"
def addToShellCommmand(self, shell_command: str):
"Given a shell command, extends it with an appropriate LD_PRELOAD clause to use mimalloc"
escaped_path = shlex.quote(str(self.libmimallocPath()))
return f"LD_PRELOAD={escaped_path} {shell_command}"
@dataclass
class ReferenceInterpreter:
path: Path
def executablePath(self) -> Path:
return self.path / "wasm"
def build(self):
runCheck("make", "Failed to build reference interpreter", self.path)
def compile(self, input_path: Path, output_path: Path):
wasm = str(self.executablePath().absolute())
input_absolute = str(input_path.absolute())
output_absolute = str(output_path.absolute())
runCheck(f"{wasm} -d '{input_absolute}' -o '{output_absolute}'", self.path)
@dataclass
class Config:
"""Configuration of various tools for each benchmark run.
This serves two purposes:
1. A more structured representation of the most important CLI config
options, showing how they should be parsed and getting their defaults.
2. For those options that exist twice for 'compare-revs' command, implements
extracting one `Config` object for each of the two revisions.
"""
use_mimalloc: bool = True
# Wasmtime specific:
wasmtime_cargo_build_args: Optional[List[str]] = None
wasmtime_release_build: bool = True
wasmtime_compile_args: Optional[List[str]] = None
wasmtime_run_args: Optional[List[str]] = None
@staticmethod
def fromCliArgs(cli_args: argparse.Namespace, revision_qualifier=None) -> "Config":
prefix = revision_qualifier + "_" if revision_qualifier else ""
def parseYN(attr_name: str, attr_value: str) -> bool:
match attr_value.lower():
case "y":
return True
case "n":
return False
case _:
raise HarnessError(
"Expected 'y' or 'n' for {attr_name}, got {attr_value} instead"
)
def splitMultiArgumentString(_attr_name: str, attr_value: str):
return shlex.split(attr_value)
prop_parsers = {
("use_mimalloc", parseYN),
("wasmtime_release_build", parseYN),
("wasmtime_cargo_build_args", splitMultiArgumentString),
("wasmtime_compile_args", splitMultiArgumentString),
(
"wasmtime_run_args",
splitMultiArgumentString,
),
}
config = Config()
for prop_name, parser in prop_parsers:
qualified_attr_name = prefix + prop_name
attr_val = getattr(cli_args, qualified_attr_name, None)
val = (
parser(qualified_attr_name, attr_val) if attr_val is not None else None
)
setattr(config, prop_name, val)
return config
def _getOrDefault(self, prop, default):
return prop if prop is not None else default
def getWasmtimeCargoBuildArgsOrDefault(self) -> List[str]:
return self._getOrDefault(
self.wasmtime_cargo_build_args, config.WASMTIME_CARGO_BUILD_ARGS
)
def getWasmtimeCompileArgsOrDefault(self) -> List[str]:
return self._getOrDefault(
self.wasmtime_compile_args, config.WASMTIME_COMPILE_ARGS
)
def getWasmtimeRunArgsOrDefault(self) -> List[str]:
return self._getOrDefault(self.wasmtime_run_args, config.WASMTIME_RUN_ARGS)
class Wasmtime:
def __init__(self, path: Path, configuration: Config):
self.path = path
self.config = configuration
def executablePath(self) -> Path:
if self.config.wasmtime_release_build:
return self.path / "target/release/wasmtime"
else:
return self.path / "target/debug/wasmtime"
def build(self):
# For the tim
release = ["--release"] if self.config.wasmtime_release_build else []
if self.config.wasmtime_cargo_build_args is not None:
logMsg(
"Overriding cargo builds args with "
+ str(self.config.wasmtime_cargo_build_args)
)
cargo_build_args = self.config.getWasmtimeCargoBuildArgsOrDefault()
check(
not "--release" in cargo_build_args,
"Do not use wasmtime-cargo-build-args to influnece release vs debug build, consider using wasmtime-release-build option",
)
runCheck(
["cargo", "build"] + release + cargo_build_args,
"Failed to build wasmtime",
self.path,
)
def compileWasm(self, input_wasm_path: Path, output_cwasm_path: Path):
wasmtime = str(self.executablePath())
command = "compile"
if self.config.wasmtime_compile_args is not None:
logMsg(
"Overriding wasmtime compile args with "
+ str(self.config.wasmtime_compile_args)
)
wasmtime_compile_args = self.config.getWasmtimeCompileArgsOrDefault()
runCheck(
[wasmtime, "compile"]
+ wasmtime_compile_args
+ [
f"--output={output_cwasm_path.absolute()}",
str(input_wasm_path.absolute()),
],
f"Failed to compile {input_wasm_path} to {output_cwasm_path}",
)
def shellCommandCwasmRun(
self,
cwasm_path: Path,
invoke_function=None,
):
wasmtime = str(self.executablePath())
command = "run"
if self.config.wasmtime_run_args is not None:
logMsg(
"Overriding wasmtime run args with "
+ str(self.config.wasmtime_run_args)
)
wasmtime_run_args = self.config.getWasmtimeRunArgsOrDefault()
invoke_args = []
if invoke_function:
invoke_args += [f"--invoke={invoke_function}"]
cmd = (
[wasmtime, "run", "--allow-precompiled"]
+ wasmtime_run_args
+ invoke_args
+ [str(cwasm_path.absolute())]
)
return shlex.join(cmd)
class Hyperfine:
@staticmethod
# `commands` contains tuples `(shell_command, desc)`, where `desc` is a human-readable description
def run(
commands: List[Tuple[str, str]],
print_stdout=False,
warmup_count=3,
json_export_path=None,
):
args = ["hyperfine", f"--warmup={warmup_count}"]
if json_export_path:
args += [f"--export-json={json_export_path}"]
shell_commands = []
command_names = []
for c in commands:
command_names.append("--command-name")
command_names.append(c[1])
shell_commands.append(c[0])
logMsg("Hyperfine will compare shell commands:\n" + "\n".join(shell_commands))
result = runCheck(args + command_names + shell_commands)
if print_stdout:
global logLevel
# Don't print if we are already printing all process output anyway
if logLevel <= 1:
print(result.stdout)
@dataclass
class WasiSdk:
path: Path
wasi_version: str
RELEASE_DOWNLOAD_FOLDER_TEMPLATE = "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-{wasi_version}/"
RELEASE_ARCHIVE_NAME_TEMPLATE = "wasi-sdk-{wasi_version_full}-linux.tar.gz"
@staticmethod
def forVersion(wasi_version: str, base_folder: Path) -> "WasiSdk":
wasi_version_full = WasiSdk._fullVersion(wasi_version)
untared_folder = f"wasi-sdk-{wasi_version_full}"
return WasiSdk(base_folder / untared_folder, wasi_version)
@staticmethod
def _fullVersion(wasi_version: str):
return f"{wasi_version}.0"
@staticmethod
def _release_tar_file_name(wasi_version: str) -> str:
wasi_version_full = WasiSdk._fullVersion(wasi_version)
return WasiSdk.RELEASE_ARCHIVE_NAME_TEMPLATE.format(
wasi_version=wasi_version, wasi_version_full=wasi_version_full
)
@staticmethod
def download(wasi_version, base_folder: Path):
wasi_version_full = WasiSdk._fullVersion(wasi_version)
base_url = folder_url = WasiSdk.RELEASE_DOWNLOAD_FOLDER_TEMPLATE.format(
wasi_version=wasi_version, wasi_version_full=wasi_version_full
)
file_name = WasiSdk._release_tar_file_name(wasi_version)
full_url = folder_url + file_name
runCheck(f"mkdir -p '{base_folder}'")
runCheck(
f"wget '{full_url}'",
cwd=base_folder,
msg="Failed to download WASI SDK from {full_url}",
)
runCheck(f"tar xvf '{file_name}'", cwd=base_folder)
def clangPath(self) -> Path:
return self.path / "bin" / "clang"
@staticmethod
def isVersionInstalled(wasi_version: str, base_folder: Path) -> bool:
return WasiSdk.forVersion(wasi_version, base_folder).clangPath().exists()
# Helper class for working at a git repo (or a working tree of a git repo) at a given path.
# This is mostly a wrapper around GitPyhton's git.Repo type
class GitRepo:
def __init__(self, path: Path):
check(
path.exists(),
f"Expecting git repo at {path}, but the folder does not exist",
)
check(
GitRepo.isRootOfRepoOrWorktree(path),
f"{path} is not the root of a git repository (or a worktree of a repository)",
)
self.path = path
@staticmethod
def initWithRemotes(path: Path, github_remotes: List[Tuple[str, str]]) -> "GitRepo":
check(
not path.exists(),
f"Asked to init a git repo at {path}, but the folder exists",
)
runCheck(f"mkdir -p '{path}'")
runCheck(f"git init '{path}'")
repo = GitRepo(path)
for user_name, repo_name in github_remotes:
github_repo_url = f"https://github.com/{user_name}/{repo_name}"
repo._git(f"remote add '{user_name}' '{github_repo_url}'")
repo.fetchAll()
return repo
def hasRev(self, rev: str) -> bool:
rev = rev + "^{commit}"
res = run(f"git rev-parse --verify '{rev}'", cwd=self.path)
return res.returncode == 0
@staticmethod
def isRootOfRepoOrWorktree(path):
res = run("git rev-parse --show-toplevel", cwd=path)
if res.returncode != 0:
return False
toplevel_path = res.stdout.strip()
return Path(toplevel_path).absolute() == Path(path).absolute()
# This uses run_check, use only for git commands that are not allowed to fail
def _git(self, args):
return runCheck("git " + args, cwd=self.path)
def isDirty(self, allow_untracked=True):
untracked_mode = "no" if allow_untracked else "normal"
res = self._git(f"status --untracked-files={untracked_mode} --porcelain")
# the --porcelain option makes the output stable, where no output means clean repository
return res.stdout.strip() != ""
def checkout(self, revision):
check(
not self.isDirty(allow_untracked=False),
f"Cannot checkout git repo at {self.path} to {revision} because it is dirty",
)
check(
self.hasRev(revision),
f"Cannot checkout {revision} in {self.path}: That revision is unknown. Consider running 'setup' subcommand with '--fetch-all' flag.",
)
self._git(f"switch --detach {revision}")
self._git("submodule update --init --recursive")
# We are very strict about changed and untracked files, to avoid
# subsequent failures: We required above that the repo is clean, not
# even having untracked files. After checking out, we then remove all
# newly untracked files, too.
# NB: Need to provide --force twice in order to delete non-empty directories
self._git("clean -d --force --force")
def addWorktree(self, newWorktreePath: Path):
check(
not newWorktreePath.exists(),
"Cannot create new git worktree at {newWorktreePath}, path exists",
)
self._git(f"worktree add --detach '{str(newWorktreePath.absolute())}'")
def fetchAll(self):
self._git("fetch --all")
def prepareCommonTools() -> Tuple[WasiSdk, Mimalloc, ReferenceInterpreter, Binaryen]:
"Prepares almost all the external tools EXCEPT wasmtime, which we compile elsewhere."
# We should have already checked that the WASI SDK binaries have been
# downloaded
check(
WasiSdk.isVersionInstalled(config.WASI_SDK_VERSION, WASI_SDK_BASE_PATH),
"Wasi SDK missing",
)
wasi_sdk = WasiSdk.forVersion(config.WASI_SDK_VERSION, WASI_SDK_BASE_PATH)
# Mimalloc setup
mimalloc_repo_path = REPOS_PATH / MIMALLOC_REPO
mimalloc_repo = GitRepo(mimalloc_repo_path)
mimalloc_repo.checkout(config.MIMALLOC_REVISION)
mimalloc = Mimalloc(mimalloc_repo_path)
mimalloc.build()
# Reference interpreter setup
spec_repo_path = REPOS_PATH / SPEC_REPO
spec_repo = GitRepo(spec_repo_path)
spec_repo.checkout(config.SPEC_REVISION)
interpreter_path = Path(os.path.join(spec_repo_path, "interpreter"))
interpreter: ReferenceInterpreter = ReferenceInterpreter(interpreter_path)
interpreter.build()
# Binaryen setup
binaryen_repo_path = REPOS_PATH / BINARYEN_REPO
binaryen_repo = GitRepo(binaryen_repo_path)
binaryen_repo.checkout(config.BINARYEN_REVISION)
binaryen = Binaryen(binaryen_repo_path)
binaryen.build()
return (wasi_sdk, mimalloc, interpreter, binaryen)
def addSharedArgsToSubparser(parser):
parser.add_argument(
"--filter",
help="Only run benchmarks that match this glob pattern",
action="append",
)
parser.add_argument(
"--allow-dirty",
help="Allows the benchfx repo to be dirty when running benchmarks",
action="store_true",
default=False,
)
parser.add_argument(
"--prepare-only",
help="""Perform all preparation steps (for external tools and benchmarks
themselves), but do not actually run benchmarks. Instead, print the shell
commands that would be compare against each other.""",
action="store_true",
default=False,
)
def addRevisionSpecificArgsToSubparser(
subparser,
revision_qualifier: Optional[str] = None,
desc: Optional[str] = None,
):
"""Adds a set of arguments to the CLI parser that exist once for the 'run' subcommand but twice for 'compare-revs'.
In the former case, `revision_qualifier` and `desc` should be None.
Otherwise, `revision_qualifier` is something like `rev1` and the final name
of each CLI argument will be prefixed with it. `desc` is then something like
"revision 1` and gets spliced into the help string of each argument.
"""
desc = f" for {desc}" if desc else ""
rev_prefix = revision_qualifier + "-" if revision_qualifier else ""
subparser.add_argument(
f"--{rev_prefix}wasmtime-cargo-build-args",
help=f"""Instead of config.WASMTIME_CARGO_BUILD_ARGS, use these
arguments building wasmtime{desc}.""",
action="store",
)
subparser.add_argument(
f"--{rev_prefix}wasmtime-run-args",
help=f"""Instead of config.WASMTIME_RUN_ARGS, use these arguments when
executing 'wasmtime run'{desc}. May be unsupported for certain
benchmarks.""",
action="store",
)
subparser.add_argument(
f"--{rev_prefix}wasmtime-compile-args",
help=f"""Instead of config.WASMTIME_RUN_ARGS, use these arguments when
executing 'wasmtime compile'{desc}. May be unsupported for certain
benchmarks.""",
action="store",
)
subparser.add_argument(
f"--{rev_prefix}wasmtime-release-build",
help=f"""Should we build wasmtime in release mode? (default: y)""",
action="store",
choices=["y", "n"],
default="y",
)
subparser.add_argument(
f"--{rev_prefix}use-mimalloc",
help=f"""Should we use mimalloc instead of standard system allocator
when running benchmarks{desc} (y/n, default: y)""",
action="store",
choices=["y", "n"],
default="y",
)
def checkBenchFxRepoClean():
benchfx_repo = GitRepo(Path(__file__).parent)
check(
not benchfx_repo.isDirty(),
"Cannot run benchmarks while benchfx repo is dirty. Consider passing --allow-dirty flag.",
)
def checkDependenciesPresent(need_second_wasmtime_repo):
def checkExternalToolsPresent():
tools = ["make", "cmake", "dune", "hyperfine", "cargo"]
for tool in tools:
runCheck(
f"command -v {tool}",
f"Could not find '{tool}' executable in $PATH, which this the benchmark harness requires",
)
def checkToolReposPresent(need_second_wasmtime_repo):
def checkRepo(repo_name, expected_root_commit):
path = REPOS_PATH / repo
check(
path.exists(),
f"Expecting {repo_name} repository at {str(path)}, but the folder does not exist. Consider running 'setup' subcommand.",
)
r = GitRepo(path)
check(
r.hasRev(expected_root_commit),
f"Repo {repo} at {path} exists, but does not contain commit {expected_root_commit}, which we expected to find there. Consider running 'setup' subcommand.",
)
repos = [SPEC_REPO, BINARYEN_REPO, MIMALLOC_REPO]
for repo in repos:
expected_root_commit, remotes = config.GITHUB_REPOS[repo]
checkRepo(repo, expected_root_commit)
wasmtime_repos = (
[WASMTIME_REPO1, WASMTIME_REPO2]
if need_second_wasmtime_repo
else [WASMTIME_REPO1]
)
wasmtime_root_commit = config.GITHUB_REPOS["wasmtime"][0]
for repo in wasmtime_repos:
checkRepo(repo, wasmtime_root_commit)
check(
WasiSdk.isVersionInstalled(config.WASI_SDK_VERSION, WASI_SDK_BASE_PATH),
f"WASI SDK version {config.WASI_SDK_VERSION} not installed. Consider running 'setup' subcommand.",
)
checkExternalToolsPresent()
checkToolReposPresent(need_second_wasmtime_repo)
# The run command, which just runs the benchmarks
class SubcommandRun:
"""Implements the 'run' subcommand.
Compares the benchmarks within each suite against each other.
"""
@staticmethod
def addSubparser(subparsers):
parser = subparsers.add_parser("run", help="runs benchmarks (used by default)")
parser.add_argument(
"wasmtime_rev",
help="Instead of config.WASMTIME_REVISION, use this wasmtime revision instead (optional)",
action="store",
default=config.WASMTIME_REVISION,
nargs="?",
)
addSharedArgsToSubparser(parser)
addRevisionSpecificArgsToSubparser(parser)
def execute(self, cli_args: argparse.Namespace):
if not cli_args.allow_dirty:
checkBenchFxRepoClean()
checkDependenciesPresent(need_second_wasmtime_repo=False)
(wasi_sdk, mimalloc, interpreter, binaryen) = prepareCommonTools()
configuration = Config.fromCliArgs(cli_args)
# Wasmtime setup
wasmtime_repo_path = REPOS_PATH / WASMTIME_REPO1
logMsg(f"wasmtime repo expected at {wasmtime_repo_path}")
wasmtime_repo = GitRepo(wasmtime_repo_path)
logMsg(f"wasmtime repo dirty? {wasmtime_repo.isDirty()}")
wasmtime_repo.checkout(cli_args.wasmtime_rev)
wasmtime = Wasmtime(wasmtime_repo_path, configuration)
wasmtime.build()
suite_shell_commands = {}
# Build benchmarks in each suite
for suite in config.BENCHMARK_SUITES:
suite_path = Path(suite.path)
check(
suite_path.exists(),
f"Found benchmark suite with non-existing path {suite_path}",
)
benchmark_commands: List[Tuple[str, str]] = []
for b in suite.benchmarks:
benchmark_filters = cli_args.filter
if benchmark_filters and not b.matchesAnyFilter(
suite, benchmark_filters
):
logMsg(
f"Skipping benchmark {b.pseudoPath(suite)} as it does not match filter"
)
continue
benchmark_output_dir = Path("out") / suite_path / b.name
benchmark_output_dir.mkdir(parents=True, exist_ok=True)
# create .wasm file for each benchmark
command = b.prepare(
suite,
benchmark_output_dir,
configuration,
mimalloc=mimalloc,
wasi_sdk=wasi_sdk,
reference_interpreter=interpreter,
binaryen=binaryen,
wasmtime=wasmtime,
)
benchmark_commands.append((command, b.name))
if benchmark_commands:
suite_shell_commands[suite.path] = benchmark_commands
#
# Perform actual benchmarking in each suite:
for suite_path, benchmark_commands in suite_shell_commands.items():
if cli_args.prepare_only:
print(f"Commands for suite {suite_path}:")
print("\n".join(map(lambda t: t[0], benchmark_commands)))
else:
Hyperfine.run(benchmark_commands, print_stdout=True)
class SubcommandCompareRevs:
"""Implements the 'compare-revs' subcommand.
For each suite s and each benchmark b in s, benchmarks b executed by first
wasmtime revision against b executed by second revision.
"""
@staticmethod
def addSubparser(subparsers):
parser = subparsers.add_parser(
"compare-revs",
help="""For each individual benchmark, compares runtime when using
first revision of wasmtime against second one""",
)
parser.add_argument(
"revision1", help="First Wasmtime revision to use in the comparison"
)
parser.add_argument(
"revision2", help="Second Wasmtime revision to use in the comparison"
)
parser.add_argument(
"--max-allowed-regression",
metavar="P",
type=int,
help="""Fail if any benchmark regresses by more than P percent.
For example, if P is 20, then the harness fails with an error if
there exists any benchmark B where B's runtime using revision 2
divided by B's runtime using revision 1 is greater than 1.2.
""",
)
addSharedArgsToSubparser(parser)
addRevisionSpecificArgsToSubparser(
parser, revision_qualifier="rev1", desc="revision 1"
)
addRevisionSpecificArgsToSubparser(
parser, revision_qualifier="rev2", desc="revision 2"
)
def prepare_wasmtime(self, repo_path: Path, revision: str, configuration: Config):
wasmtime_repo = GitRepo(repo_path)
logMsg(f"wasmtime repo dirty? {wasmtime_repo.isDirty()}")
wasmtime_repo.checkout(revision)
wasmtime = Wasmtime(repo_path, configuration)
wasmtime.build()
return wasmtime
def execute(self, cli_args: argparse.Namespace):
if not cli_args.allow_dirty:
checkBenchFxRepoClean()
check(
cli_args.max_allowed_regression is None
or cli_args.max_allowed_regression >= 0,
"--max-allowed-regression must be non-negative",
)
checkDependenciesPresent(need_second_wasmtime_repo=True)
(wasi_sdk, mimalloc, interpreter, binaryen) = prepareCommonTools()
rev1_config = Config.fromCliArgs(cli_args, revision_qualifier="rev1")
rev2_config = Config.fromCliArgs(cli_args, revision_qualifier="rev2")
# Wasmtime1 setup
wasmtime1_repo_path = REPOS_PATH / WASMTIME_REPO1
wasmtime1 = self.prepare_wasmtime(
wasmtime1_repo_path, cli_args.revision1, rev1_config
)
# Wasmtime2 setup
wasmtime2_repo_path = REPOS_PATH / WASMTIME_REPO2
wasmtime2 = self.prepare_wasmtime(
wasmtime2_repo_path, cli_args.revision2, rev2_config
)
suite_files = {}