1+ #! /bin/bash
2+
3+ # --- Colors for Output ---
4+ RED=' \033[0;31m'
5+ GREEN=' \033[0;32m'
6+ CYAN=' \033[0;36m'
7+ YELLOW=' \033[1;33m'
8+ BOLD=' \033[1m'
9+ NC=' \033[0m' # No Color
10+
11+ # --- Configuration & Args ---
12+ OLD_FILE=${1:- " benchmarks/old.txt" }
13+ NEW_FILE=${2:- " benchmarks/new.txt" }
14+
15+ # Validation
16+ if [[ ! -f " $OLD_FILE " || ! -f " $NEW_FILE " ]]; then
17+ echo " ${RED}${BOLD} Error:${NC} Files not found."
18+ echo " Usage: $0 [old_file] [new_file]"
19+ exit 1
20+ fi
21+
22+ # --- Extraction Logic ---
23+ # Extract metadata from the first file (assuming they are for the same scenario)
24+ SCENARIO=$( grep " ^scenario," " $OLD_FILE " | cut -d' ,' -f2)
25+ TASKS=$( grep " ^tasks," " $OLD_FILE " | cut -d' ,' -f2)
26+
27+ # Calculation Function via AWK
28+ # Returns: avg|count
29+ process_data () {
30+ awk -F' ,' ' /^measured build/ { sum += $2; count++ } END { if (count > 0) print sum/count "|" count; else print "0|0" }' " $1 "
31+ }
32+
33+ OLD_DATA=$( process_data " $OLD_FILE " )
34+ NEW_DATA=$( process_data " $NEW_FILE " )
35+
36+ OLD_AVG=$( echo " $OLD_DATA " | cut -d' |' -f1)
37+ OLD_CNT=$( echo " $OLD_DATA " | cut -d' |' -f2)
38+ NEW_AVG=$( echo " $NEW_DATA " | cut -d' |' -f1)
39+ NEW_CNT=$( echo " $NEW_DATA " | cut -d' |' -f2)
40+
41+ # --- Verbose Pretty Print ---
42+ echo " ${CYAN}${BOLD} =============================================================="
43+ echo " GRADLE BENCHMARK COMPARISON"
44+ echo " ==============================================================${NC} "
45+ printf " ${BOLD} %-12s${NC} %s\n" " Scenario:" " $SCENARIO "
46+ printf " ${BOLD} %-12s${NC} %s\n" " Tasks:" " $TASKS "
47+ echo " --------------------------------------------------------------"
48+
49+ # Table Header
50+ printf " ${BOLD} %-15s | %-12s | %-15s | %-10s${NC} \n" " Target" " Builds" " Average (ms)" " Minutes"
51+ echo " ----------------|--------------|-----------------|------------"
52+
53+ # Row function for reuse
54+ print_row () {
55+ local label=$1
56+ local cnt=$2
57+ local avg=$3
58+ # Calculate minutes/seconds inside AWK for the row
59+ local min_fmt=$( awk -v ms=" $avg " ' BEGIN { printf "%dm %05.2fs", int(ms/60000), (ms%60000)/1000 }' )
60+ printf " %-15s | %-12s | %-15.2f | %-10s\n" " $label " " $cnt " " $avg " " $min_fmt "
61+ }
62+
63+ print_row " Main" " $OLD_CNT " " $OLD_AVG "
64+ print_row " Optimized" " $NEW_CNT " " $NEW_AVG "
65+
66+ echo " ----------------|--------------|-----------------|------------"
67+
68+ # Final Comparison Logic
69+ awk -v old=" $OLD_AVG " -v new=" $NEW_AVG " \
70+ -v red=" $RED " -v grn=" $GREEN " -v yel=" $YELLOW " -v bld=" $BOLD " -v nc=" $NC " '
71+ BEGIN {
72+ diff = new - old
73+ pct = (old > 0) ? (diff / old) * 100 : 0
74+ abs_diff = (diff < 0) ? -diff : diff
75+
76+ # Format diff to minutes
77+ diff_min = sprintf("%dm %05.2fs", int(abs_diff/60000), (abs_diff%60000)/1000)
78+
79+ if (diff < -1) {
80+ printf "\n%sRESULT: IMPROVEMENT%s\n\n", grn bld, nc
81+ printf "The new build is %s%.2f ms (%s) faster%s\n", grn, abs_diff, diff_min, nc
82+ printf "Speedup: %s%.2f%%%s\n", grn, -pct, nc
83+ } else if (diff > 1) {
84+ printf "\n%sRESULT: REGRESSION%s\n", red bld, nc
85+ printf "The new build is %s%.2f ms (%s) slower%s\n", red, diff, diff_min, nc
86+ printf "Slowdown: %s+%.2f%%%s\n", red, pct, nc
87+ } else {
88+ printf "\n%sRESULT: NEGLIGIBLE CHANGE%s\n", yel, nc
89+ }
90+ print ""
91+ }'
0 commit comments