8
8
Requirements:
9
9
- pre-commit must be installed and available in PATH
10
10
- Two config files:
11
- - testing/pre-commit-config- cpp-linter-hooks.yaml
12
- - testing/pre-commit-config- mirrors-clang-format.yaml
13
- - Target files: testing/main .c (or adjust as needed)
11
+ - testing/cpp-linter-hooks.yaml
12
+ - testing/mirrors-clang-format.yaml
13
+ - Target files: testing/examples/* .c (or adjust as needed)
14
14
"""
15
15
16
16
import os
17
17
import subprocess
18
18
import time
19
19
import statistics
20
- import glob
21
20
22
21
HOOKS = [
23
- {
24
- "name" : "cpp-linter-hooks" ,
25
- "config" : "testing/benchmark_hook_1.yaml" ,
26
- },
27
22
{
28
23
"name" : "mirrors-clang-format" ,
29
24
"config" : "testing/benchmark_hook_2.yaml" ,
30
25
},
26
+ {
27
+ "name" : "cpp-linter-hooks" ,
28
+ "config" : "testing/benchmark_hook_1.yaml" ,
29
+ },
31
30
]
32
31
33
- # Automatically find all C/C++ files in testing/ (and optionally src/, include/)
34
- TARGET_FILES = glob .glob ("testing/test-examples/*.c" , recursive = True )
35
-
36
32
REPEATS = 5
37
33
RESULTS_FILE = "testing/benchmark_results.txt"
38
34
39
35
40
- def git_clone ():
36
+ def prepare_code ():
41
37
try :
38
+ subprocess .run (["rm" , "-rf" , "testing/examples" ], check = True )
42
39
subprocess .run (
43
40
[
44
41
"git" ,
45
42
"clone" ,
46
43
"--depth" ,
47
44
"1" ,
48
45
"https://github.com/gouravthakur39/beginners-C-program-examples.git" ,
49
- "testing/test- examples" ,
46
+ "testing/examples" ,
50
47
],
51
48
check = True ,
52
49
)
53
50
except subprocess .CalledProcessError :
54
51
pass
55
52
56
53
57
- def run_hook (config , files ):
58
- cmd = ["pre-commit" , "run" , "--config" , config , "--files" ] + files
54
+ def run_hook (config ):
55
+ cmd = ["pre-commit" , "run" , "--config" , config , "--all- files" ]
59
56
start = time .perf_counter ()
60
57
try :
61
58
subprocess .run (cmd , check = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
@@ -66,30 +63,16 @@ def run_hook(config, files):
66
63
return end - start
67
64
68
65
69
- def safe_git_restore (files ):
70
- # Only restore files tracked by git
71
- tracked = []
72
- for f in files :
73
- result = subprocess .run (
74
- ["git" , "ls-files" , "--error-unmatch" , f ],
75
- stdout = subprocess .PIPE ,
76
- stderr = subprocess .PIPE ,
77
- )
78
- if result .returncode == 0 :
79
- tracked .append (f )
80
- if tracked :
81
- subprocess .run (["git" , "restore" ] + tracked )
82
-
83
-
84
66
def benchmark ():
85
67
results = {}
68
+ os .chdir ("testing/examples" )
86
69
for hook in HOOKS :
87
70
times = []
88
71
print (f"\n Benchmarking { hook ['name' ]} ..." )
89
72
for i in range (REPEATS ):
90
- safe_git_restore ( TARGET_FILES )
73
+ prepare_code ( )
91
74
subprocess .run (["pre-commit" , "clean" ])
92
- t = run_hook (hook ["config" ], TARGET_FILES )
75
+ t = run_hook (hook ["config" ])
93
76
print (f" Run { i + 1 } : { t :.3f} seconds" )
94
77
times .append (t )
95
78
results [hook ["name" ]] = times
@@ -132,20 +115,27 @@ def report(results):
132
115
f .write (line + "\n " )
133
116
print (f"\n Results saved to { RESULTS_FILE } " )
134
117
135
- # Write to GitHub Actions summary if available
118
+ # Write to GitHub Actions summary
136
119
summary_path = os .environ .get ("GITHUB_STEP_SUMMARY" )
137
120
if summary_path :
138
121
with open (summary_path , "a" ) as f :
139
122
f .write ("## Benchmark Results\n \n " )
140
- f .write (header_row + "\n " )
141
- f .write ("-+-" .join ("-" * w for w in col_widths ) + "\n " )
142
- for line in lines :
143
- f .write (line + "\n " )
123
+ # Markdown table header
124
+ md_header = "| " + " | " .join (headers ) + " |\n "
125
+ md_sep = "|" + "|" .join (["-" * (w + 2 ) for w in col_widths ]) + "|\n "
126
+ f .write (md_header )
127
+ f .write (md_sep )
128
+ for name , times in results .items ():
129
+ avg = statistics .mean (times )
130
+ std = statistics .stdev (times ) if len (times ) > 1 else 0.0
131
+ min_t = min (times )
132
+ max_t = max (times )
133
+ md_row = f"| { name } | { avg :.3f} | { std :.3f} | { min_t :.3f} | { max_t :.3f} | { len (times )} |\n "
134
+ f .write (md_row )
144
135
f .write ("\n " )
145
136
146
137
147
138
def main ():
148
- git_clone ()
149
139
results = benchmark ()
150
140
report (results )
151
141
0 commit comments