refactor(benchmark): restart bench server after each run

This commit is contained in:
yusing
2026-01-03 12:54:18 +08:00
parent b51d280b29
commit c026a0df7c
2 changed files with 98 additions and 86 deletions

View File

@@ -125,9 +125,9 @@ dev-build: build
benchmark: benchmark:
@if [ -z "$(TARGET)" ]; then \ @if [ -z "$(TARGET)" ]; then \
docker compose -f dev.compose.yml up -d --force-recreate whoami godoxy traefik caddy nginx; \ docker compose -f dev.compose.yml up -d --force-recreate godoxy traefik caddy nginx; \
else \ else \
docker compose -f dev.compose.yml up -d --force-recreate whoami $(TARGET); \ docker compose -f dev.compose.yml up -d --force-recreate $(TARGET); \
fi fi
sleep 1 sleep 1
@./scripts/benchmark.sh @./scripts/benchmark.sh

View File

@@ -9,7 +9,7 @@ HOST="bench.domain.com"
DURATION="${DURATION:-10s}" DURATION="${DURATION:-10s}"
THREADS="${THREADS:-4}" THREADS="${THREADS:-4}"
CONNECTIONS="${CONNECTIONS:-100}" CONNECTIONS="${CONNECTIONS:-100}"
TARGET="${TARGET:-}" TARGET="${TARGET-}"
# Color functions for output # Color functions for output
red() { echo -e "\033[0;31m$*\033[0m"; } red() { echo -e "\033[0;31m$*\033[0m"; }
@@ -18,25 +18,25 @@ yellow() { echo -e "\033[1;33m$*\033[0m"; }
blue() { echo -e "\033[0;34m$*\033[0m"; } blue() { echo -e "\033[0;34m$*\033[0m"; }
# Check if wrk is installed # Check if wrk is installed
if ! command -v wrk &> /dev/null; then if ! command -v wrk &>/dev/null; then
red "Error: wrk is not installed" red "Error: wrk is not installed"
echo "Please install wrk:" echo "Please install wrk:"
echo " Ubuntu/Debian: sudo apt-get install wrk" echo " Ubuntu/Debian: sudo apt-get install wrk"
echo " macOS: brew install wrk" echo " macOS: brew install wrk"
echo " Or build from source: https://github.com/wg/wrk" echo " Or build from source: https://github.com/wg/wrk"
exit 1 exit 1
fi fi
if ! command -v h2load &> /dev/null; then if ! command -v h2load &>/dev/null; then
red "Error: h2load is not installed" red "Error: h2load is not installed"
echo "Please install h2load (nghttp2-client):" echo "Please install h2load (nghttp2-client):"
echo " Ubuntu/Debian: sudo apt-get install nghttp2-client" echo " Ubuntu/Debian: sudo apt-get install nghttp2-client"
echo " macOS: brew install nghttp2" echo " macOS: brew install nghttp2"
exit 1 exit 1
fi fi
OUTFILE="/tmp/reverse_proxy_benchmark_$(date +%Y%m%d_%H%M%S).log" OUTFILE="/tmp/reverse_proxy_benchmark_$(date +%Y%m%d_%H%M%S).log"
: > "$OUTFILE" : >"$OUTFILE"
exec > >(tee -a "$OUTFILE") 2>&1 exec > >(tee -a "$OUTFILE") 2>&1
blue "========================================" blue "========================================"
@@ -48,16 +48,16 @@ echo "Duration: $DURATION"
echo "Threads: $THREADS" echo "Threads: $THREADS"
echo "Connections: $CONNECTIONS" echo "Connections: $CONNECTIONS"
if [ -n "$TARGET" ]; then if [ -n "$TARGET" ]; then
echo "Filter: $TARGET" echo "Filter: $TARGET"
fi fi
echo "" echo ""
# Define services to test # Define services to test
declare -A services=( declare -A services=(
["GoDoxy"]="http://127.0.0.1:8080" ["GoDoxy"]="http://127.0.0.1:8080"
["Traefik"]="http://127.0.0.1:8081" ["Traefik"]="http://127.0.0.1:8081"
["Caddy"]="http://127.0.0.1:8082" ["Caddy"]="http://127.0.0.1:8082"
["Nginx"]="http://127.0.0.1:8083" ["Nginx"]="http://127.0.0.1:8083"
) )
# Array to store connection errors # Array to store connection errors
@@ -65,39 +65,39 @@ declare -a connection_errors=()
# Function to test connection before benchmarking # Function to test connection before benchmarking
test_connection() { test_connection() {
local name=$1 local name=$1
local url=$2 local url=$2
yellow "Testing connection to $name..." yellow "Testing connection to $name..."
# Test HTTP/1.1 # Test HTTP/1.1
local res1=$(curl -sS -w "\n%{http_code}" --http1.1 -H "Host: $HOST" --max-time 5 "$url") local res1=$(curl -sS -w "\n%{http_code}" --http1.1 -H "Host: $HOST" --max-time 5 "$url")
local body1=$(echo "$res1" | head -n -1) local body1=$(echo "$res1" | head -n -1)
local status1=$(echo "$res1" | tail -n 1) local status1=$(echo "$res1" | tail -n 1)
# Test HTTP/2 # Test HTTP/2
local res2=$(curl -sS -w "\n%{http_code}" --http2-prior-knowledge -H "Host: $HOST" --max-time 5 "$url") local res2=$(curl -sS -w "\n%{http_code}" --http2-prior-knowledge -H "Host: $HOST" --max-time 5 "$url")
local body2=$(echo "$res2" | head -n -1) local body2=$(echo "$res2" | head -n -1)
local status2=$(echo "$res2" | tail -n 1) local status2=$(echo "$res2" | tail -n 1)
local failed=false local failed=false
if [ "$status1" != "200" ] || [ ${#body1} -ne 4096 ]; then if [ "$status1" != "200" ] || [ ${#body1} -ne 4096 ]; then
red "$name failed HTTP/1.1 connection test (Status: $status1, Body length: ${#body1})" red "$name failed HTTP/1.1 connection test (Status: $status1, Body length: ${#body1})"
failed=true failed=true
fi fi
if [ "$status2" != "200" ] || [ ${#body2} -ne 4096 ]; then if [ "$status2" != "200" ] || [ ${#body2} -ne 4096 ]; then
red "$name failed HTTP/2 connection test (Status: $status2, Body length: ${#body2})" red "$name failed HTTP/2 connection test (Status: $status2, Body length: ${#body2})"
failed=true failed=true
fi fi
if [ "$failed" = true ]; then if [ "$failed" = true ]; then
connection_errors+=("$name failed connection test (URL: $url)") connection_errors+=("$name failed connection test (URL: $url)")
return 1 return 1
else else
green "$name is reachable (HTTP/1.1 & HTTP/2)" green "$name is reachable (HTTP/1.1 & HTTP/2)"
return 0 return 0
fi fi
} }
blue "========================================" blue "========================================"
@@ -107,9 +107,9 @@ echo ""
# Run connection tests for all services # Run connection tests for all services
for name in "${!services[@]}"; do for name in "${!services[@]}"; do
if [ -z "$TARGET" ] || [ "${name,,}" = "${TARGET,,}" ]; then if [ -z "$TARGET" ] || [ "${name,,}" = "${TARGET,,}" ]; then
test_connection "$name" "${services[$name]}" test_connection "$name" "${services[$name]}"
fi fi
done done
echo "" echo ""
@@ -117,14 +117,14 @@ blue "========================================"
# Exit if any connection test failed # Exit if any connection test failed
if [ ${#connection_errors[@]} -gt 0 ]; then if [ ${#connection_errors[@]} -gt 0 ]; then
echo "" echo ""
red "Connection test failed for the following services:" red "Connection test failed for the following services:"
for error in "${connection_errors[@]}"; do for error in "${connection_errors[@]}"; do
red " - $error" red " - $error"
done done
echo "" echo ""
red "Please ensure all services are running before benchmarking" red "Please ensure all services are running before benchmarking"
exit 1 exit 1
fi fi
echo "" echo ""
@@ -133,44 +133,56 @@ echo ""
blue "========================================" blue "========================================"
echo "" echo ""
restart_bench() {
local name=$1
echo ""
yellow "Restarting bench service before benchmarking $name HTTP/1.1..."
docker compose -f dev.compose.yml up -d --force-recreate bench >/dev/null 2>&1
sleep 1
}
# Function to run benchmark # Function to run benchmark
run_benchmark() { run_benchmark() {
local name=$1 local name=$1
local url=$2 local url=$2
local h2_duration="${DURATION%s}" local h2_duration="${DURATION%s}"
yellow "Testing $name..." restart_bench "$name"
echo "========================================" yellow "Testing $name..."
echo "$name"
echo "URL: $url"
echo "========================================"
echo ""
echo "[HTTP/1.1] wrk"
wrk -t"$THREADS" -c"$CONNECTIONS" -d"$DURATION" \ echo "========================================"
-H "Host: $HOST" \ echo "$name"
"$url" echo "URL: $url"
echo "========================================"
echo ""
echo "[HTTP/1.1] wrk"
echo "" wrk -t"$THREADS" -c"$CONNECTIONS" -d"$DURATION" \
echo "[HTTP/2] h2load" -H "Host: $HOST" \
"$url"
h2load -t"$THREADS" -c"$CONNECTIONS" --duration="$h2_duration" \ restart_bench "$name"
-H "Host: $HOST" \
-H ":authority: $HOST" \
"$url" | grep -vE "^(starting benchmark...|spawning thread|progress: |Warm-up |Main benchmark duration|Stopped all clients|Process Request Failure)"
echo "" echo ""
green "$name benchmark completed" echo "[HTTP/2] h2load"
blue "----------------------------------------"
echo "" h2load -t"$THREADS" -c"$CONNECTIONS" --duration="$h2_duration" \
-H "Host: $HOST" \
-H ":authority: $HOST" \
"$url" | grep -vE "^(starting benchmark...|spawning thread|progress: |Warm-up |Main benchmark duration|Stopped all clients|Process Request Failure)"
echo ""
green "$name benchmark completed"
blue "----------------------------------------"
echo ""
} }
# Run benchmarks for each service # Run benchmarks for each service
for name in "${!services[@]}"; do for name in "${!services[@]}"; do
if [ -z "$TARGET" ] || [ "${name,,}" = "${TARGET,,}" ]; then if [ -z "$TARGET" ] || [ "${name,,}" = "${TARGET,,}" ]; then
run_benchmark "$name" "${services[$name]}" run_benchmark "$name" "${services[$name]}"
fi fi
done done
blue "========================================" blue "========================================"