Skip to content

Commit a8d4642

Browse files
authored
fix(perf): wait for gateway WebSocket readiness (#117655)
* fix(perf): wait for gateway WebSocket readiness * fix(perf): pace gateway readiness retries
1 parent a3bdc53 commit a8d4642

2 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎.github/workflows/openclaw-performance.yml‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,12 +744,16 @@ jobs:
744744
done
745745
746746
gateway_home="$(mktemp -d)"
747+
gateway_readiness_home="$(mktemp -d)"
747748
gateway_port="$(node -e "const net=require('node:net'); const s=net.createServer(); s.listen(0,'127.0.0.1',()=>{ console.log(s.address().port); s.close(); });")"
748749
gateway_state="$gateway_home/.openclaw"
749750
gateway_config="$gateway_state/openclaw.json"
751+
gateway_readiness_state="$gateway_readiness_home/.openclaw"
752+
gateway_readiness_config="$gateway_readiness_state/openclaw.json"
750753
gateway_log="$SOURCE_PERF_DIR/cli-gateway.log"
754+
gateway_readiness_log="$SOURCE_PERF_DIR/cli-gateway-readiness.log"
751755
gateway_pid=""
752-
mkdir -p "$gateway_state"
756+
mkdir -p "$gateway_state" "$gateway_readiness_state"
753757
cat > "$gateway_config" <<EOF
754758
{
755759
"browser": { "enabled": false },
@@ -769,12 +773,14 @@ jobs:
769773
}
770774
}
771775
EOF
776+
cp "$gateway_config" "$gateway_readiness_config"
777+
: > "$gateway_readiness_log"
772778
cleanup_gateway() {
773779
if [[ -n "${gateway_pid:-}" ]] && kill -0 "$gateway_pid" 2>/dev/null; then
774780
kill "$gateway_pid" 2>/dev/null || true
775781
wait "$gateway_pid" 2>/dev/null || true
776782
fi
777-
rm -rf "$gateway_home"
783+
rm -rf "$gateway_home" "$gateway_readiness_home"
778784
}
779785
trap cleanup_gateway EXIT
780786
OPENCLAW_HOME="$gateway_home" OPENCLAW_STATE_DIR="$gateway_state" OPENCLAW_CONFIG_PATH="$gateway_config" OPENCLAW_GATEWAY_PORT="$gateway_port" OPENCLAW_SKIP_CHANNELS=1 \
@@ -807,6 +813,35 @@ jobs:
807813
sleep 1
808814
done
809815
816+
# Exercise the real WebSocket handshake and health RPC without warming benchmark device state.
817+
while true; do
818+
gateway_ready_remaining=$((gateway_ready_deadline - SECONDS))
819+
if (( gateway_ready_remaining <= 0 )); then
820+
cat "$gateway_log" >&2
821+
cat "$gateway_readiness_log" >&2
822+
echo "Timed out after ${gateway_ready_timeout_seconds}s waiting for gateway WebSocket health." >&2
823+
exit 1
824+
fi
825+
gateway_ready_remaining_ms=$((gateway_ready_remaining * 1000))
826+
if OPENCLAW_HOME="$gateway_readiness_home" OPENCLAW_STATE_DIR="$gateway_readiness_state" OPENCLAW_CONFIG_PATH="$gateway_readiness_config" \
827+
node dist/entry.js gateway health \
828+
--port "$gateway_port" \
829+
--timeout "$gateway_ready_remaining_ms" \
830+
--json >"$gateway_readiness_log" 2>&1; then
831+
break
832+
fi
833+
if ! kill -0 "$gateway_pid" 2>/dev/null; then
834+
cat "$gateway_log" >&2
835+
cat "$gateway_readiness_log" >&2
836+
exit 1
837+
fi
838+
# Pace fast transport failures without extending the shared startup deadline.
839+
gateway_ready_remaining=$((gateway_ready_deadline - SECONDS))
840+
if (( gateway_ready_remaining > 0 )); then
841+
sleep 1
842+
fi
843+
done
844+
810845
OPENCLAW_HOME="$gateway_home" OPENCLAW_STATE_DIR="$gateway_state" OPENCLAW_CONFIG_PATH="$gateway_config" OPENCLAW_GATEWAY_PORT="$gateway_port" \
811846
node --import tsx scripts/bench-cli-startup.ts \
812847
--case gatewayHealthJsonConnected \

‎test/scripts/openclaw-performance-workflow.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,15 @@ describe("OpenClaw performance workflow", () => {
239239
].join("\n");
240240
const boundedProbe =
241241
'curl -fsS --connect-timeout 2 --max-time "$gateway_probe_timeout" "http://127.0.0.1:${gateway_port}/healthz"';
242+
const websocketTimeout = "gateway_ready_remaining_ms=$((gateway_ready_remaining * 1000))";
243+
const websocketProbe = "node dist/entry.js gateway health \\";
244+
const websocketRetryDelay = [
245+
" gateway_ready_remaining=$((gateway_ready_deadline - SECONDS))",
246+
" if (( gateway_ready_remaining > 0 )); then",
247+
" sleep 1",
248+
" fi",
249+
].join("\n");
250+
const benchmark = "node --import tsx scripts/bench-cli-startup.ts \\";
242251

243252
expect(run).toContain("gateway_ready_timeout_seconds=120");
244253
expect(run).toContain("gateway_probe_timeout_seconds=5");
@@ -247,11 +256,37 @@ describe("OpenClaw performance workflow", () => {
247256
expect(run).toContain(deadlineFailure);
248257
expect(run).toContain(probeCap);
249258
expect(run).toContain(boundedProbe);
259+
expect(run).toContain(websocketTimeout);
260+
expect(run).toContain(websocketProbe);
261+
expect(run).toContain('--port "$gateway_port" \\');
262+
expect(run).toContain('--timeout "$gateway_ready_remaining_ms" \\');
263+
expect(run).toContain('--json >"$gateway_readiness_log" 2>&1; then');
264+
expect(run).toContain(websocketRetryDelay);
265+
expect(run).toContain(
266+
"Timed out after ${gateway_ready_timeout_seconds}s waiting for gateway WebSocket health.",
267+
);
250268
expect(run.split("/healthz")).toHaveLength(2);
251269
expect(run.indexOf(deadline)).toBeLessThan(run.indexOf(remaining));
252270
expect(run.indexOf(remaining)).toBeLessThan(run.indexOf(deadlineFailure));
253271
expect(run.indexOf(deadlineFailure)).toBeLessThan(run.indexOf(probeCap));
254272
expect(run.indexOf(probeCap)).toBeLessThan(run.indexOf(boundedProbe));
273+
expect(run.indexOf(boundedProbe)).toBeLessThan(run.indexOf(websocketTimeout));
274+
expect(run.indexOf(websocketTimeout)).toBeLessThan(run.indexOf(websocketProbe));
275+
const websocketRetryDelayIndex = run.indexOf(websocketRetryDelay, run.indexOf(websocketProbe));
276+
expect(websocketRetryDelayIndex).toBeGreaterThan(run.indexOf(websocketProbe));
277+
expect(websocketRetryDelayIndex).toBeLessThan(run.indexOf(benchmark));
278+
});
279+
280+
it("isolates gateway readiness identity from benchmark device state", () => {
281+
const run = findStep("Run OpenClaw source performance probes", "source_performance").run ?? "";
282+
283+
expect(run).toContain('gateway_readiness_home="$(mktemp -d)"');
284+
expect(run).toContain('gateway_readiness_state="$gateway_readiness_home/.openclaw"');
285+
expect(run).toContain('gateway_readiness_config="$gateway_readiness_state/openclaw.json"');
286+
expect(run).toContain('mkdir -p "$gateway_state" "$gateway_readiness_state"');
287+
expect(run).toContain('cp "$gateway_config" "$gateway_readiness_config"');
288+
expect(run).toContain(': > "$gateway_readiness_log"');
289+
expect(run).toContain('rm -rf "$gateway_home" "$gateway_readiness_home"');
255290
});
256291

257292
it("measures warmed and first-device gateway health separately", () => {

0 commit comments

Comments
 (0)