Created
August 16, 2025 05:26
-
-
Save ddanielsantos/d532928556eb5ec5f0db0dfce265003c to your computer and use it in GitHub Desktop.
stress test for woovers-js pix qrcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http from 'k6/http'; | |
import { check, sleep } from 'k6'; | |
// Configuration | |
const BASE_URL = 'http://localhost:3000'; | |
const VUS = 10; | |
const DURATION = '30s'; | |
export const options = { | |
vus: VUS, // number of virtual users | |
stages: [ | |
{ duration: '10s', target: VUS / 2 }, // ramp up to 50% of VUs | |
{ duration: '10s', target: VUS }, // ramp up to 100% of VUs | |
{ duration: DURATION, target: VUS }, // stay at 100% for the main duration | |
{ duration: '10s', target: 0 }, // ramp down to 0 VUs | |
], | |
thresholds: { | |
'http_req_failed': ['rate<0.01'], // http errors should be less than 1% | |
'http_req_duration': ['p(95)<500'], // 95% of requests should be below 500 ms | |
}, | |
}; | |
// Setup function: Create an initial QR code to get a valid ID for GET requests | |
export function setup() { | |
const payload = JSON.stringify({ | |
name: 'Setup Test QR Code', | |
correlationID: `setup-qrcode-${Date.now()}`, | |
value: 1.0, | |
comment: 'QR code created during test setup', | |
}); | |
const params = { | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
}; | |
const res = http.post(`${BASE_URL}/api/v1/qrcode-static`, payload, params); | |
check(res, { 'Setup POST request successful': (r) => r.status === 201 }); | |
if (res.status !== 201) { | |
console.error('Setup failed. Could not create initial QR code.'); | |
return { qrCodeId: null }; | |
} | |
const body = JSON.parse(res.body); | |
return { qrCodeId: body.pixQrCode.identifier }; | |
} | |
export default function (data) { | |
if (!data.qrCodeId) { | |
console.error('Cannot run test, setup failed.'); | |
return; | |
} | |
// Simulate a workload with 80% read and 20% write operations | |
if (__VU % 5 < 4) { | |
// Read operation (GET single QR code) | |
const res = http.get(`${BASE_URL}/api/v1/qrcode-static/${data.qrCodeId}`); | |
check(res, { | |
'GET /:id status is 200': (r) => r.status === 200, | |
}); | |
} else { | |
// Write operation (POST new QR code) | |
const payload = JSON.stringify({ | |
name: `Test QR Code from VU ${__VU}`, | |
correlationID: `test-vu-${__VU}-${__ITER}`, | |
value: Math.random() * 100 + 1, | |
comment: 'QR code created during the main test execution.', | |
}); | |
const params = { | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
}; | |
const res = http.post(`${BASE_URL}/api/v1/qrcode-static`, payload, params); | |
check(res, { | |
'POST / status is 201': (r) => r.status === 201, | |
}); | |
} | |
sleep(1); // wait for 1 second between iterations | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sem wal mode
com wal