import os
import sys
from colorama import init, Fore, Style
init()
if len(sys.argv) < 3:
print("usage: cmp.py <before-benchmark> <after-benchmark>")
sys.exit(1)
run = [{}, {}]
for i in range(2):
raw = open(sys.argv[i + 1], 'r').read().split('\n')
raw = raw[raw.index("benchmarking...")+1:]
tests = {}
for l in raw:
l = l.strip().split()
if len(l) == 0:
continue
if l[0] == "TOTAL:":
run[i]["total"] = float(l[1])
continue
if l[0] == "UNCERTAINTY:":
run[i]["diff"] = float(l[1])
continue
name = l[0]
if l[1] != "mean:" or l[3] != "(+/-":
print("unexpected input")
sys.exit(1)
time = float(l[2])
diff = float(l[4][:-1])
tests[name] = { "time": time, "diff": diff }
run[i]["tests"] = tests
for name, t0 in run[0]["tests"].items():
t1 = run[1]["tests"][name]
t1time = t1["time"]
t1diff = t1["diff"]
t0time = t0["time"]
t0diff = t0["diff"]
if abs(t1time - t0time) < t0diff or abs(t1time - t0time) < t1diff:
print("%20s mean: %f (%+f) %+.2f %% (within uncertainty)" % (name, t1time, t1time - t0time, (t1time - t0time) / t0time * 100.0))
else:
if t0time > t1time:
print(Fore.GREEN, end='')
else:
print(Fore.RED, end='')
print("%20s mean: %f (%+f) %+.2f %%" % (name, t1time, t1time - t0time, (t1time - t0time) / t0time * 100.0))
print(Fore.RESET, end='')
tot0 = run[0]["total"]
tot1 = run[1]["total"]
tot0diff = run[0]["diff"]
tot1diff = run[1]["diff"]
if abs(tot0 - tot1) < tot0diff or abs(tot0 - tot1) < tot1diff:
print("TOTAL: %f (%+f) %+.2f %% (within uncertainty)" % (tot1, tot1 - tot0, (tot1 - tot0) / tot0 * 100.0))
else:
if tot0 > tot1:
print(Fore.GREEN, end='')
else:
print(Fore.RED, end='')
print("TOTAL: %f (%+f) %+.2f %%" % (tot1, tot1 - tot0, (tot1 - tot0) / tot0 * 100.0))
print(Fore.RESET, end='')