Skip to content

Commit 5e59c8e

Browse files
committed
moved pinghandler to correct if-branch, fixed script to reliably log times
1 parent 6b9f990 commit 5e59c8e

File tree

3 files changed

+58
-22
lines changed

3 files changed

+58
-22
lines changed

http-responsiveness-server/Examples/ping-benchmarks/collect_benchmarks.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Reads “Request handled in XX ms” lines from a SwiftNIO HTTPResponsivenessServer
77
# log file (produced when started with --collect-benchmarks) and computes
88
# p0, p25, p50, p75, p90, p99 and p100 statistics.
9-
#
9+
#
1010
# Called from run_benchmarks.sh
1111
#
1212
#
@@ -24,7 +24,7 @@ SAMPLES="$3"
2424

2525
# extract exactly the first N timings
2626
mapfile -t times < <(
27-
grep "Request handled in" "$LOGFILE" \
27+
grep -a "Request handled in" "$LOGFILE" \
2828
| head -n "$SAMPLES" \
2929
| sed -E 's/.*Request handled in ([0-9]+(\.[0-9]+)?) ms/\1/'
3030
)

http-responsiveness-server/Examples/ping-benchmarks/run_benchmarks.sh

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@
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

3131
set -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
4346
done
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+
4563
echo "→ Sending $SAMPLES requests to http://$HOST:$PORT$ENDPOINT"
4664
for i in $(seq 1 "$SAMPLES"); do
4765
curl -s "http://$HOST:$PORT$ENDPOINT" > /dev/null
4866
done
4967

50-
# give the server a moment to flush
68+
# give the server a moment to log
5169
sleep 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…"
5490
Examples/ping-benchmarks/collect_benchmarks.sh "$LOGFILE" "$ENDPOINT" "$SAMPLES"

http-responsiveness-server/Sources/HTTPResponsivenessServer/main.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ final class PingHandler: ChannelInboundHandler {
9393
return
9494

9595
case .end where handlingPing:
96-
// ping is done; forward the .end so measurement & mux see it
96+
// ping is done
9797
handlingPing = false
9898
return
9999

@@ -180,8 +180,8 @@ func channelInitializer(
180180
.flatMapThrowing { _ in
181181
if collectBenchmarks {
182182
try channel.pipeline.syncOperations.addHandler(PerformanceMeasurementHandler())
183+
try channel.pipeline.syncOperations.addHandler(PingHandler())
183184
}
184-
try channel.pipeline.syncOperations.addHandler(PingHandler())
185185
try channel.pipeline.syncOperations.addHandler(
186186
SimpleResponsivenessRequestMux(responsivenessConfigBuffer: config)
187187
)

0 commit comments

Comments
 (0)