1#!/usr/bin/env python3
2
3import json
4import subprocess
5import re
6
7def Run(cmd):
8  subprocess.check_call(cmd, shell=True)
9
10def RunAgainstBranch(branch, outfile, runs=12):
11  tmpfile = "/tmp/bench-output.json"
12  Run("rm -rf {}".format(tmpfile))
13  Run("git checkout {}".format(branch))
14  Run("bazel build -c opt :benchmark")
15
16  Run("./bazel-bin/benchmark --benchmark_out_format=json --benchmark_out={} --benchmark_repetitions={}".format(tmpfile, runs))
17
18  with open(tmpfile) as f:
19    bench_json = json.load(f)
20
21  with open(outfile, "w") as f:
22    for run in bench_json["benchmarks"]:
23      name = re.sub(r'^BM_', 'Benchmark', run["name"])
24      if name.endswith("_mean") or name.endswith("_median") or name.endswith("_stddev"):
25        continue
26      values = (name, run["iterations"], run["cpu_time"])
27      print("{} {} {} ns/op".format(*values), file=f)
28
29RunAgainstBranch("master", "/tmp/old.txt")
30RunAgainstBranch("decoder", "/tmp/new.txt")
31
32Run("~/go/bin/benchstat /tmp/old.txt /tmp/new.txt")
33