|
| 1 | +import http from "k6/http"; |
| 2 | +import { check, sleep } from "k6"; |
| 3 | +import { Trend } from "k6/metrics"; |
| 4 | +import { BASE_URL, authHeaders } from "../config.js"; |
| 5 | +import { roomName, senderName, uuid } from "../helpers.js"; |
| 6 | + |
| 7 | +const inboxPendingLatency = new Trend("inbox_pending_latency", true); |
| 8 | +const inboxAckLatency = new Trend("inbox_ack_latency", true); |
| 9 | + |
| 10 | +export const options = { |
| 11 | + scenarios: { |
| 12 | + inbox: { |
| 13 | + executor: "ramping-vus", |
| 14 | + startVUs: 1, |
| 15 | + stages: [ |
| 16 | + { duration: "10s", target: 10 }, |
| 17 | + { duration: "15s", target: 50 }, |
| 18 | + { duration: "10s", target: 0 }, |
| 19 | + ], |
| 20 | + }, |
| 21 | + }, |
| 22 | + thresholds: { |
| 23 | + inbox_pending_latency: ["p(95)<500"], |
| 24 | + inbox_ack_latency: ["p(95)<500"], |
| 25 | + http_req_failed: ["rate<0.05"], |
| 26 | + }, |
| 27 | +}; |
| 28 | + |
| 29 | +export default function () { |
| 30 | + const vuId = __VU; |
| 31 | + const room = roomName("inbox", vuId); |
| 32 | + const sender = senderName("inbox", vuId); |
| 33 | + const consumer = `bench-consumer-vu${vuId}`; |
| 34 | + const headers = authHeaders(); |
| 35 | + |
| 36 | + // Publish a message first |
| 37 | + const msgId = uuid(); |
| 38 | + const publishRes = http.post( |
| 39 | + `${BASE_URL}/api/v1/publish?room=${room}&sender=${sender}&id=${msgId}`, |
| 40 | + JSON.stringify({ kind: "bench.inbox", iteration: __ITER }), |
| 41 | + { headers }, |
| 42 | + ); |
| 43 | + |
| 44 | + check(publishRes, { |
| 45 | + "inbox publish 200": (r) => r.status === 200, |
| 46 | + }); |
| 47 | + |
| 48 | + // Get pending messages |
| 49 | + const pendingRes = http.get( |
| 50 | + `${BASE_URL}/api/v1/inbox/pending?room=${room}&consumer=${consumer}&limit=10`, |
| 51 | + { headers }, |
| 52 | + ); |
| 53 | + |
| 54 | + inboxPendingLatency.add(pendingRes.timings.duration); |
| 55 | + |
| 56 | + const pendingOk = check(pendingRes, { |
| 57 | + "pending status 200": (r) => r.status === 200, |
| 58 | + "pending has envelopes": (r) => { |
| 59 | + const body = r.json(); |
| 60 | + return body.ok === true && Array.isArray(body.envelopes); |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + // Ack the messages |
| 65 | + if (pendingOk) { |
| 66 | + const pending = pendingRes.json(); |
| 67 | + if (pending.envelopes && pending.envelopes.length > 0) { |
| 68 | + const ids = pending.envelopes.map((e) => e.id); |
| 69 | + const ackRes = http.post( |
| 70 | + `${BASE_URL}/api/v1/inbox/ack?room=${room}&consumer=${consumer}`, |
| 71 | + JSON.stringify({ ids }), |
| 72 | + { headers }, |
| 73 | + ); |
| 74 | + |
| 75 | + inboxAckLatency.add(ackRes.timings.duration); |
| 76 | + |
| 77 | + check(ackRes, { |
| 78 | + "ack status 200": (r) => r.status === 200, |
| 79 | + "ack ok": (r) => r.json().ok === true, |
| 80 | + }); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Verify pending is now empty (or reduced) |
| 85 | + const verifyRes = http.get( |
| 86 | + `${BASE_URL}/api/v1/inbox/pending?room=${room}&consumer=${consumer}&limit=10`, |
| 87 | + { headers }, |
| 88 | + ); |
| 89 | + |
| 90 | + check(verifyRes, { |
| 91 | + "verify pending 200": (r) => r.status === 200, |
| 92 | + }); |
| 93 | + |
| 94 | + sleep(2); |
| 95 | +} |
0 commit comments