88Requirements:
99- pre-commit must be installed and available in PATH
1010- 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)
1414"""
1515
1616import os
1717import subprocess
1818import time
1919import statistics
20- import glob
2120
2221HOOKS = [
23- {
24- "name" : "cpp-linter-hooks" ,
25- "config" : "testing/benchmark_hook_1.yaml" ,
26- },
2722 {
2823 "name" : "mirrors-clang-format" ,
2924 "config" : "testing/benchmark_hook_2.yaml" ,
3025 },
26+ {
27+ "name" : "cpp-linter-hooks" ,
28+ "config" : "testing/benchmark_hook_1.yaml" ,
29+ },
3130]
3231
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-
3632REPEATS = 5
3733RESULTS_FILE = "testing/benchmark_results.txt"
3834
3935
40- def git_clone ():
36+ def prepare_code ():
4137 try :
38+ subprocess .run (["rm" , "-rf" , "testing/examples" ], check = True )
4239 subprocess .run (
4340 [
4441 "git" ,
4542 "clone" ,
4643 "--depth" ,
4744 "1" ,
4845 "https://github.com/gouravthakur39/beginners-C-program-examples.git" ,
49- "testing/test- examples" ,
46+ "testing/examples" ,
5047 ],
5148 check = True ,
5249 )
5350 except subprocess .CalledProcessError :
5451 pass
5552
5653
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" ]
5956 start = time .perf_counter ()
6057 try :
6158 subprocess .run (cmd , check = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
@@ -66,30 +63,16 @@ def run_hook(config, files):
6663 return end - start
6764
6865
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-
8466def benchmark ():
8567 results = {}
68+ os .chdir ("testing/examples" )
8669 for hook in HOOKS :
8770 times = []
8871 print (f"\n Benchmarking { hook ['name' ]} ..." )
8972 for i in range (REPEATS ):
90- safe_git_restore ( TARGET_FILES )
73+ prepare_code ( )
9174 subprocess .run (["pre-commit" , "clean" ])
92- t = run_hook (hook ["config" ], TARGET_FILES )
75+ t = run_hook (hook ["config" ])
9376 print (f" Run { i + 1 } : { t :.3f} seconds" )
9477 times .append (t )
9578 results [hook ["name" ]] = times
@@ -132,20 +115,27 @@ def report(results):
132115 f .write (line + "\n " )
133116 print (f"\n Results saved to { RESULTS_FILE } " )
134117
135- # Write to GitHub Actions summary if available
118+ # Write to GitHub Actions summary
136119 summary_path = os .environ .get ("GITHUB_STEP_SUMMARY" )
137120 if summary_path :
138121 with open (summary_path , "a" ) as f :
139122 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 )
144135 f .write ("\n " )
145136
146137
147138def main ():
148- git_clone ()
149139 results = benchmark ()
150140 report (results )
151141
0 commit comments