Debian Codegraph @

js/src/tests/compare_bench.py

raw
1 #!/usr/bin/env python2.4
2 """usage: %progname candidate_path baseline_path
3 """
4
5 import json
6 import optparse
7 from operator import itemgetter
8
9
10 def avg(seq):
11 return sum(seq) / len(seq)
12
13
14 def compare(current, baseline):
15 percent_speedups = []
16 for key, current_result in iter(current.items()):
17 try:
18 baseline_result = baseline[key]
19 except KeyError:
20 print(key, "missing from baseline")
21 continue
22 val_getter = itemgetter("average_ms", "stddev_ms")
23 base_avg, base_stddev = val_getter(baseline_result)
24 current_avg, current_stddev = val_getter(current_result)
25 t_best = current_avg - current_stddev
26 t_worst = current_avg + current_stddev
27 base_t_best = base_avg - base_stddev
28 base_t_worst = base_avg + base_stddev
29 if t_worst < base_t_best:
30 # Worst takes less time (better) than baseline's best.
31 speedup = -((t_worst - base_t_best) / base_t_best) * 100
32 result = f"faster: {t_worst:6.2f}ms < baseline {base_t_best:6.2f}ms ({speedup:+6.2f}%)"
33 percent_speedups.append(speedup)
34 elif t_best > base_t_worst:
35 # Best takes more time (worse) than baseline's worst.
36 slowdown = -((t_best - base_t_worst) / base_t_worst) * 100
37 result = f"SLOWER: {t_best:6.2f}ms > baseline {base_t_worst:6.2f}ms ({slowdown:+6.2f}%) "
38 percent_speedups.append(slowdown)
39 else:
40 result = "Meh."
41 print(f"{key:30s}: {result}")
42 if percent_speedups:
43 print(f"Average speedup: {avg(percent_speedups):.2f}%")
44
45
46 def compare_immediate(current_map, baseline_path):
47 baseline_file = open(baseline_path)
48 baseline_map = json.load(baseline_file)
49 baseline_file.close()
50 compare(current_map, baseline_map)
51
52
53 def main(candidate_path, baseline_path):
54 candidate_file, baseline_file = open(candidate_path), open(baseline_path)
55 candidate = json.load(candidate_file)
56 baseline = json.load(baseline_file)
57 compare(candidate, baseline)
58 candidate_file.close()
59 baseline_file.close()
60
61
62 if __name__ == "__main__":
63 parser = optparse.OptionParser(usage=__doc__.strip())
64 options, args = parser.parse_args()
65 try:
66 candidate_path = args.pop(0)
67 except IndexError:
68 parser.error("A JSON filepath to compare against baseline is required")
69 try:
70 baseline_path = args.pop(0)
71 except IndexError:
72 parser.error("A JSON filepath for baseline is required")
73 main(candidate_path, baseline_path)