transfers.js100 lines, 3.9 KB
| 1 | // A load test of transfers, through the gateway, the way clients call it. |
| 2 | // setup() signs up the users. Then transfers arrive at a set rate, however |
| 3 | // slowly we answer: an open model, like real traffic. |
| 4 | import http from 'k6/http'; |
| 5 | import { check, fail } from 'k6'; |
| 6 | |
| 7 | const BASE_URL = __ENV.BASE_URL || 'http://localhost:8080'; |
| 8 | // The gateway allows a user 120 requests a minute: at 40 transfers a |
| 9 | // second, 50 users send about 50 each. |
| 10 | const USERS = 50; |
| 11 | const PASSWORD = 'correct horse battery staple'; |
| 12 | |
| 13 | export const options = { |
| 14 | setupTimeout: '2m', |
| 15 | scenarios: { |
| 16 | transfers: { |
| 17 | executor: 'ramping-arrival-rate', |
| 18 | startRate: 5, |
| 19 | timeUnit: '1s', |
| 20 | preAllocatedVUs: 20, |
| 21 | maxVUs: 100, |
| 22 | stages: [ |
| 23 | { duration: '30s', target: 40 }, // up to 40 transfers a second |
| 24 | { duration: '45s', target: 40 }, // hold it there |
| 25 | { duration: '15s', target: 0 }, // and back down |
| 26 | ], |
| 27 | }, |
| 28 | }, |
| 29 | // If any of these fails, so does the run, with an exit code CI can see. |
| 30 | thresholds: { |
| 31 | 'http_req_failed{name:transfer}': ['rate<0.01'], |
| 32 | 'http_req_duration{name:transfer}': ['p(95)<200', 'p(99)<500'], |
| 33 | checks: ['rate>0.99'], |
| 34 | }, |
| 35 | summaryTrendStats: ['med', 'p(95)', 'p(99)', 'max'], |
| 36 | }; |
| 37 | |
| 38 | // Every request sends JSON, a token if there is one, a new Idempotency-Key, |
| 39 | // and a name, so its numbers get a line of their own. |
| 40 | function params(token, name) { |
| 41 | const headers = { 'Content-Type': 'application/json', 'Idempotency-Key': crypto.randomUUID() }; |
| 42 | if (token) headers.Authorization = `Bearer ${token}`; |
| 43 | return { headers, tags: { name } }; |
| 44 | } |
| 45 | |
| 46 | // post sends a request in setup, where anything unexpected stops the test. |
| 47 | function post(path, body, token, name, status) { |
| 48 | const res = http.post(`${BASE_URL}${path}`, JSON.stringify(body), params(token, name)); |
| 49 | if (!check(res, { [`${name}: ${status}`]: (r) => r.status === status })) { |
| 50 | fail(`${name}: got ${res.status}, want ${status}: ${res.body}`); |
| 51 | } |
| 52 | return res; |
| 53 | } |
| 54 | |
| 55 | // Each user registers, logs in, opens a wallet, and deposits $1,000. |
| 56 | export function setup() { |
| 57 | const run = Date.now(); |
| 58 | const users = []; |
| 59 | for (let i = 0; i < USERS; i++) { |
| 60 | const email = `load-${run}-${i}@example.com`; |
| 61 | post('/v1/auth/register', { email, password: PASSWORD }, null, 'register', 201); |
| 62 | const token = post('/v1/auth/login', { email, password: PASSWORD }, null, 'login', 200) |
| 63 | .json('access_token'); |
| 64 | const wallet = post('/v1/wallets', { currency: 'USD' }, token, 'wallet', 201).json('id'); |
| 65 | post(`/v1/wallets/${wallet}/deposits`, { amount: 100000, currency: 'USD' }, token, 'deposit', 201); |
| 66 | users.push({ token, wallet }); |
| 67 | } |
| 68 | return users; |
| 69 | } |
| 70 | |
| 71 | // One iteration is one transfer of a dollar, to someone else. |
| 72 | export default function (users) { |
| 73 | const i = Math.floor(Math.random() * users.length); |
| 74 | const j = (i + 1 + Math.floor(Math.random() * (users.length - 1))) % users.length; |
| 75 | const res = http.post(`${BASE_URL}/v1/transfers`, JSON.stringify({ |
| 76 | from: users[i].wallet, to: users[j].wallet, amount: 100, currency: 'USD', |
| 77 | }), params(users[i].token, 'transfer')); |
| 78 | check(res, { 'transfer: 202': (r) => r.status === 202 }); |
| 79 | } |
| 80 | |
| 81 | // k6's own summary is long. This one fits a terminal: the transfers, their |
| 82 | // latency, and every threshold, passed or failed. |
| 83 | export function handleSummary(data) { |
| 84 | const m = data.metrics; |
| 85 | const ms = (v) => `${v.toFixed(0)} ms`; |
| 86 | const d = m['http_req_duration{name:transfer}'].values; |
| 87 | const failed = m['http_req_failed{name:transfer}'].values.rate * 100; |
| 88 | const lines = [ |
| 89 | '', |
| 90 | `transfers ${m.iterations.values.count}, ${failed.toFixed(2)}% failed`, |
| 91 | `latency med ${ms(d.med)}, p95 ${ms(d['p(95)'])}, p99 ${ms(d['p(99)'])}, max ${ms(d.max)}`, |
| 92 | '', |
| 93 | ]; |
| 94 | for (const [name, metric] of Object.entries(m)) { |
| 95 | for (const [rule, { ok }] of Object.entries(metric.thresholds || {})) { |
| 96 | lines.push(`${ok ? '✓' : '✗'} ${name}: ${rule}`); |
| 97 | } |
| 98 | } |
| 99 | return { stdout: lines.join('\n') + '\n' }; |
| 100 | } |