From c026a0df7c65e584d152289f7220f41bfc356200 Mon Sep 17 00:00:00 2001 From: yusing Date: Sat, 3 Jan 2026 12:54:18 +0800 Subject: [PATCH] refactor(benchmark): restart bench server after each run --- Makefile | 4 +- scripts/benchmark.sh | 180 +++++++++++++++++++++++-------------------- 2 files changed, 98 insertions(+), 86 deletions(-) diff --git a/Makefile b/Makefile index 627c10f9..2a5a805a 100755 --- a/Makefile +++ b/Makefile @@ -125,9 +125,9 @@ dev-build: build benchmark: @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 \ - docker compose -f dev.compose.yml up -d --force-recreate whoami $(TARGET); \ + docker compose -f dev.compose.yml up -d --force-recreate $(TARGET); \ fi sleep 1 @./scripts/benchmark.sh diff --git a/scripts/benchmark.sh b/scripts/benchmark.sh index 9a8bf165..c7fc0e7f 100644 --- a/scripts/benchmark.sh +++ b/scripts/benchmark.sh @@ -9,7 +9,7 @@ HOST="bench.domain.com" DURATION="${DURATION:-10s}" THREADS="${THREADS:-4}" CONNECTIONS="${CONNECTIONS:-100}" -TARGET="${TARGET:-}" +TARGET="${TARGET-}" # Color functions for output 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"; } # Check if wrk is installed -if ! command -v wrk &> /dev/null; then - red "Error: wrk is not installed" - echo "Please install wrk:" - echo " Ubuntu/Debian: sudo apt-get install wrk" - echo " macOS: brew install wrk" - echo " Or build from source: https://github.com/wg/wrk" - exit 1 +if ! command -v wrk &>/dev/null; then + red "Error: wrk is not installed" + echo "Please install wrk:" + echo " Ubuntu/Debian: sudo apt-get install wrk" + echo " macOS: brew install wrk" + echo " Or build from source: https://github.com/wg/wrk" + exit 1 fi -if ! command -v h2load &> /dev/null; then - red "Error: h2load is not installed" - echo "Please install h2load (nghttp2-client):" - echo " Ubuntu/Debian: sudo apt-get install nghttp2-client" - echo " macOS: brew install nghttp2" - exit 1 +if ! command -v h2load &>/dev/null; then + red "Error: h2load is not installed" + echo "Please install h2load (nghttp2-client):" + echo " Ubuntu/Debian: sudo apt-get install nghttp2-client" + echo " macOS: brew install nghttp2" + exit 1 fi OUTFILE="/tmp/reverse_proxy_benchmark_$(date +%Y%m%d_%H%M%S).log" -: > "$OUTFILE" +: >"$OUTFILE" exec > >(tee -a "$OUTFILE") 2>&1 blue "========================================" @@ -48,16 +48,16 @@ echo "Duration: $DURATION" echo "Threads: $THREADS" echo "Connections: $CONNECTIONS" if [ -n "$TARGET" ]; then - echo "Filter: $TARGET" + echo "Filter: $TARGET" fi echo "" # Define services to test declare -A services=( - ["GoDoxy"]="http://127.0.0.1:8080" - ["Traefik"]="http://127.0.0.1:8081" - ["Caddy"]="http://127.0.0.1:8082" - ["Nginx"]="http://127.0.0.1:8083" + ["GoDoxy"]="http://127.0.0.1:8080" + ["Traefik"]="http://127.0.0.1:8081" + ["Caddy"]="http://127.0.0.1:8082" + ["Nginx"]="http://127.0.0.1:8083" ) # Array to store connection errors @@ -65,39 +65,39 @@ declare -a connection_errors=() # Function to test connection before benchmarking test_connection() { - local name=$1 - local url=$2 + local name=$1 + local url=$2 - yellow "Testing connection to $name..." + yellow "Testing connection to $name..." - # Test HTTP/1.1 - 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 status1=$(echo "$res1" | tail -n 1) + # Test HTTP/1.1 + 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 status1=$(echo "$res1" | tail -n 1) - # Test HTTP/2 - 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 status2=$(echo "$res2" | tail -n 1) + # Test HTTP/2 + 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 status2=$(echo "$res2" | tail -n 1) - local failed=false - if [ "$status1" != "200" ] || [ ${#body1} -ne 4096 ]; then - red "✗ $name failed HTTP/1.1 connection test (Status: $status1, Body length: ${#body1})" - failed=true - fi + local failed=false + if [ "$status1" != "200" ] || [ ${#body1} -ne 4096 ]; then + red "✗ $name failed HTTP/1.1 connection test (Status: $status1, Body length: ${#body1})" + failed=true + fi - if [ "$status2" != "200" ] || [ ${#body2} -ne 4096 ]; then - red "✗ $name failed HTTP/2 connection test (Status: $status2, Body length: ${#body2})" - failed=true - fi + if [ "$status2" != "200" ] || [ ${#body2} -ne 4096 ]; then + red "✗ $name failed HTTP/2 connection test (Status: $status2, Body length: ${#body2})" + failed=true + fi - if [ "$failed" = true ]; then - connection_errors+=("$name failed connection test (URL: $url)") - return 1 - else - green "✓ $name is reachable (HTTP/1.1 & HTTP/2)" - return 0 - fi + if [ "$failed" = true ]; then + connection_errors+=("$name failed connection test (URL: $url)") + return 1 + else + green "✓ $name is reachable (HTTP/1.1 & HTTP/2)" + return 0 + fi } blue "========================================" @@ -107,9 +107,9 @@ echo "" # Run connection tests for all services for name in "${!services[@]}"; do - if [ -z "$TARGET" ] || [ "${name,,}" = "${TARGET,,}" ]; then - test_connection "$name" "${services[$name]}" - fi + if [ -z "$TARGET" ] || [ "${name,,}" = "${TARGET,,}" ]; then + test_connection "$name" "${services[$name]}" + fi done echo "" @@ -117,14 +117,14 @@ blue "========================================" # Exit if any connection test failed if [ ${#connection_errors[@]} -gt 0 ]; then - echo "" - red "Connection test failed for the following services:" - for error in "${connection_errors[@]}"; do - red " - $error" - done - echo "" - red "Please ensure all services are running before benchmarking" - exit 1 + echo "" + red "Connection test failed for the following services:" + for error in "${connection_errors[@]}"; do + red " - $error" + done + echo "" + red "Please ensure all services are running before benchmarking" + exit 1 fi echo "" @@ -133,44 +133,56 @@ echo "" blue "========================================" 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 run_benchmark() { - local name=$1 - local url=$2 - local h2_duration="${DURATION%s}" + local name=$1 + local url=$2 + local h2_duration="${DURATION%s}" - yellow "Testing $name..." + restart_bench "$name" - echo "========================================" - echo "$name" - echo "URL: $url" - echo "========================================" - echo "" - echo "[HTTP/1.1] wrk" + yellow "Testing $name..." - wrk -t"$THREADS" -c"$CONNECTIONS" -d"$DURATION" \ - -H "Host: $HOST" \ - "$url" + echo "========================================" + echo "$name" + echo "URL: $url" + echo "========================================" + echo "" + echo "[HTTP/1.1] wrk" - echo "" - echo "[HTTP/2] h2load" + wrk -t"$THREADS" -c"$CONNECTIONS" -d"$DURATION" \ + -H "Host: $HOST" \ + "$url" - 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)" + restart_bench "$name" - echo "" - green "✓ $name benchmark completed" - blue "----------------------------------------" - echo "" + echo "" + echo "[HTTP/2] h2load" + + 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 for name in "${!services[@]}"; do - if [ -z "$TARGET" ] || [ "${name,,}" = "${TARGET,,}" ]; then - run_benchmark "$name" "${services[$name]}" - fi + if [ -z "$TARGET" ] || [ "${name,,}" = "${TARGET,,}" ]; then + run_benchmark "$name" "${services[$name]}" + fi done blue "========================================"