55# SPDX-License-Identifier: MIT
66
77# Run all Python benchmark files and save results
8- # Usage: ./run_all.sh [OUTPUT_DIR]
8+ # Usage: ./run_all.sh [OUTPUT_DIR] [--json]
9+
10+ # Enable pipefail to catch errors in piped commands
11+ set -o pipefail
912
1013cd " $( dirname " $0 " ) "
1114
1215OUTPUT_DIR=" ${1:- .} "
16+ FORMAT=" txt"
17+
18+ # Parse arguments
19+ for arg in " $@ " ; do
20+ if [[ " $arg " == " --json" ]]; then
21+ FORMAT=" json"
22+ elif [[ -z " $OUTPUT_DIR " ]] || [[ " $OUTPUT_DIR " == " --json" ]]; then
23+ OUTPUT_DIR=" ."
24+ fi
25+ done
26+
27+ # If --json is first argument, reset OUTPUT_DIR
28+ if [[ " $OUTPUT_DIR " == " --json" ]]; then
29+ OUTPUT_DIR=" ${2:- .} "
30+ fi
31+
1332mkdir -p " $OUTPUT_DIR "
1433
1534echo " Running benchmarks sequentially (parallel execution disabled to ensure accurate results)..."
35+ echo " Output format: $FORMAT "
1636echo " Results will be saved to: $OUTPUT_DIR "
1737echo " Current directory: $( pwd) "
1838echo " Benchmark files found: $( ls bench_* .py 2> /dev/null | wc -l) "
@@ -24,7 +44,27 @@ if [[ ! -w "$OUTPUT_DIR" ]]; then
2444 exit 1
2545fi
2646
27- # Run each benchmark and capture output
47+ # Use JSON runner if --json flag is set
48+ if [[ " $FORMAT " == " json" ]]; then
49+ echo " Using JSON output format..."
50+ if python3 run_all_json.py " $OUTPUT_DIR " ; then
51+ echo " "
52+ echo " =========================================="
53+ echo " All benchmarks complete!"
54+ echo " Results directory: $OUTPUT_DIR "
55+ echo " Files created:"
56+ ls -lh " $OUTPUT_DIR " /* .json 2> /dev/null || echo " No result files found"
57+ echo " =========================================="
58+ exit 0
59+ else
60+ echo " Benchmark execution failed" >&2
61+ exit 1
62+ fi
63+ fi
64+
65+ # Original text format runner
66+ FAILED_BENCHMARKS=()
67+
2868for file in bench_* .py; do
2969 if [[ ! -f " $file " ]]; then
3070 echo " Warning: No benchmark files matching bench_*.py found" >&2
@@ -38,23 +78,43 @@ for file in bench_*.py; do
3878 echo " Running $file ..."
3979 echo " =========================================="
4080
41- # Ensure output file is created even if benchmark produces no output
42- touch " $output_file "
43-
44- if python " $file " 2>&1 | tee " $output_file " ; then
81+ # Run benchmark and capture output
82+ # Note: tee will create the file, errors go to both console and file
83+ if python3 " $file " 2>&1 | tee " $output_file " ; then
84+ # Success - ensure file is readable
85+ chmod 644 " $output_file " 2> /dev/null || true
4586 echo " ✓ PASSED: $file "
4687 echo " Results saved to: $output_file "
4788 else
89+ # Failure - mark file and ensure readable
90+ # tee already captured the output, just prepend marker
91+ (echo " BENCHMARK FAILED" ; echo " " ; cat " $output_file " ) > " $output_file .new" 2> /dev/null && \
92+ mv " $output_file .new" " $output_file " 2> /dev/null || \
93+ echo " BENCHMARK FAILED" > " $output_file "
94+ chmod 644 " $output_file " 2> /dev/null || true
4895 echo " ✗ FAILED: $file "
49- echo " FAILED " > " $output_file "
50- exit 1 # Exit with error if any benchmark fails
96+ echo " Error details saved to: $output_file "
97+ FAILED_BENCHMARKS+=( " $file " )
5198 fi
5299 echo " "
53100done
54101
55102echo " =========================================="
56- echo " All benchmarks complete!"
103+ if [ ${# FAILED_BENCHMARKS[@]} -eq 0 ]; then
104+ echo " All benchmarks complete! ✓"
105+ else
106+ echo " Benchmarks complete with failures! ✗"
107+ echo " Failed benchmarks:"
108+ for failed in " ${FAILED_BENCHMARKS[@]} " ; do
109+ echo " - $failed "
110+ done
111+ fi
57112echo " Results directory: $OUTPUT_DIR "
58113echo " Files created:"
59114ls -lh " $OUTPUT_DIR " /* _results.txt 2> /dev/null || echo " No result files found"
60115echo " =========================================="
116+
117+ # Exit with error if any benchmarks failed
118+ if [ ${# FAILED_BENCHMARKS[@]} -gt 0 ]; then
119+ exit 1
120+ fi
0 commit comments