bench.sh (4626B)
1 #!/bin/sh 2 # bench.sh - Benchmark script for cngf-pf solver performance and stability 3 # 4 # Usage: 5 # ./bench.sh # Run benchmarks on current build 6 # ./bench.sh -c branch1 branch2 # Compare two branches 7 # ./bench.sh -r N # Run N repetitions (default: 3) 8 # ./bench.sh -o file.csv # Output results to CSV file 9 # ./bench.sh -a file.csv # Append results to CSV file 10 # 11 # Output columns: 12 # branch, test, elapsed_time, timesteps, poisson_iters, darcy_iters, 13 # coupled_iters, stress_iters, iters_per_timestep 14 15 set -e 16 17 BIN=../cngf-pf 18 REPS=3 19 OUTFILE="" 20 APPEND=0 21 COMPARE="" 22 BRANCH1="" 23 BRANCH2="" 24 25 # Benchmark test configurations 26 # Note: transient mode (-T) skipped due to regression after inertia fix 27 BENCH_TESTS=" 28 dry_small:-o 0.03 -L 0.64 -n 40e3 29 dry_large:-L 10.0 -n 200e3 -e 10.0 30 wet_small:-o 0.03 -L 0.64 -n 40e3 -F 31 wet_large:-L 8.0 -n 150e3 -F -k 2e-17 -O 50e3 -a 50e3 -q 0.0000115741 -e 3600 32 " 33 34 usage() { 35 echo "Usage: $0 [-r reps] [-o outfile] [-a outfile] [-c branch1 branch2]" 36 echo " -r N Run N repetitions (default: 3)" 37 echo " -o file.csv Output results to CSV file" 38 echo " -a file.csv Append results to CSV file" 39 echo " -c b1 b2 Compare two git branches" 40 exit 1 41 } 42 43 # Parse arguments 44 while [ $# -gt 0 ]; do 45 case "$1" in 46 -r) REPS="$2"; shift 2 ;; 47 -o) OUTFILE="$2"; APPEND=0; shift 2 ;; 48 -a) OUTFILE="$2"; APPEND=1; shift 2 ;; 49 -c) COMPARE=1; BRANCH1="$2"; BRANCH2="$3"; shift 3 ;; 50 -h|--help) usage ;; 51 *) usage ;; 52 esac 53 done 54 55 # Parse benchmark output 56 parse_output() { 57 local output="$1" 58 elapsed=$(echo "$output" | grep "elapsed_time" | sed 's/.*elapsed_time=//' | cut -d' ' -f1) 59 timesteps=$(echo "$output" | grep "timesteps=" | sed 's/.*timesteps=//' | cut -d' ' -f1) 60 poisson=$(echo "$output" | grep "poisson_iters=" | sed 's/.*poisson_iters=//' | cut -d' ' -f1) 61 darcy=$(echo "$output" | grep "darcy_iters=" | sed 's/.*darcy_iters=//' | cut -d' ' -f1) 62 coupled=$(echo "$output" | grep "coupled_iters=" | sed 's/.*coupled_iters=//' | cut -d' ' -f1) 63 stress=$(echo "$output" | grep "stress_iters=" | sed 's/.*stress_iters=//' | cut -d' ' -f1) 64 65 # Calculate total iterations per timestep 66 total_iters=$((poisson + darcy + coupled + stress)) 67 if [ "$timesteps" -gt 0 ]; then 68 iters_per_step=$(echo "scale=2; $total_iters / $timesteps" | bc) 69 else 70 iters_per_step=0 71 fi 72 73 echo "$elapsed,$timesteps,$poisson,$darcy,$coupled,$stress,$iters_per_step" 74 } 75 76 # Run benchmarks for a given branch/state 77 run_benchmarks() { 78 local branch_name="$1" 79 80 echo "$BENCH_TESTS" | while IFS=: read -r name opts; do 81 [ -z "$name" ] && continue 82 83 # Run multiple repetitions 84 for rep in $(seq 1 "$REPS"); do 85 output=$($BIN -B $opts 2>&1) 86 result=$(parse_output "$output") 87 echo "$branch_name,$name,$rep,$result" 88 done 89 done 90 } 91 92 # Header for CSV output 93 csv_header() { 94 echo "branch,test,rep,elapsed_time,timesteps,poisson_iters,darcy_iters,coupled_iters,stress_iters,iters_per_timestep" 95 } 96 97 # Main execution 98 if [ -n "$COMPARE" ]; then 99 # Compare two branches 100 current_branch=$(git rev-parse --abbrev-ref HEAD) 101 102 echo "Comparing $BRANCH1 vs $BRANCH2" >&2 103 104 # Build and benchmark branch 1 105 echo "Building $BRANCH1..." >&2 106 git checkout -q "$BRANCH1" 107 make -C .. clean >/dev/null 2>&1 108 make -C .. >/dev/null 2>&1 109 results1=$(run_benchmarks "$BRANCH1") 110 111 # Build and benchmark branch 2 112 echo "Building $BRANCH2..." >&2 113 git checkout -q "$BRANCH2" 114 make -C .. clean >/dev/null 2>&1 115 make -C .. >/dev/null 2>&1 116 results2=$(run_benchmarks "$BRANCH2") 117 118 # Return to original branch 119 git checkout -q "$current_branch" 120 make -C .. clean >/dev/null 2>&1 121 make -C .. >/dev/null 2>&1 122 123 # Output combined results 124 if [ -n "$OUTFILE" ]; then 125 if [ "$APPEND" -eq 0 ]; then 126 csv_header > "$OUTFILE" 127 fi 128 echo "$results1" >> "$OUTFILE" 129 echo "$results2" >> "$OUTFILE" 130 echo "Results written to $OUTFILE" >&2 131 else 132 csv_header 133 echo "$results1" 134 echo "$results2" 135 fi 136 else 137 # Single run on current build 138 branch_name=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown") 139 140 if [ -n "$OUTFILE" ]; then 141 if [ "$APPEND" -eq 0 ]; then 142 csv_header > "$OUTFILE" 143 fi 144 run_benchmarks "$branch_name" >> "$OUTFILE" 145 echo "Results written to $OUTFILE" >&2 146 else 147 csv_header 148 run_benchmarks "$branch_name" 149 fi 150 fi