1+ #! /bin/bash
2+ set -euo pipefail
3+
4+ # Script to combine individual benchmark results into LATEST.md
5+ # Usage: ./combine-benchmarks.sh <output_dir>
6+
7+ OUTPUT_DIR=" ${1:- benches} "
8+
9+ # Create output directory if it doesn't exist
10+ mkdir -p " $OUTPUT_DIR "
11+
12+ # Define the benchmark files and their section names
13+ declare -A BENCHMARK_FILES=(
14+ [" forge_test_bench.md" ]=" Forge Test"
15+ [" forge_build_bench.md" ]=" Forge Build"
16+ [" forge_coverage_bench.md" ]=" Forge Coverage"
17+ )
18+
19+ # Function to extract a specific section from a benchmark file
20+ extract_section () {
21+ local file=$1
22+ local section=$2
23+ local in_section=0
24+
25+ while IFS= read -r line; do
26+ if [[ " $line " =~ ^# #[[:space:]]+"$section" ]]; then
27+ in_section= 1
28+ echo " $line "
29+ elif [[ $in_section -eq 1 && " $line " =~ ^# #[[:space:]] && ! "$line" =~ ^##[[:space:]]+"$section" ]]; then
30+ break
31+ elif [[ $in_section -eq 1 ]]; then
32+ echo " $line "
33+ fi
34+ done < " $file "
35+ }
36+
37+ # Function to extract summary info (repos and versions) from a file
38+ extract_summary_info () {
39+ local file=$1
40+ local in_summary=0
41+ local in_repos=0
42+ local in_versions=0
43+
44+ while IFS= read -r line; do
45+ # Check for Summary section
46+ if [[ " $line " =~ ^# #[[:space:]]+Summary ]]; then
47+ in_summary= 1
48+ continue
49+ fi
50+
51+ # Check for Repositories Tested subsection
52+ if [[ $in_summary -eq 1 && " $line " =~ ^# ##[[:space:]]+Repositories[[:space:]]+Tested ]]; then
53+ in_repos= 1
54+ echo " ### Repositories Tested"
55+ echo
56+ continue
57+ fi
58+
59+ # Check for Foundry Versions subsection
60+ if [[ $in_summary -eq 1 && " $line " =~ ^# ##[[:space:]]+Foundry[[:space:]]+Versions ]]; then
61+ in_repos= 0
62+ in_versions= 1
63+ echo " ### Foundry Versions"
64+ echo
65+ continue
66+ fi
67+
68+ # End of summary section
69+ if [[ $in_summary -eq 1 && " $line " =~ ^# #[[:space:]] && ! "$line" =~ ^##[[:space:]]+Summary ]]; then
70+ break
71+ fi
72+
73+ # Output repo or version lines
74+ if [[ ($in_repos -eq 1 || $in_versions -eq 1) && -n " $line " ]]; then
75+ echo " $line "
76+ fi
77+ done < " $file "
78+ }
79+
80+ # Function to extract benchmark table from a section
81+ extract_benchmark_table () {
82+ local file=$1
83+ local section=$2
84+ local in_section=0
85+ local found_table=0
86+
87+ while IFS= read -r line; do
88+ if [[ " $line " =~ ^# #[[:space:]]+"$section" ]]; then
89+ in_section= 1
90+ continue
91+ elif [[ $in_section -eq 1 && " $line " =~ ^# #[[:space:]] && ! "$line" =~ ^##[[:space:]]+"$section" ]]; then
92+ break
93+ elif [[ $in_section -eq 1 ]]; then
94+ # Skip empty lines before table
95+ if [[ -z " $line " && $found_table -eq 0 ]]; then
96+ continue
97+ fi
98+ # Detect table start
99+ if [[ " $line " =~ ^\| [[:space:]]* Repository ]]; then
100+ found_table=1
101+ fi
102+ # Output table lines
103+ if [[ $found_table -eq 1 && -n " $line " ]]; then
104+ echo " $line "
105+ fi
106+ fi
107+ done < " $file "
108+ }
109+
110+ # Function to extract system information
111+ extract_system_info () {
112+ local file=$1
113+ # Extract from System Information to end of file (EOF)
114+ awk ' /^## System Information/ { found=1; next } found { print }' " $file "
115+ }
116+
117+ # Start building LATEST.md
118+ cat > " $OUTPUT_DIR /LATEST.md" << EOF
119+ # 📊 Foundry Benchmark Results
120+
121+ ** Generated at** : $( date -u ' +%Y-%m-%d %H:%M:%S UTC' )
122+
123+ EOF
124+
125+ # Process each benchmark file
126+ FIRST_FILE= 1
127+ SYSTEM_INFO= " "
128+
129+ for bench_file in " forge_test_bench.md" " forge_isolate_test_bench.md" " forge_build_bench.md" " forge_coverage_bench.md" ; do
130+ if [ -f " $OUTPUT_DIR /$bench_file " ]; then
131+ echo " Processing $bench_file ..."
132+
133+ # Get the section name
134+ case " $bench_file " in
135+ " forge_test_bench.md" )
136+ SECTION_NAME=" Forge Test"
137+ ;;
138+ " forge_isolate_test_bench.md" )
139+ SECTION_NAME=" Forge Test (Isolated)"
140+ ;;
141+ " forge_build_bench.md" )
142+ SECTION_NAME=" Forge Build"
143+ ;;
144+ " forge_coverage_bench.md" )
145+ SECTION_NAME=" Forge Coverage"
146+ ;;
147+ esac
148+
149+ # Add section header
150+ echo " ## $SECTION_NAME " >> " $OUTPUT_DIR /LATEST.md"
151+ echo >> " $OUTPUT_DIR /LATEST.md"
152+
153+ # Add summary info (repos and versions)
154+ extract_summary_info " $OUTPUT_DIR /$bench_file " >> " $OUTPUT_DIR /LATEST.md"
155+ echo >> " $OUTPUT_DIR /LATEST.md"
156+
157+ # Handle different benchmark types
158+ if [[ " $bench_file " == " forge_test_bench.md" ]]; then
159+ # Extract both Forge Test and Forge Fuzz Test tables
160+ extract_benchmark_table " $OUTPUT_DIR /$bench_file " " Forge Test" >> " $OUTPUT_DIR /LATEST.md"
161+
162+ # Check if Forge Fuzz Test section exists
163+ if grep -q " ^## Forge Fuzz Test" " $OUTPUT_DIR /$bench_file " ; then
164+ echo >> " $OUTPUT_DIR /LATEST.md"
165+ echo " ## Forge Fuzz Test" >> " $OUTPUT_DIR /LATEST.md"
166+ echo >> " $OUTPUT_DIR /LATEST.md"
167+ extract_benchmark_table " $OUTPUT_DIR /$bench_file " " Forge Fuzz Test" >> " $OUTPUT_DIR /LATEST.md"
168+ fi
169+ elif [[ " $bench_file " == " forge_build_bench.md" ]]; then
170+ # Extract No Cache table
171+ echo " ### No Cache" >> " $OUTPUT_DIR /LATEST.md"
172+ echo >> " $OUTPUT_DIR /LATEST.md"
173+ extract_benchmark_table " $OUTPUT_DIR /$bench_file " " Forge Build (No Cache)" >> " $OUTPUT_DIR /LATEST.md"
174+ echo >> " $OUTPUT_DIR /LATEST.md"
175+
176+ # Extract With Cache table
177+ echo " ### With Cache" >> " $OUTPUT_DIR /LATEST.md"
178+ echo >> " $OUTPUT_DIR /LATEST.md"
179+ extract_benchmark_table " $OUTPUT_DIR /$bench_file " " Forge Build (With Cache)" >> " $OUTPUT_DIR /LATEST.md"
180+ else
181+ # Extract the benchmark table for other types
182+ extract_benchmark_table " $OUTPUT_DIR /$bench_file " " $SECTION_NAME " >> " $OUTPUT_DIR /LATEST.md"
183+ fi
184+
185+ echo >> " $OUTPUT_DIR /LATEST.md"
186+
187+ # Extract system info from first file only
188+ if [[ $FIRST_FILE -eq 1 ]]; then
189+ SYSTEM_INFO=$( extract_system_info " $OUTPUT_DIR /$bench_file " )
190+ FIRST_FILE=0
191+ fi
192+ else
193+ echo " Warning: $bench_file not found, skipping..."
194+ fi
195+ done
196+
197+ # Add system information at the end
198+ if [[ -n " $SYSTEM_INFO " ]]; then
199+ echo " ## System Information" >> " $OUTPUT_DIR /LATEST.md"
200+ echo >> " $OUTPUT_DIR /LATEST.md"
201+ echo " $SYSTEM_INFO " >> " $OUTPUT_DIR /LATEST.md"
202+ fi
203+
204+ echo " Successfully combined benchmark results into $OUTPUT_DIR /LATEST.md"
0 commit comments