28 """Benchmarks the current working directory against a given baseline.
30 This script benchmarks both size and speed. Sample output:
41 @contextlib.contextmanager
43 tmpdir = tempfile.mkdtemp()
44 subprocess.run([
'git',
'worktree',
'add',
'-q',
'-d', tmpdir, commit], check=
True)
51 subprocess.run([
'git',
'worktree',
'remove', tmpdir], check=
True)
54 subprocess.check_call(cmd, shell=
True)
56 def Benchmark(outbase, bench_cpu=True, runs=12, fasttable=False):
57 tmpfile =
"/tmp/bench-output.json"
61 extra_args =
" --//:fasttable_enabled=true"
66 Run(
"CC=clang bazel build -c opt --copt=-march=native benchmarks:benchmark" + extra_args)
67 Run(
"./bazel-bin/benchmarks/benchmark --benchmark_out_format=json --benchmark_out={} --benchmark_repetitions={} --benchmark_min_time=0.05 --benchmark_enable_random_interleaving=true".
format(tmpfile, runs))
68 with open(tmpfile)
as f:
69 bench_json = json.load(f)
72 txt_filename = outbase +
".txt"
73 with open(txt_filename,
"w")
as f:
74 for run
in bench_json[
"benchmarks"]:
75 if run[
"run_type"] ==
"aggregate":
78 name = name.replace(
" ",
"")
79 name = re.sub(
r'^BM_',
'Benchmark', name)
80 values = (name, run[
"iterations"], run[
"cpu_time"])
81 print(
"{} {} {} ns/op".
format(*values), file=f)
82 Run(
"sort {} -o {} ".
format(txt_filename, txt_filename))
84 Run(
"CC=clang bazel build -c opt --copt=-g --copt=-march=native :conformance_upb"
86 Run(
"cp -f bazel-bin/conformance_upb {}.bin".
format(outbase))
94 baseline = sys.argv[1]
101 Benchmark(
"/tmp/new", bench_cpu, fasttable=fasttable)
105 Benchmark(
"/tmp/old", bench_cpu, fasttable=fasttable)
111 Run(
"~/go/bin/benchstat /tmp/old.txt /tmp/new.txt")
116 Run(
"objcopy --strip-debug /tmp/old.bin /tmp/old.bin.stripped")
117 Run(
"objcopy --strip-debug /tmp/new.bin /tmp/new.bin.stripped")
118 Run(
"~/code/bloaty/bloaty /tmp/new.bin.stripped -- /tmp/old.bin.stripped --debug-file=/tmp/old.bin --debug-file=/tmp/new.bin -d compileunits,symbols")