C.W.K.
Stream
Lesson 05 of 07 · published

Monitoring & Testing

~12 min · production, k6, metrics

Level 0Poller
0 XP0/60 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Manual tests with wscat / websocat

Two CLI tools dominate manual WebSocket testing. wscat (Node.js) is the friendliest. websocat (Rust) is the most powerful — supports custom headers, TLS, subprotocols, binary framing. Keep both in your toolbox.

Load tests with k6

k6 has a first-class WebSocket module. Write a JS test, ramp up virtual users, push thousands of concurrent connections to your server. The output covers connection rate, message throughput, error rate, and latency percentiles. Run it before every production change.

Five metrics to monitor in production

Active connections (count, by server). Messages per second (in/out). Connection duration distribution. Close-code histogram (1006 spikes = trouble). Heartbeat round-trip latency p95. Wire these into Prometheus or whatever your stack uses. Alarm on anomalies, not absolute thresholds.

Code

Manual testing with websocat·shell
# brew install websocat  (or: cargo install websocat)

# Connect, send, receive
websocat ws://localhost:8000/ws
> {"type":"ping"}
{"type":"pong"}

# With headers (auth token)
websocat -H "Authorization: Bearer xxx" wss://api.example.com/ws

# Binary frames
echo -n -e '\x01\x02\x03' | websocat -b ws://localhost:8000/ws
k6 load test·javascript
import ws from 'k6/ws';
import { check } from 'k6';

export const options = {
  stages: [
    { duration: '30s', target: 100 },   // ramp up
    { duration: '1m',  target: 1_000 }, // sustained
    { duration: '30s', target: 0 },     // ramp down
  ],
};

export default function () {
  const url = 'ws://localhost:8000/ws';
  const res = ws.connect(url, {}, (socket) => {
    socket.on('open', () => {
      socket.send(JSON.stringify({ type: 'ping' }));
    });
    socket.on('message', (msg) => {
      check(msg, { 'got pong': (m) => m.includes('pong') });
    });
    socket.setTimeout(() => socket.close(), 10_000);
  });
  check(res, { 'status is 101': (r) => r && r.status === 101 });
}

// Run: k6 run k6-websocket-test.js

External links

Exercise

Run a k6 load test that ramps to 1,000 concurrent WebSocket connections to your local server. Plot connection rate, message throughput, and error rate. Find your server's break point — the connection count at which errors start appearing. Note OS-level signals (fd count, memory) at the break point.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.