-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Expand file tree
/
Copy pathbenchmark-boot-time.sh
More file actions
executable file
·203 lines (176 loc) · 5.86 KB
/
Copy pathbenchmark-boot-time.sh
File metadata and controls
executable file
·203 lines (176 loc) · 5.86 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
#!/bin/bash
#
# Benchmark dev server boot time (wall-clock)
#
# Measures TWO metrics:
# 1. listen_time: When server starts accepting TCP connections
# 2. ready_time: When server responds to first HTTP request
#
# The delta between these shows how much initialization is deferred after "Ready".
#
# Usage:
# ./scripts/benchmark-boot-time.sh [runs] [test-dir]
#
# Examples:
# ./scripts/benchmark-boot-time.sh # 5 runs, uses /tmp/next-boot-test
# ./scripts/benchmark-boot-time.sh 3 # 3 runs
# ./scripts/benchmark-boot-time.sh 5 ./my-app # 5 runs on existing app
set -e
RUNS=${1:-5}
TEST_DIR=${2:-/tmp/next-boot-test}
NEXT_BIN="$(dirname "$0")/../packages/next/dist/bin/next"
PORT=3456
echo "=== Dev Server Boot Time Benchmark ==="
echo "Runs: $RUNS"
echo "Test dir: $TEST_DIR"
echo "Next.js: $NEXT_BIN"
echo ""
echo "Metrics:"
echo " listen_time: TCP port accepting connections"
echo " ready_time: First HTTP request succeeds"
echo " delta: ready_time - listen_time (deferred init)"
echo ""
# Create test app if it doesn't exist
if [ ! -f "$TEST_DIR/package.json" ]; then
echo "Creating test app..."
mkdir -p "$TEST_DIR/app"
cat > "$TEST_DIR/package.json" << 'EOF'
{
"name": "boot-test",
"private": true,
"dependencies": {
"react": "19.0.0",
"react-dom": "19.0.0"
}
}
EOF
cat > "$TEST_DIR/app/layout.tsx" << 'EOF'
export default function RootLayout({ children }: { children: React.ReactNode }) {
return <html><body>{children}</body></html>
}
EOF
cat > "$TEST_DIR/app/page.tsx" << 'EOF'
export default function Home() { return <h1>Hello</h1> }
EOF
(cd "$TEST_DIR" && npm install --silent)
# Link local next
(cd "$TEST_DIR" && npm link "$(dirname "$NEXT_BIN")/.." 2>/dev/null || true)
fi
# Kill any existing next dev on our port
pkill -f "next dev.*$PORT" 2>/dev/null || true
sleep 0.5
# Returns: listen_time,ready_time (comma-separated)
benchmark_run() {
local label=$1
local clean_next=$2
if [ "$clean_next" = "true" ]; then
rm -rf "$TEST_DIR/.next"
fi
# Measure wall-clock time from command start
local start_time=$(python3 -c 'import time; print(int(time.time() * 1000))')
"$NEXT_BIN" dev --turbopack --port $PORT "$TEST_DIR" > /dev/null 2>&1 &
local pid=$!
local timeout=600 # 30s at 50ms intervals
local listen_time=""
local ready_time=""
# Phase 1: Wait for port to be listening (nc -z)
for i in $(seq 1 $timeout); do
if nc -z localhost $PORT 2>/dev/null; then
listen_time=$(python3 -c 'import time; print(int(time.time() * 1000))')
break
fi
sleep 0.05
done
# Phase 2: Wait for HTTP response (curl)
if [ -n "$listen_time" ]; then
for i in $(seq 1 $timeout); do
if curl -s "http://localhost:$PORT" > /dev/null 2>&1; then
ready_time=$(python3 -c 'import time; print(int(time.time() * 1000))')
break
fi
sleep 0.05
done
fi
# Kill the server
kill $pid 2>/dev/null || true
wait $pid 2>/dev/null || true
if [ -n "$listen_time" ] && [ -n "$ready_time" ]; then
local listen_delta=$((listen_time - start_time))
local ready_delta=$((ready_time - start_time))
echo "$listen_delta,$ready_delta"
else
echo "TIMEOUT,TIMEOUT"
fi
}
run_benchmark_series() {
local series_name=$1
local clean_next=$2
echo "--- $series_name ---"
echo "Run | Listen | Ready | Delta"
echo "----|--------|-------|------"
local listen_times=""
local ready_times=""
local deltas=""
for i in $(seq 1 $RUNS); do
RESULT=$(benchmark_run "$series_name-$i" "$clean_next")
LISTEN=$(echo "$RESULT" | cut -d',' -f1)
READY=$(echo "$RESULT" | cut -d',' -f2)
if [ "$LISTEN" != "TIMEOUT" ] && [ "$READY" != "TIMEOUT" ]; then
DELTA=$((READY - LISTEN))
printf "%3d | %5dms | %5dms | %5dms\n" "$i" "$LISTEN" "$READY" "$DELTA"
listen_times="$listen_times $LISTEN"
ready_times="$ready_times $READY"
deltas="$deltas $DELTA"
else
printf "%3d | TIMEOUT | TIMEOUT | -\n" "$i"
fi
done
# Calculate averages
local listen_avg=$(echo $listen_times | tr ' ' '\n' | grep -v '^$' | awk '{sum+=$1; count++} END {if(count>0) printf "%.0f", sum/count; else print "N/A"}')
local ready_avg=$(echo $ready_times | tr ' ' '\n' | grep -v '^$' | awk '{sum+=$1; count++} END {if(count>0) printf "%.0f", sum/count; else print "N/A"}')
local delta_avg=$(echo $deltas | tr ' ' '\n' | grep -v '^$' | awk '{sum+=$1; count++} END {if(count>0) printf "%.0f", sum/count; else print "N/A"}')
echo ""
echo "Average: listen=${listen_avg}ms, ready=${ready_avg}ms, delta=${delta_avg}ms"
echo ""
# Export for summary
export "${series_name}_LISTEN_AVG=$listen_avg"
export "${series_name}_READY_AVG=$ready_avg"
export "${series_name}_DELTA_AVG=$delta_avg"
}
# Run cold start benchmarks
run_benchmark_series "COLD" true
# Warmup for bytecode cache
echo "--- Warming up bytecode cache (12s) ---"
"$NEXT_BIN" dev --turbopack --port $PORT "$TEST_DIR" > /dev/null 2>&1 &
WARMUP_PID=$!
for i in $(seq 1 200); do
if curl -s "http://localhost:$PORT" > /dev/null 2>&1; then
break
fi
sleep 0.05
done
sleep 12
kill $WARMUP_PID 2>/dev/null || true
wait $WARMUP_PID 2>/dev/null || true
echo ""
# Run warm start benchmarks
run_benchmark_series "WARM" false
# Summary
echo "=============================================="
echo " SUMMARY"
echo "=============================================="
echo ""
echo "Cold Start ($RUNS runs):"
echo " Port listening: ${COLD_LISTEN_AVG}ms"
echo " First request: ${COLD_READY_AVG}ms"
echo " Deferred init: ${COLD_DELTA_AVG}ms"
echo ""
echo "Warm Start ($RUNS runs):"
echo " Port listening: ${WARM_LISTEN_AVG}ms"
echo " First request: ${WARM_READY_AVG}ms"
echo " Deferred init: ${WARM_DELTA_AVG}ms"
echo ""
if [ "$COLD_READY_AVG" != "N/A" ] && [ "$WARM_READY_AVG" != "N/A" ]; then
CACHE_BENEFIT=$((COLD_READY_AVG - WARM_READY_AVG))
echo "Cache benefit: ${CACHE_BENEFIT}ms (cold - warm ready)"
fi