|
| 1 | +/* |
| 2 | + Performance test: fixed arrival rate |
| 3 | +
|
| 4 | + Fires exactly `targetRps` event POST requests per second for `duration`, |
| 5 | + regardless of how long each request takes. This decouples load generation |
| 6 | + from response time, which is the correct model for finding the system's |
| 7 | + throughput ceiling. |
| 8 | +
|
| 9 | + If k6 cannot start iterations fast enough — because the system is overloaded, |
| 10 | + VUs are exhausted, or registration is too slow — `dropped_iterations` climbs |
| 11 | + above 0, telling you the target rate was not sustained. |
| 12 | +
|
| 13 | + How to find the ceiling: |
| 14 | + Start low (e.g. -e targetRps=10) and increase until you see either |
| 15 | + dropped_iterations > 0 or a sharp spike in p(95) POST latency. |
| 16 | + That inflection point is your system's throughput ceiling. |
| 17 | +
|
| 18 | + After the test, handleSummary() prints SQL queries with timestamps filled in: |
| 19 | + - SQL 1 ingest vs outbound counts (registered, queued, webhook responses) |
| 20 | + - SQL 2 pipeline latency (registration → OutboundQueue lag) |
| 21 | + - SQL 3 throughput per 5s bucket (registration, queuing, webhook delivery) |
| 22 | +
|
| 23 | + Command: |
| 24 | + podman compose run k6 run /src/tests/performance/fixed-rate.js \ |
| 25 | + -e altinn_env=yt01 \ |
| 26 | + -e tokenGeneratorUserName=autotest \ |
| 27 | + -e tokenGeneratorUserPwd=*** \ |
| 28 | + -e runId=$(date +%Y%m%d-%H%M%S) \ |
| 29 | + -e targetRps=20 \ |
| 30 | + -e duration=2m \ |
| 31 | + -e maxVus=50 \ |
| 32 | + -e queryOffsetMinutes=15 |
| 33 | +
|
| 34 | + Tuning preAllocatedVUs: |
| 35 | + k6 pre-warms this many VUs before the test starts. The rule of thumb is |
| 36 | + preAllocatedVUs ≈ targetRps × expected_p95_latency_in_seconds. |
| 37 | + This script defaults to targetRps × 2 (assumes up to 2s per request), |
| 38 | + capped by maxVus. Override maxVus if you need more headroom. |
| 39 | +*/ |
| 40 | + |
| 41 | +import { |
| 42 | + runId, performanceSetup, postCloudEvent, performanceTeardown, |
| 43 | + warnIfUntagged, getTimeWindow, getBaseMetrics, buildSummary, |
| 44 | +} from "./helpers.js"; |
| 45 | + |
| 46 | +const eventType = `performancetest.fixed-rate.${runId}`; |
| 47 | +const targetRps = Number.parseInt(__ENV.targetRps || "20", 10); |
| 48 | +const duration = __ENV.duration || "2m"; |
| 49 | +const maxVus = Number.parseInt(__ENV.maxVus || "50", 10); |
| 50 | + |
| 51 | +// Pre-allocate enough VUs to sustain targetRps assuming up to ~2s per request. |
| 52 | +// k6 will spin up more (up to maxVus) if needed mid-test. |
| 53 | +const preAllocatedVUs = Math.min(targetRps * 2, maxVus); |
| 54 | + |
| 55 | +warnIfUntagged(); |
| 56 | + |
| 57 | +export const options = { |
| 58 | + thresholds: { |
| 59 | + errors: ["count<1"], |
| 60 | + http_req_duration: ["p(95)<5000"], |
| 61 | + dropped_iterations: ["count<1"], |
| 62 | + }, |
| 63 | + scenarios: { |
| 64 | + fixed_rate: { |
| 65 | + executor: "constant-arrival-rate", |
| 66 | + rate: targetRps, |
| 67 | + timeUnit: "1s", |
| 68 | + duration: duration, |
| 69 | + preAllocatedVUs: preAllocatedVUs, |
| 70 | + maxVUs: maxVus, |
| 71 | + }, |
| 72 | + }, |
| 73 | +}; |
| 74 | + |
| 75 | +export function setup() { |
| 76 | + return performanceSetup(`Target rate: ${targetRps} events/s for ${duration} (preAllocated: ${preAllocatedVUs}, max: ${maxVus} VUs)`); |
| 77 | +} |
| 78 | + |
| 79 | +export default function runTests(data) { |
| 80 | + postCloudEvent(eventType, data.token); |
| 81 | +} |
| 82 | + |
| 83 | +export function teardown(data) { |
| 84 | + performanceTeardown(data); |
| 85 | +} |
| 86 | + |
| 87 | +export function handleSummary(data) { |
| 88 | + const { durationSec } = getTimeWindow(data); |
| 89 | + const { successCount, errorCount, p95Ms } = getBaseMetrics(data); |
| 90 | + const droppedCount = data.metrics["dropped_iterations"]?.values?.count ?? 0; |
| 91 | + const effectiveRps = durationSec > 0 ? (successCount / durationSec).toFixed(1) : "N/A"; |
| 92 | + const targetMet = droppedCount === 0 ? "YES" : `NO — ${droppedCount} iterations dropped`; |
| 93 | + |
| 94 | + return buildSummary("Fixed-Rate Throughput Test — Summary ║", [ |
| 95 | + " k6 results:", |
| 96 | + ` Target rate : ${targetRps} events/s`, |
| 97 | + ` Effective rate : ${effectiveRps} events/s`, |
| 98 | + ` Target rate sustained : ${targetMet}`, |
| 99 | + ` Events posted (HTTP 200) : ${successCount}`, |
| 100 | + ` Errors : ${errorCount}`, |
| 101 | + ` Dropped iterations : ${droppedCount}`, |
| 102 | + ` p(95) POST latency : ${p95Ms.toFixed(0)} ms`, |
| 103 | + ], eventType, data); |
| 104 | +} |
0 commit comments