Skip to content

Commit 60eea57

Browse files
committed
fix: update benchmark test script
1 parent e2859e2 commit 60eea57

File tree

3 files changed

+64
-15
lines changed

3 files changed

+64
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ result.txt
1313
testing/main.c
1414
*/*compile_commands.json
1515
testing/benchmark_results.txt
16+
testing/test-examples/
1617

1718
# Ignore Python wheel packages (clang-format, clang-tidy)
1819
clang-tidy-1*

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ repos:
1717
rev: v0.12.11
1818
hooks:
1919
- id: ruff
20+
args: [--fix]
2021
- id: ruff-format

testing/benchmark_hooks.py

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,29 @@
3030
]
3131

3232
# Automatically find all C/C++ files in testing/ (and optionally src/, include/)
33-
TARGET_FILES = (
34-
glob.glob("testing/**/*.c", recursive=True)
35-
+ glob.glob("testing/**/*.cpp", recursive=True)
36-
+ glob.glob("testing/**/*.h", recursive=True)
37-
+ glob.glob("testing/**/*.hpp", recursive=True)
38-
)
33+
TARGET_FILES = glob.glob("testing/test-examples/*.c", recursive=True)
3934

4035
REPEATS = 5
4136
RESULTS_FILE = "testing/benchmark_results.txt"
4237

4338

39+
def git_clone():
40+
try:
41+
subprocess.run(
42+
[
43+
"git",
44+
"clone",
45+
"--depth",
46+
"1",
47+
"https://github.com/gouravthakur39/beginners-C-program-examples.git",
48+
"testing/test-examples",
49+
],
50+
check=True,
51+
)
52+
except subprocess.CalledProcessError:
53+
pass
54+
55+
4456
def run_hook(config, files):
4557
cmd = ["pre-commit", "run", "--config", config, "--files"] + files
4658
start = time.perf_counter()
@@ -53,14 +65,28 @@ def run_hook(config, files):
5365
return end - start
5466

5567

68+
def safe_git_restore(files):
69+
# Only restore files tracked by git
70+
tracked = []
71+
for f in files:
72+
result = subprocess.run(
73+
["git", "ls-files", "--error-unmatch", f],
74+
stdout=subprocess.PIPE,
75+
stderr=subprocess.PIPE,
76+
)
77+
if result.returncode == 0:
78+
tracked.append(f)
79+
if tracked:
80+
subprocess.run(["git", "restore"] + tracked)
81+
82+
5683
def benchmark():
5784
results = {}
5885
for hook in HOOKS:
5986
times = []
6087
print(f"Benchmarking {hook['name']}...")
6188
for i in range(REPEATS):
62-
# Clean up any changes before each run
63-
subprocess.run(["git", "restore"] + TARGET_FILES)
89+
safe_git_restore(TARGET_FILES)
6490
subprocess.run(["pre-commit", "clean"])
6591
t = run_hook(hook["config"], TARGET_FILES)
6692
print(f" Run {i + 1}: {t:.3f} seconds")
@@ -70,23 +96,44 @@ def benchmark():
7096

7197

7298
def report(results):
99+
headers = ["Hook", "Avg (s)", "Std (s)", "Min (s)", "Max (s)", "Runs"]
100+
col_widths = [max(len(h), 16) for h in headers]
101+
# Calculate max width for each column
102+
for name, times in results.items():
103+
col_widths[0] = max(col_widths[0], len(name))
104+
print("\nBenchmark Results:\n")
105+
# Print header
106+
header_row = " | ".join(h.ljust(w) for h, w in zip(headers, col_widths))
107+
print(header_row)
108+
print("-+-".join("-" * w for w in col_widths))
109+
# Print rows
73110
lines = []
74111
for name, times in results.items():
75112
avg = statistics.mean(times)
76113
std = statistics.stdev(times) if len(times) > 1 else 0.0
77114
min_t = min(times)
78115
max_t = max(times)
79-
lines.append(
80-
f"{name}: avg={avg:.3f}s, std={std:.3f}s, min={min_t:.3f}s, max={max_t:.3f}s, runs={len(times)}"
81-
)
82-
print("\nBenchmark Results:")
83-
print("\n".join(lines))
116+
row = [
117+
name.ljust(col_widths[0]),
118+
f"{avg:.3f}".ljust(col_widths[1]),
119+
f"{std:.3f}".ljust(col_widths[2]),
120+
f"{min_t:.3f}".ljust(col_widths[3]),
121+
f"{max_t:.3f}".ljust(col_widths[4]),
122+
str(len(times)).ljust(col_widths[5]),
123+
]
124+
print(" | ".join(row))
125+
lines.append(" | ".join(row))
126+
# Save to file
84127
with open(RESULTS_FILE, "w") as f:
85-
f.write("\n".join(lines) + "\n")
86-
print(f"Results saved to {RESULTS_FILE}")
128+
f.write(header_row + "\n")
129+
f.write("-+-".join("-" * w for w in col_widths) + "\n")
130+
for line in lines:
131+
f.write(line + "\n")
132+
print(f"\nResults saved to {RESULTS_FILE}")
87133

88134

89135
def main():
136+
git_clone()
90137
results = benchmark()
91138
report(results)
92139

0 commit comments

Comments
 (0)