-
Notifications
You must be signed in to change notification settings - Fork 9
/
benchcmp-to-diff.py
executable file
·113 lines (94 loc) · 3.58 KB
/
benchcmp-to-diff.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
#!/usr/bin/env python3
import re
import sys
new_results = []
total_time = 0
line_count = 0
table_rows = []
max_name_len = 0
max_new_time_len = 0
max_old_time_len = 0
def transform_to_micro_seconds(string):
value = float(re.sub("±.*", "", string))
if string.endswith("ns"):
return value / 1000
elif string.endswith("ms"):
return value * 1000
elif string.endswith("µs"):
return value
return value * 1000 * 1000
for line in open(sys.argv[1]):
line = line.strip()
line_count += 1
if line_count >= 3:
start = "# "
columns = list(filter(None, line.split(" ")))
benchmark_name = columns[0]
benchmark_new_time = columns[2]
if len(columns) < 7:
# Old benchmark did not exist
benchmark_old_time = benchmark_new_time
else:
benchmark_old_time = columns[6]
benchmark_new_time = transform_to_micro_seconds(benchmark_new_time)
benchmark_old_time = transform_to_micro_seconds(benchmark_old_time)
max_name_len = max(max_name_len, len(benchmark_name))
max_new_time_len = max(max_new_time_len, len(str(benchmark_new_time)))
max_old_time_len = max(max_old_time_len, len(str(benchmark_old_time)))
total_time += benchmark_new_time
speedup = benchmark_new_time / benchmark_old_time
table_rows.append((benchmark_name, benchmark_new_time, benchmark_old_time))
new_results.append((benchmark_new_time, benchmark_name))
# Output diff table:
title_change = "Change (%)"
desired_name_len = max_name_len
space_between_columns = 3
line_width = (
max_name_len
+ max_new_time_len
+ max_old_time_len
+ len(title_change)
+ 3 * space_between_columns
)
print("```diff")
print(("@@" + "Benchmark Difference".center(line_width - 2) + "@@").replace(" ", " "))
# Table title:
title_name = "Name"
title_new = "New (μs)"
title_old = "Old (μs)"
desired_new_len = max_new_time_len + space_between_columns
desired_old_len = max_old_time_len + space_between_columns
desired_change_len = len(title_change) + space_between_columns
line = " " * (desired_name_len - len(title_name)) + title_name
line += " " * (desired_old_len - len(title_old)) + title_old
line += " " * (desired_new_len - len(title_new)) + title_new
line += " " * space_between_columns + title_change
print(("# " + line).replace(" ", " "))
for (name, new_time, old_time) in table_rows:
percentage_change = int(100 * (new_time / old_time - 1))
delta = 30
if percentage_change < -delta:
start = "+ "
elif percentage_change > delta:
start = "- "
else:
start = " "
percentage_change = ("+" if percentage_change > 0 else "") + str(percentage_change)
new_time = f"{int(round(new_time)):,}"
old_time = f"{int(round(old_time)):,}"
desired_name_len = max_name_len
desired_new_len = max_new_time_len + space_between_columns
desired_old_len = max_old_time_len + space_between_columns
line = " " * (desired_name_len - len(name)) + name
line += " " * (desired_old_len - len(str(old_time))) + str(old_time)
line += " " * (desired_new_len - len(str(new_time))) + str(new_time)
line += " " * (desired_change_len - len(str(percentage_change))) + percentage_change
print((start + line).replace(" ", " ")) # Non-breaking space for diff alignment
print("```")
new_results.sort(reverse=True)
print("")
print("Benchmark | Time (μs) | Time (%)")
print("--- | --: | --:")
for (time, name) in new_results:
percentage_time = (100.0 * time) / total_time
print(f"{name} | {int(round(time)):,} | {percentage_time:.1f}")