-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbaremetal.sh
More file actions
executable file
·286 lines (246 loc) · 11.4 KB
/
baremetal.sh
File metadata and controls
executable file
·286 lines (246 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env bash
# =============================================================================
# baremetal.sh — Bare-metal benchmark: static-web vs Bun
#
# Builds static-web from source, benchmarks two servers on the same port one
# at a time, then prints a head-to-head comparison. No Docker.
#
# Configurations tested:
# 1. static-web --preload --gc-percent 400 (production optimised)
# 2. Bun native static HTML server
#
# Usage:
# ./benchmark/baremetal.sh [OPTIONS]
#
# Options:
# -c <int> Connections (default: 50)
# -n <int> Total requests (default: 100000)
# -d <int> Duration seconds — overrides -n when set
# -p <int> Port to use (default: 8080)
# -r <dir> Root directory (default: ./public)
# -h Show this help
#
# Requirements:
# - go (to build static-web)
# - bun (https://bun.sh)
# - bombardier (https://github.com/codesenberg/bombardier)
# =============================================================================
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
RESULTS_DIR="${SCRIPT_DIR}/results"
# ---------- defaults ---------------------------------------------------------
CONNECTIONS=100
REQUESTS=100000
DURATION=""
PORT=8080
ROOT_DIR="./public"
WARMUP_REQUESTS=50000
SETTLE_SECONDS=3 # pause between server start and warmup
# ---------- colours ----------------------------------------------------------
RED='\033[0;31m'; YELLOW='\033[1;33m'; GREEN='\033[0;32m'
CYAN='\033[0;36m'; BOLD='\033[1m'; DIM='\033[2m'; RESET='\033[0m'
# ---------- arg parse --------------------------------------------------------
usage() {
grep '^#' "$0" | grep -v '^#!/' | sed 's/^# \{0,2\}//'
exit 0
}
while getopts "c:n:d:p:r:h" opt; do
case $opt in
c) CONNECTIONS="$OPTARG" ;;
n) REQUESTS="$OPTARG" ;;
d) DURATION="$OPTARG" ;;
p) PORT="$OPTARG" ;;
r) ROOT_DIR="$OPTARG" ;;
h) usage ;;
*) echo "Unknown option -$OPTARG"; exit 1 ;;
esac
done
# ---------- dependency checks ------------------------------------------------
check_deps() {
local missing=""
command -v go >/dev/null 2>&1 || missing="$missing go"
command -v bun >/dev/null 2>&1 || missing="$missing bun"
command -v bombardier >/dev/null 2>&1 || missing="$missing bombardier"
if [ -n "$missing" ]; then
echo -e "${RED}Missing dependencies:${missing}${RESET}"
echo ""
echo "Install bombardier: brew install bombardier"
echo "Install bun: curl -fsSL https://bun.sh/install | bash"
exit 1
fi
}
# ---------- helpers ----------------------------------------------------------
BIN="${PROJECT_ROOT}/static-web"
SERVER_PID=""
cleanup() {
if [ -n "$SERVER_PID" ] && kill -0 "$SERVER_PID" 2>/dev/null; then
kill "$SERVER_PID" 2>/dev/null
wait "$SERVER_PID" 2>/dev/null || true
fi
}
trap cleanup EXIT
wait_for_port() {
local port=$1 max=15 i=0
while ! curl -sf -o /dev/null "http://localhost:${port}/" 2>/dev/null; do
sleep 0.5
i=$((i + 1))
if [ "$i" -ge "$max" ]; then
echo -e " ${RED}TIMEOUT${RESET}"
return 1
fi
done
}
kill_on_port() {
local pids
pids=$(lsof -ti :"$1" 2>/dev/null || true)
if [ -n "$pids" ]; then
echo "$pids" | xargs kill -9 2>/dev/null || true
sleep 1
fi
}
run_bombardier() {
local url=$1
if [ -n "$DURATION" ]; then
bombardier -c "$CONNECTIONS" -d "${DURATION}s" -l --print r "$url" 2>/dev/null
else
bombardier -c "$CONNECTIONS" -n "$REQUESTS" -l --print r "$url" 2>/dev/null
fi
}
parse_rps() { awk '/Reqs\/sec/{print $2; exit}'; }
parse_p50() { awk '/50\%/{print $2; exit}'; }
parse_p99() { awk '/99\%/{print $2; exit}'; }
parse_tp() { awk '/Throughput/{print $2; exit}'; }
# ---------- main -------------------------------------------------------------
main() {
check_deps
mkdir -p "$RESULTS_DIR"
# Resolve root to absolute path
local abs_root
abs_root="$(cd "$PROJECT_ROOT" && cd "$ROOT_DIR" 2>/dev/null && pwd)" || {
echo -e "${RED}Root directory not found: ${ROOT_DIR}${RESET}"
exit 1
}
echo ""
echo -e "${BOLD}╔════════════════════════════════════════════════════════════════════╗${RESET}"
echo -e "${BOLD}║ Bare-Metal Benchmark: static-web vs Bun ║${RESET}"
echo -e "${BOLD}╚════════════════════════════════════════════════════════════════════╝${RESET}"
echo ""
if [ -n "$DURATION" ]; then
echo -e " ${CYAN}Mode: duration ${DURATION}s${RESET}"
else
echo -e " ${CYAN}Mode: ${REQUESTS} requests${RESET}"
fi
echo -e " ${CYAN}Connections: ${CONNECTIONS}${RESET}"
echo -e " ${CYAN}Warmup: ${WARMUP_REQUESTS} requests${RESET}"
echo -e " ${CYAN}Port: ${PORT}${RESET}"
echo -e " ${CYAN}Root: ${abs_root}${RESET}"
echo -e " ${CYAN}Tool: $(bombardier --version 2>&1 | head -1)${RESET}"
echo -e " ${CYAN}Go: $(go version | awk '{print $3}')${RESET}"
echo -e " ${CYAN}Bun: $(bun --version)${RESET}"
echo -e " ${CYAN}Date: $(date -u '+%Y-%m-%d %H:%M:%S UTC')${RESET}"
echo -e " ${CYAN}OS/Arch: $(uname -s)/$(uname -m)${RESET}"
echo ""
# ---- use pre-built static-web binary -------------------------------------
echo -e "${BOLD}→ Using pre-built static-web: ${BIN}${RESET}"
if [ ! -x "$BIN" ]; then
echo -e "${RED}Binary not found or not executable: ${BIN}${RESET}"
echo -e "${RED}Run: go build -ldflags=\"-s -w\" -o static-web ./cmd/static-web${RESET}"
exit 1
fi
echo ""
# Make sure port is free
kill_on_port "$PORT"
local URL="http://localhost:${PORT}/index.html"
# Result arrays (indexed: 0=preload, 1=bun)
local -a NAMES RPS_ARR P50_ARR P99_ARR TP_ARR
NAMES=("static-web (preload+gc400)" "Bun")
# ======================================================================
# Test 1: static-web --preload --gc-percent 400 --no-etag (production mode)
# ======================================================================
echo -e "${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}"
echo -e "${BOLD} [ static-web — production: --preload --gc-percent 400 --no-etag ]${RESET}"
echo -e "${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}"
"$BIN" --quiet --no-compress --no-etag --preload --gc-percent 400 --port "$PORT" "$abs_root" &
SERVER_PID=$!
sleep "$SETTLE_SECONDS"
wait_for_port "$PORT"
echo -e " ${GREEN}Server ready (PID ${SERVER_PID})${RESET}"
echo -e " ${DIM}Warming up (${WARMUP_REQUESTS} requests)...${RESET}"
bombardier -c "$CONNECTIONS" -n "$WARMUP_REQUESTS" --print i "$URL" >/dev/null 2>&1
echo -e " ${DIM}Settle (${SETTLE_SECONDS}s)...${RESET}"
sleep "$SETTLE_SECONDS"
echo -e " ${CYAN}Benchmarking...${RESET}"
local raw
raw=$(run_bombardier "$URL" | tee "${RESULTS_DIR}/baremetal-static-web-preload.txt")
echo ""
RPS_ARR[0]=$(echo "$raw" | parse_rps)
P50_ARR[0]=$(echo "$raw" | parse_p50)
P99_ARR[0]=$(echo "$raw" | parse_p99)
TP_ARR[0]=$(echo "$raw" | parse_tp)
kill "$SERVER_PID" 2>/dev/null; wait "$SERVER_PID" 2>/dev/null || true
SERVER_PID=""
sleep 1
kill_on_port "$PORT"
# ======================================================================
# Test 2: Bun static serve
# ======================================================================
echo -e "${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}"
echo -e "${BOLD} [ Bun — native static HTML server ]${RESET}"
echo -e "${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}"
(cd "$abs_root" && bun --port "$PORT" ./index.html) &
SERVER_PID=$!
sleep "$SETTLE_SECONDS"
wait_for_port "$PORT"
echo -e " ${GREEN}Server ready (PID ${SERVER_PID})${RESET}"
echo -e " ${DIM}Warming up (${WARMUP_REQUESTS} requests)...${RESET}"
bombardier -c "$CONNECTIONS" -n "$WARMUP_REQUESTS" --print i "$URL" >/dev/null 2>&1
echo -e " ${DIM}Settle (${SETTLE_SECONDS}s)...${RESET}"
sleep "$SETTLE_SECONDS"
echo -e " ${CYAN}Benchmarking...${RESET}"
raw=$(run_bombardier "$URL" | tee "${RESULTS_DIR}/baremetal-bun.txt")
echo ""
RPS_ARR[1]=$(echo "$raw" | parse_rps)
P50_ARR[1]=$(echo "$raw" | parse_p50)
P99_ARR[1]=$(echo "$raw" | parse_p99)
TP_ARR[1]=$(echo "$raw" | parse_tp)
kill "$SERVER_PID" 2>/dev/null; wait "$SERVER_PID" 2>/dev/null || true
SERVER_PID=""
# ======================================================================
# Rank results (descending by RPS — simple swap for 2 elements)
# ======================================================================
local -a SORTED_IDX=(0 1)
if awk "BEGIN{exit !(${RPS_ARR[1]} > ${RPS_ARR[0]})}" 2>/dev/null; then
SORTED_IDX=(1 0)
fi
# ======================================================================
# Print results table
# ======================================================================
echo ""
echo -e "${BOLD}╔════════════════════════════════════════════════════════════════════╗${RESET}"
echo -e "${BOLD}║ Bare-Metal Results ║${RESET}"
echo -e "${BOLD}╠════════════════════════════════════════════════════════════════════╣${RESET}"
printf "${BOLD}║ %-4s %-30s %10s %8s %8s ║${RESET}\n" \
"#" "Server" "Req/sec" "p50" "p99"
echo -e "${BOLD}╠════════════════════════════════════════════════════════════════════╣${RESET}"
local rank=1
for idx in "${SORTED_IDX[@]}"; do
local colour medal
if [ "$rank" -eq 1 ]; then
colour="$GREEN"; medal="1st"
else
colour="$YELLOW"; medal="2nd"
fi
printf "${colour}║ %-4s %-30s %10s %8s %8s ║${RESET}\n" \
"$medal" "${NAMES[$idx]}" "${RPS_ARR[$idx]}" "${P50_ARR[$idx]}" "${P99_ARR[$idx]}"
rank=$((rank + 1))
done
echo -e "${BOLD}╚════════════════════════════════════════════════════════════════════╝${RESET}"
echo ""
echo -e " ${DIM}Throughput:${RESET}"
echo -e " ${DIM} preload+gc400 ${TP_ARR[0]}${RESET}"
echo -e " ${DIM} Bun ${TP_ARR[1]}${RESET}"
echo -e " ${DIM}Results saved to: ${RESULTS_DIR}/baremetal-*.txt${RESET}"
echo ""
}
main "$@"