|
| 1 | +import http from 'k6/http'; |
| 2 | +import { check } from 'k6'; |
| 3 | +import { Trend, Counter } from 'k6/metrics'; |
| 4 | + |
| 5 | +// Custom metrics — show up as separate series in Grafana |
| 6 | +const insertDuration = new Trend('sdk_insert_duration', true); // true = percentiles |
| 7 | +const detokDuration = new Trend('sdk_detokenize_duration', true); |
| 8 | +const insertErrors = new Counter('sdk_insert_errors'); |
| 9 | +const detokErrors = new Counter('sdk_detokenize_errors'); |
| 10 | + |
| 11 | +export const options = { |
| 12 | + stages: [ |
| 13 | + { duration: '1m', target: 0 }, |
| 14 | + { duration: '5m', target: 100 }, |
| 15 | + { duration: '5m', target: 500 }, |
| 16 | + { duration: '5m', target: 1000 }, |
| 17 | + { duration: '5m', target: 500 }, |
| 18 | + ], |
| 19 | + thresholds: { |
| 20 | + http_req_duration: ['p99<3000'], |
| 21 | + http_req_failed: ['rate<0.01'], |
| 22 | + }, |
| 23 | +}; |
| 24 | + |
| 25 | +const BASE_URL = __ENV.WRAPPER_URL || 'http://localhost:8080'; |
| 26 | + |
| 27 | +export default function () { |
| 28 | + // ── Step 1: Insert ──────────────────────────────────────────────────────── |
| 29 | + const insertRes = http.post( |
| 30 | + `${BASE_URL}/insert`, |
| 31 | + JSON.stringify({ |
| 32 | + table: 'cards', |
| 33 | + records: [{ card_number: '4111111111111111', name: 'Jane Doe' }], |
| 34 | + }), |
| 35 | + { headers: { 'Content-Type': 'application/json' } } |
| 36 | + ); |
| 37 | + |
| 38 | + insertDuration.add(insertRes.timings.duration); |
| 39 | + const insertOk = check(insertRes, { 'insert status 200': (r) => r.status === 200 }); |
| 40 | + if (!insertOk) { |
| 41 | + insertErrors.add(1); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + // Extract the token the SDK returned from the insert response |
| 46 | + const token = JSON.parse(insertRes.body).tokens?.card_number; |
| 47 | + if (!token) return; |
| 48 | + |
| 49 | + // ── Step 2: Detokenize using the token from step 1 ──────────────────────── |
| 50 | + const detokRes = http.post( |
| 51 | + `${BASE_URL}/detokenize`, |
| 52 | + JSON.stringify({ tokens: [token] }), |
| 53 | + { headers: { 'Content-Type': 'application/json' } } |
| 54 | + ); |
| 55 | + |
| 56 | + detokDuration.add(detokRes.timings.duration); |
| 57 | + const detokOk = check(detokRes, { |
| 58 | + 'detok status 200': (r) => r.status === 200, |
| 59 | + 'value is correct': (r) => JSON.parse(r.body).records?.[0]?.value === '4111111111111111', |
| 60 | + }); |
| 61 | + if (!detokOk) detokErrors.add(1); |
| 62 | +} |
0 commit comments