1
1
#!/usr/bin/env python3
2
2
3
3
import argparse
4
+ import logging
4
5
import re
6
+ import sys
5
7
from pathlib import Path
6
8
9
+ # Set up logging
10
+ logger = logging .getLogger (__name__ )
11
+ logging .basicConfig (level = logging .INFO , format = "%(message)s" )
12
+
7
13
8
14
def parse_benchmark_line (line : str ):
9
15
"""
@@ -15,27 +21,27 @@ def parse_benchmark_line(line: str):
15
21
Returns a tuple of (key, gflops) or (None, None) if parsing fails.
16
22
"""
17
23
line = line .strip ()
18
- if ':' not in line :
24
+ if ":" not in line :
19
25
return None , None
20
26
21
- key , data_part = line .split (':' , 1 )
27
+ key , data_part = line .split (":" , 1 )
22
28
key = key .strip ()
23
29
24
30
# Remove ANSI color codes from the data part
25
- data_part = re .sub (r' \x1b\[[0-9;]*m' , '' , data_part )
31
+ data_part = re .sub (r" \x1b\[[0-9;]*m" , "" , data_part )
26
32
27
33
# Find the last number and unit in the data part
28
- match = re .search (r' ([\d\.]+)\s+(GFLOPS|TFLOPS|MFLOPS)\s*$' , data_part .strip ())
34
+ match = re .search (r" ([\d\.]+)\s+(GFLOPS|TFLOPS|MFLOPS)\s*$" , data_part .strip ())
29
35
if not match :
30
36
return None , None
31
37
32
38
value_str , unit = match .groups ()
33
39
value = float (value_str )
34
40
35
41
# Normalize everything to GFLOPS
36
- if unit == ' TFLOPS' :
42
+ if unit == " TFLOPS" :
37
43
gflops = value * 1000
38
- elif unit == ' MFLOPS' :
44
+ elif unit == " MFLOPS" :
39
45
gflops = value / 1000
40
46
else : # GFLOPS
41
47
gflops = value
@@ -47,7 +53,7 @@ def extract_commit_id(filepath: Path) -> str:
47
53
"""Extract commit ID from filename like test-backend-ops-perf-abc1234.log"""
48
54
filename = filepath .name
49
55
# Pattern: test-backend-ops-perf-<commit_id>.log
50
- match = re .match (r' test-backend-ops-perf-([^.]+)\.log' , filename )
56
+ match = re .match (r" test-backend-ops-perf-([^.]+)\.log" , filename )
51
57
if match :
52
58
return match .group (1 )
53
59
return ""
@@ -57,14 +63,14 @@ def load_results(filepath: Path) -> dict:
57
63
"""Loads all benchmark results from a file into a dictionary."""
58
64
results = {}
59
65
try :
60
- with open (filepath , 'r' , encoding = ' utf-8' ) as f :
66
+ with open (filepath , "r" , encoding = " utf-8" ) as f :
61
67
for line in f :
62
68
key , gflops = parse_benchmark_line (line )
63
69
if key :
64
70
results [key ] = gflops
65
71
except FileNotFoundError :
66
- print (f"Error: File not found at { filepath } " )
67
- exit (1 )
72
+ logger . error (f"Error: File not found at { filepath } " )
73
+ sys . exit (1 )
68
74
return results
69
75
70
76
@@ -82,29 +88,32 @@ def main():
82
88
"""Main function to compare benchmark files."""
83
89
parser = argparse .ArgumentParser (
84
90
description = "Compare two benchmark result files and generate a report." ,
85
- formatter_class = argparse .RawTextHelpFormatter
91
+ formatter_class = argparse .RawTextHelpFormatter ,
86
92
)
87
- help_b = (
88
- "Path to the baseline benchmark results file."
93
+ help_b = "Path to the baseline benchmark results file."
94
+ parser .add_argument (
95
+ "-b" , "--baseline" , dest = "baseline" , type = Path , required = True , help = help_b
89
96
)
90
- parser . add_argument ( "-b" , "--baseline" , dest = "baseline" , type = Path , required = True , help = help_b )
91
- help_c = (
92
- "Path to the benchmark results file to compare against the baseline."
97
+ help_c = " Path to the benchmark results file to compare against the baseline."
98
+ parser . add_argument (
99
+ "-c" , "--compare" , dest = " compare" , type = Path , required = True , help = help_c
93
100
)
94
- parser .add_argument ("-c" , "--compare" , dest = "compare" , type = Path , required = True , help = help_c )
95
101
parser .add_argument (
96
- "-o" , "--output" , type = Path , default = "comparison_backend_ops_perf.txt" ,
97
- help = "Path to the output report file (default: comparison_backend_ops_perf.txt)."
102
+ "-o" ,
103
+ "--output" ,
104
+ type = Path ,
105
+ default = "comparison_backend_ops_perf.txt" ,
106
+ help = "Path to the output report file (default: comparison_backend_ops_perf.txt)." ,
98
107
)
99
108
args = parser .parse_args ()
100
109
101
- print (f"Loading baseline results from: { args .baseline } " )
110
+ logger . info (f"Loading baseline results from: { args .baseline } " )
102
111
baseline_results = load_results (args .baseline )
103
- print (f"Loading compare results from: { args .compare } " )
112
+ logger . info (f"Loading compare results from: { args .compare } " )
104
113
compare_results = load_results (args .compare )
105
114
106
115
if not baseline_results or not compare_results :
107
- print ("Could not load results from one or both files. Exiting." )
116
+ logger . error ("Could not load results from one or both files. Exiting." )
108
117
return
109
118
110
119
# Extract commit IDs from filenames
@@ -119,15 +128,20 @@ def main():
119
128
baseline_val = baseline_results .get (key )
120
129
compare_val = compare_results .get (key )
121
130
122
- entry = {"key" : key , "baseline" : baseline_val , "compare" : compare_val , "change" : 0 }
131
+ entry = {
132
+ "key" : key ,
133
+ "baseline" : baseline_val ,
134
+ "compare" : compare_val ,
135
+ "change" : 0 ,
136
+ }
123
137
124
138
if baseline_val is not None and compare_val is not None :
125
139
entry ["change" ] = ((compare_val - baseline_val ) / baseline_val ) * 100
126
140
127
141
comparisons .append (entry )
128
142
129
143
# --- Generate Report ---
130
- with open (args .output , 'w' , encoding = ' utf-8' ) as f :
144
+ with open (args .output , "w" , encoding = " utf-8" ) as f :
131
145
132
146
# Create header with commit IDs extracted from filenames
133
147
baseline_header = "Baseline GFLOPS"
@@ -144,12 +158,18 @@ def main():
144
158
f .write ("-" * len (header ) + "\n " )
145
159
146
160
for item in comparisons :
147
- baseline_str = f"{ item ['baseline' ]:.2f} " if item ['baseline' ] is not None else "N/A"
148
- compare_str = f"{ item ['compare' ]:.2f} " if item ['compare' ] is not None else "N/A"
149
- change_str = format_change (item ['change' ])
150
- f .write (f"{ item ['key' ]:<{key_width }} { baseline_str :>25} { compare_str :>25} { change_str :>15} \n " )
151
-
152
- print (f"Comparison report successfully generated at: { args .output } " )
161
+ baseline_str = (
162
+ f"{ item ['baseline' ]:.2f} " if item ["baseline" ] is not None else "N/A"
163
+ )
164
+ compare_str = (
165
+ f"{ item ['compare' ]:.2f} " if item ["compare" ] is not None else "N/A"
166
+ )
167
+ change_str = format_change (item ["change" ])
168
+ f .write (
169
+ f"{ item ['key' ]:<{key_width }} { baseline_str :>25} { compare_str :>25} { change_str :>15} \n "
170
+ )
171
+
172
+ logger .info (f"Comparison report successfully generated at: { args .output } " )
153
173
154
174
155
175
if __name__ == "__main__" :
0 commit comments