Skip to content

Commit 48909db

Browse files
committed
lint by flake8 && format by black and isort
Signed-off-by: Xiaodong Ye <xiaodong.ye@mthreads.com>
1 parent ff17436 commit 48909db

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

scripts/compare-test-backend-ops-perf.py

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#!/usr/bin/env python3
22

33
import argparse
4+
import logging
45
import re
6+
import sys
57
from pathlib import Path
68

9+
# Set up logging
10+
logger = logging.getLogger(__name__)
11+
logging.basicConfig(level=logging.INFO, format="%(message)s")
12+
713

814
def parse_benchmark_line(line: str):
915
"""
@@ -15,27 +21,27 @@ def parse_benchmark_line(line: str):
1521
Returns a tuple of (key, gflops) or (None, None) if parsing fails.
1622
"""
1723
line = line.strip()
18-
if ':' not in line:
24+
if ":" not in line:
1925
return None, None
2026

21-
key, data_part = line.split(':', 1)
27+
key, data_part = line.split(":", 1)
2228
key = key.strip()
2329

2430
# 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)
2632

2733
# 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())
2935
if not match:
3036
return None, None
3137

3238
value_str, unit = match.groups()
3339
value = float(value_str)
3440

3541
# Normalize everything to GFLOPS
36-
if unit == 'TFLOPS':
42+
if unit == "TFLOPS":
3743
gflops = value * 1000
38-
elif unit == 'MFLOPS':
44+
elif unit == "MFLOPS":
3945
gflops = value / 1000
4046
else: # GFLOPS
4147
gflops = value
@@ -47,7 +53,7 @@ def extract_commit_id(filepath: Path) -> str:
4753
"""Extract commit ID from filename like test-backend-ops-perf-abc1234.log"""
4854
filename = filepath.name
4955
# 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)
5157
if match:
5258
return match.group(1)
5359
return ""
@@ -57,14 +63,14 @@ def load_results(filepath: Path) -> dict:
5763
"""Loads all benchmark results from a file into a dictionary."""
5864
results = {}
5965
try:
60-
with open(filepath, 'r', encoding='utf-8') as f:
66+
with open(filepath, "r", encoding="utf-8") as f:
6167
for line in f:
6268
key, gflops = parse_benchmark_line(line)
6369
if key:
6470
results[key] = gflops
6571
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)
6874
return results
6975

7076

@@ -82,29 +88,32 @@ def main():
8288
"""Main function to compare benchmark files."""
8389
parser = argparse.ArgumentParser(
8490
description="Compare two benchmark result files and generate a report.",
85-
formatter_class=argparse.RawTextHelpFormatter
91+
formatter_class=argparse.RawTextHelpFormatter,
8692
)
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
8996
)
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
93100
)
94-
parser.add_argument("-c", "--compare", dest="compare", type=Path, required=True, help=help_c)
95101
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).",
98107
)
99108
args = parser.parse_args()
100109

101-
print(f"Loading baseline results from: {args.baseline}")
110+
logger.info(f"Loading baseline results from: {args.baseline}")
102111
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}")
104113
compare_results = load_results(args.compare)
105114

106115
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.")
108117
return
109118

110119
# Extract commit IDs from filenames
@@ -119,15 +128,20 @@ def main():
119128
baseline_val = baseline_results.get(key)
120129
compare_val = compare_results.get(key)
121130

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+
}
123137

124138
if baseline_val is not None and compare_val is not None:
125139
entry["change"] = ((compare_val - baseline_val) / baseline_val) * 100
126140

127141
comparisons.append(entry)
128142

129143
# --- Generate Report ---
130-
with open(args.output, 'w', encoding='utf-8') as f:
144+
with open(args.output, "w", encoding="utf-8") as f:
131145

132146
# Create header with commit IDs extracted from filenames
133147
baseline_header = "Baseline GFLOPS"
@@ -144,12 +158,18 @@ def main():
144158
f.write("-" * len(header) + "\n")
145159

146160
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}")
153173

154174

155175
if __name__ == "__main__":

0 commit comments

Comments
 (0)