33# run_benchmarks.sh
44#
55# Description:
6- # Sends HTTP GETs to a running HTTPResponsivenessServer (started with --collect-benchmarks)
7- # then invokes collect-benchmarks .sh on its log.
6+ # Sends HTTP GETs to a running HTTPResponsivenessServer (started with --collect-benchmarks)
7+ # then invokes collect_benchmarks .sh on its log.
88#
99# Prerequisites:
10- # 1. Build your server with benchmarking support :
10+ # 1. Build your server:
1111# swift build -c release
1212#
1313# 2. Start it in the background, redirecting stdout+stderr to a log:
14- # .build/release/HTTPResponsivenessServer \
15- # --host 127.0.0.1 \
16- # --insecure-port 8080 \
17- # --collect-benchmarks \
18- # > server.log 2>&1 &
14+ #
15+ # stdbuf -oL -eL .build/release/HTTPResponsivenessServer \
16+ # --host 127.0.0.1 \
17+ # --insecure-port 8080 \
18+ # --collect-benchmarks \
19+ # &> server.log
1920#
2021# Usage:
2122# In the root directory of this App
22- #
23- # Examples/ping-benchmarks/run_benchmarks.sh \
24- # --host 127.0.0.1 \
25- # --port 8080 \
26- # --endpoint /ping \
27- # --samples 100 \
28- # --logfile server.log
23+ # Examples/ping-benchmarks/run_benchmarks.sh \
24+ # --host 127.0.0.1 \
25+ # --port 8080 \
26+ # --endpoint /ping \
27+ # --samples 100 \
28+ # --logfile server.log
2929#
3030
3131set -euo pipefail
@@ -38,17 +38,53 @@ while [[ $# -gt 0 ]]; do
3838 --endpoint) ENDPOINT=" $2 " ; shift 2 ;;
3939 --samples) SAMPLES=" $2 " ; shift 2 ;;
4040 --logfile) LOGFILE=" $2 " ; shift 2 ;;
41- * ) echo " Unknown argument: $1 " >&2 ; exit 1 ;;
41+ * )
42+ echo " Unknown argument: $1 " >&2
43+ exit 1
44+ ;;
4245 esac
4346done
4447
48+ # --- sanity check logfile ---
49+ if [[ ! -f " $LOGFILE " ]]; then
50+ cat << EOF >&2
51+ ERROR: logfile '$LOGFILE ' not found.
52+ Make sure you started the server with:
53+
54+ stdbuf -oL -eL .build/release/HTTPResponsivenessServer --host $HOST --insecure-port $PORT --collect-benchmarks &> $LOGFILE
55+
56+ EOF
57+ exit 1
58+ fi
59+
60+ # --- clear out any old measurements ---
61+ : > " $LOGFILE "
62+
4563echo " → Sending $SAMPLES requests to http://$HOST :$PORT$ENDPOINT "
4664for i in $( seq 1 " $SAMPLES " ) ; do
4765 curl -s " http://$HOST :$PORT$ENDPOINT " > /dev/null
4866done
4967
50- # give the server a moment to flush
68+ # give the server a moment to log
5169sleep 0.1
5270
53- # hand off to the collector
71+ # --- wait for exactly SAMPLES log entries ---
72+ echo " → Waiting up to 5s for $SAMPLES entries in '$LOGFILE ' …"
73+ deadline=$(( SECONDS + 5 ))
74+ while (( SECONDS < deadline )) ; do
75+ count=$( grep -c " Request handled in" " $LOGFILE " || true)
76+ if (( count >= SAMPLES )) ; then
77+ break
78+ fi
79+ sleep 0.1
80+ done
81+
82+ # final check
83+ count=$( grep -c " Request handled in" " $LOGFILE " || true)
84+ if (( count < SAMPLES )) ; then
85+ echo " ERROR: only found $count samples in '$LOGFILE ' (wanted $SAMPLES )" >&2
86+ exit 1
87+ fi
88+
89+ echo " → Collected $count samples, now computing percentiles…"
5490Examples/ping-benchmarks/collect_benchmarks.sh " $LOGFILE " " $ENDPOINT " " $SAMPLES "
0 commit comments