/** * Throughput and concurrency load tests for mytodos. * * The application is entirely Blade + Livewire with no API surface, so the * dominant real-world cost is the POST to Livewire's update endpoint rather * than the page GET that precedes it. Any test that only loads pages * understates load by roughly an order of magnitude, because a single page view * is normally followed by a burst of update round-trips: search is bound to a * 300ms debounce, folders render on expand, and toggles each commit. * * Snapshots are captured from real pages and replayed. They cannot be * synthesised, because Livewire signs every snapshot with * hash_hmac('sha256', json_encode($snapshot), APP_KEY). A generator that * invents snapshots is also self-defeating: the checksum guard locks out an IP * after ten bad payloads in ten minutes, which would invalidate the whole run. * * Run one scenario at a time so the numbers stay attributable: * * SCENARIOS=page_load VUS=25 DURATION=2m k6 run load-tests/app.js * * Then step VUS upward until the p95 breaches the SLO. The driver script * load-tests/ramp.sh does that sweep and collects the results. */ import http from 'k6/http'; import exec from 'k6/execution'; import { check } from 'k6'; import { Counter, Rate, Trend } from 'k6/metrics'; const BASE_URL = (__ENV.BASE_URL || 'http://mytodos.test').replace(/\/$/, ''); const VUS = parseInt(__ENV.VUS || '25', 10); const DURATION = __ENV.DURATION || '2m'; const LABEL = __ENV.LABEL || `vus-${VUS}`; const ALL_SCENARIOS = [ 'anon', 'page_load', 'livewire_read', 'livewire_write', 'folders_read', 'folders_write', 'admin', ]; const ENABLED = (__ENV.SCENARIOS || ALL_SCENARIOS.join(',')) .split(',') .map((name) => name.trim()) .filter((name) => ALL_SCENARIOS.indexOf(name) !== -1); if (ENABLED.length === 0) { throw new Error(`SCENARIOS must name at least one of: ${ALL_SCENARIOS.join(', ')}`); } /** * Read the session manifest written by `php artisan app:seed-load-test`. * * Each entry already carries the encrypted session cookie, because * EncryptCookies wraps cookie values in an HMAC prefix and then encrypts them. * A bare session id is rejected by the framework. */ const sessions = JSON.parse(open('./sessions.json')); const userSessions = sessions.filter((s) => !s.isAdmin); const adminSessions = sessions.filter((s) => s.isAdmin); /** * Pair a latency trend with a request counter. * * A Trend's summary values carry percentiles but no sample count, and the count * is what throughput is derived from, so each scenario needs both. */ function scenarioMetrics(name) { return { trend: new Trend(`${name}_latency`, true), counter: new Counter(`${name}_requests`), }; } const METRICS = { anon: scenarioMetrics('anon'), page_load: scenarioMetrics('page_load'), livewire_read: scenarioMetrics('livewire_read'), livewire_write: scenarioMetrics('livewire_write'), folders_read: scenarioMetrics('folders_read'), folders_write: scenarioMetrics('folders_write'), admin: scenarioMetrics('admin'), }; const failures = new Rate('functional_failures'); const serverErrors = new Counter('server_errors'); const checksumRejects = new Counter('checksum_rejects'); /** * Record one successful request against a scenario's metrics. */ function record(name, durationMs) { METRICS[name].trend.add(durationMs); METRICS[name].counter.add(1); failures.add(false); } // Service level objectives the ramp is looking for a breach of. const SLO_P95_MS = parseInt(__ENV.SLO_P95_MS || '1000', 10); const SLO_ERROR_RATE = parseFloat(__ENV.SLO_ERROR_RATE || '0.01'); export const options = { discardResponseBodies: false, // p99 is not in k6's default summary set, and it is the number that exposes // a tail growing long before the mean moves. summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(50)', 'p(90)', 'p(95)', 'p(99)'], scenarios: ENABLED.reduce((acc, name) => { acc[name] = { executor: 'constant-vus', vus: VUS, duration: DURATION, exec: name, tags: { scenario: name }, }; return acc; }, {}), thresholds: { functional_failures: [`rate<${SLO_ERROR_RATE}`], }, }; /** * Choose the session a VU always authenticates as. * * Pinning each VU to one account keeps a single session cookie hot for the * whole run and spreads load across accounts, which is closer to real use than * re-authenticating on every iteration. */ function sessionFor(pool, vuId) { if (pool.length === 0) { throw new Error('The session manifest has no usable sessions. Run php artisan app:seed-load-test first.'); } return pool[(vuId - 1) % pool.length]; } function authHeaders(session) { return { Cookie: `${session.cookieName}=${session.cookie}`, Accept: 'text/html,application/xhtml+xml', }; } function livewireHeaders(session, page) { return { Cookie: `${session.cookieName}=${session.cookie}`, 'Content-Type': 'application/json', 'X-Livewire': '1', 'X-CSRF-TOKEN': page.csrf, Accept: 'application/json', }; } /** * Pull the three values a Livewire round-trip needs out of a rendered page: * the signed snapshot, the CSRF token and the update endpoint URL. */ function parsePage(body) { const snapshot = /wire:snapshot="([^"]*)"/.exec(body); const csrf = /data-csrf="([^"]*)"/.exec(body); const uri = /data-update-uri="([^"]*)"/.exec(body); if (!snapshot || !csrf || !uri) { throw new Error('Could not find the Livewire snapshot, CSRF token or update URI in the response.'); } return { snapshot: htmlDecode(snapshot[1]), csrf: csrf[1], uri: uri[1], }; } /** * Blade escapes attribute values, so the snapshot arrives HTML-escaped inside * the wire:snapshot attribute. */ function htmlDecode(value) { return value .replace(/"/g, '"') .replace(/'/g, "'") .replace(/</g, '<') .replace(/>/g, '>') .replace(/&/g, '&'); } /** * Load a page and remember the session context for later update calls. * * Cached per VU because the update scenarios chain snapshots: each response * carries the next valid snapshot, so refetching every iteration would measure * page loads rather than the update path. */ function loadPage(pool, path) { const session = sessionFor(pool, exec.vu.idInTest); const response = http.get(BASE_URL + path, { headers: authHeaders(session), tags: { name: path, step: 'page' }, }); const ok = check(response, { 'page returned 200': (r) => r.status === 200, 'page is not the paywall redirect': (r) => r.status !== 302, }); if (!ok) { failures.add(true); serverErrors.add(1); throw new Error(`${path} returned ${response.status}`); } const page = parsePage(response.body); page.timing = response.timings.duration; return { session, path, ...page }; } /** * Post one Livewire update, chaining the returned snapshot for the next call. */ function sendUpdate(page, scenario, updates, calls) { const payload = JSON.stringify({ components: [ { snapshot: page.snapshot, updates: updates || {}, // Livewire 4 validates that every call carries a method string and a // params array, and rejects the whole payload with a 500 otherwise. calls: (calls || []).map((call) => ({ path: '', method: call.method, params: call.params || [], })), }, ], }); const response = http.post(page.uri, payload, { headers: livewireHeaders(page.session, page), tags: { name: page.path, step: 'update' }, }); if (response.status !== 200) { failures.add(true); serverErrors.add(1); if (response.status === 429) { checksumRejects.add(1); } throw new Error(`Livewire update to ${page.path} returned ${response.status}`); } let body; try { body = JSON.parse(response.body); } catch (e) { failures.add(true); throw new Error(`Livewire update to ${page.path} did not return JSON`); } const component = body.components && body.components[0]; if (!component || !component.snapshot) { failures.add(true); throw new Error(`Livewire update to ${page.path} returned no snapshot`); } // The response snapshot is the only one valid for the next request. page.snapshot = component.snapshot; record(scenario, response.timings.duration); return response; } export function anon() { for (const path of ['/', '/pricing']) { const response = http.get(BASE_URL + path, { tags: { name: path } }); const ok = check(response, { 'anonymous page returned 200': (r) => r.status === 200 }); if (!ok) { failures.add(true); serverErrors.add(1); } else { record('anon', response.timings.duration); } } } export function page_load() { // Rotate through the three authenticated pages a user actually navigates // between, so the reported number is not the mean of one page's cost. const page = loadPage(userSessions, PAGE_LOAD_PATHS[exec.scenario.iterationInTest % PAGE_LOAD_PATHS.length]); record('page_load', page.timing); } export function livewire_read() { const page = getTodosContext(); const step = exec.scenario.iterationInTest % 4; if (step === 0) { // The search box is bound to a 300ms debounce, so every few keystrokes of // a real search session produce one of these. It is also the only query in // the app whose predicate cannot use an index. const term = SEARCH_TERMS[exec.scenario.iterationInTest % SEARCH_TERMS.length]; sendUpdate(page, 'livewire_read', { search: term }, []); } else if (step === 1) { sendUpdate(page, 'livewire_read', {}, [{ method: 'resetFilters' }]); } else if (step === 2) { const status = STATUS_FILTERS[exec.scenario.iterationInTest % STATUS_FILTERS.length]; sendUpdate(page, 'livewire_read', { status }, []); } else { // Paginates one more page, which re-runs the filtered COUNT(*). sendUpdate(page, 'livewire_read', {}, [{ method: 'loadMore' }]); } } export function livewire_write() { const page = getTodosContext(); const session = page.session; const todoId = session.todoIds[exec.scenario.iterationInTest % session.todoIds.length]; sendUpdate(page, 'livewire_write', {}, [{ method: 'toggleComplete', params: [todoId] }]); } export function folders_read() { const page = getFoldersContext(); const folderIds = page.session.folderIds; // A fresh id every time means the component always expands rather than // toggling shut, so every iteration pays the per-folder todo query. const folderId = folderIds[exec.scenario.iterationInTest % folderIds.length]; sendUpdate(page, 'folders_read', {}, [{ method: 'toggleExpandFolder', params: [folderId] }]); } export function folders_write() { const page = getFoldersContext(); const session = page.session; const todoId = session.todoIds[exec.scenario.iterationInTest % session.todoIds.length]; const folderId = session.folderIds[exec.scenario.iterationInTest % session.folderIds.length]; sendUpdate(page, 'folders_write', {}, [ { method: 'moveTodoToFolder', params: [todoId, folderId] }, ]); } export function admin() { const pool = adminSessions.length > 0 ? adminSessions : userSessions; const response = http.get(BASE_URL + '/admin/notes-limits', { headers: authHeaders(sessionFor(pool, exec.vu.idInTest)), tags: { name: '/admin/notes-limits' }, }); const ok = check(response, { 'admin page returned 200': (r) => r.status === 200 }); if (!ok) { failures.add(true); serverErrors.add(1); } else { record('admin', response.timings.duration); } } /** * Search terms, including one that matches nothing, so the LIKE predicate is * exercised against both a matching and a full-scan outcome. */ const SEARCH_TERMS = ['invoice', 'review', 'the', 'a', 'zzznomatch', 'team', 'update', '']; const STATUS_FILTERS = ['open', 'completed', 'overdue', 'all']; const PAGE_LOAD_PATHS = ['/dashboard', '/todos', '/folders']; /** * Per-VU snapshot chains. Each VU gets its own module instance, so these are * private to that VU and survive across its iterations. */ let todosContext = null; let foldersContext = null; function getTodosContext() { if (todosContext === null) { todosContext = loadPage(userSessions, '/todos'); } return todosContext; } function getFoldersContext() { if (foldersContext === null) { foldersContext = loadPage(userSessions, '/folders'); } return foldersContext; } /** * Turn the run into the number the question actually asks for: how many * concurrent users this workload supports. * * Little's Law gives that directly. With a stable arrival rate, the mean number * of requests resident in the system equals the arrival rate times the mean * time each spends there, so: * * concurrent users = throughput (req/s) x mean response time (s) * * Note this is a floor derived from the offered load. Once the server is * saturated, response time grows in step with offered concurrency, so the * product stops growing too. That is why the SLO breach, not the product, is * the signal to stop ramping. */ function capacityFor(metrics, scenario, durationSeconds) { const trend = metrics[METRICS[scenario].trend.name]; const counter = metrics[METRICS[scenario].counter.name]; if (!trend || !counter || !durationSeconds) { return null; } const count = counter.values.count; const values = trend.values; if (!count) { return null; } const rps = count / durationSeconds; const meanSeconds = values.avg / 1000; return { requests: count, rps: round(rps, 2), meanMs: round(values.avg, 1), p50Ms: round(values.med, 1), p95Ms: round(values['p(95)'], 1), p99Ms: round(values['p(99)'], 1), maxMs: round(values.max, 1), concurrentUsers: round(rps * meanSeconds, 1), breachesP95: values['p(95)'] > SLO_P95_MS, }; } function round(value, places) { const factor = Math.pow(10, places); return Math.round(value * factor) / factor; } export function handleSummary(data) { const durationSeconds = (data.state.testRunDurationMs || 0) / 1000; const rows = []; for (const name of ENABLED) { rows.push({ scenario: name, ...(capacityFor(data.metrics, name, durationSeconds) || { error: 'no samples' }) }); } const errorRate = data.metrics.functional_failures ? data.metrics.functional_failures.values.rate : 0; const missing = rows.filter((row) => row.error); const breached = rows.filter((row) => row.breachesP95); const saturated = errorRate >= SLO_ERROR_RATE; const lines = []; lines.push(''); lines.push(`label ${LABEL}`); lines.push(`base url ${BASE_URL}`); lines.push(`vus/scenario ${VUS}`); lines.push(`duration ${DURATION} (measured ${round(durationSeconds, 1)}s)`); lines.push(`error rate ${round(errorRate * 100, 2)}% (SLO < ${SLO_ERROR_RATE * 100}%)`); lines.push(''); lines.push('scenario rps mean p50 p95 p99 max users p95-ok'); lines.push('----------------- ------ ------ ----- ----- ----- ----- ------ ------'); for (const row of rows) { if (row.error) { lines.push(`${row.scenario.padEnd(17)} ${row.error}`); continue; } lines.push( [ row.scenario.padEnd(17), String(row.rps).padStart(6), String(row.meanMs).padStart(6), String(row.p50Ms).padStart(5), String(row.p95Ms).padStart(5), String(row.p99Ms).padStart(5), String(row.maxMs).padStart(5), String(row.concurrentUsers).padStart(6), (row.breachesP95 ? 'NO' : 'yes').padStart(6), ].join(' '), ); } lines.push(''); lines.push(`p95 SLO: ${SLO_P95_MS}ms`); if (missing.length > 0) { lines.push(`VERDICT: ${missing.map((r) => r.scenario).join(', ')} produced no samples. Fix the scenario before reading any capacity number from this run.`); } else if (saturated) { lines.push(`VERDICT: saturated at ${VUS} VU(s). Throughput is capped by the server, not by the workload.`); } else if (breached.length > 0) { lines.push(`VERDICT: ${breached.map((r) => r.scenario).join(', ')} breached the p95 SLO at ${VUS} VU(s). This is the knee.`); } else { lines.push(`VERDICT: still within SLO at ${VUS} VU(s). Raise VUS and measure again.`); } const summary = { label: LABEL, baseUrl: BASE_URL, vus: VUS, durationSeconds: round(durationSeconds, 2), errorRate: errorRate, sloP95Ms: SLO_P95_MS, rows, }; const output = { stdout: lines.join('\n') + '\n' }; if (__ENV.RESULTS) { output[__ENV.RESULTS] = JSON.stringify(summary, null, 2); } return output; }