Skip to content

Instantly share code, notes, and snippets.

@ddanielsantos
Created August 16, 2025 05:26
Show Gist options
  • Save ddanielsantos/d532928556eb5ec5f0db0dfce265003c to your computer and use it in GitHub Desktop.
Save ddanielsantos/d532928556eb5ec5f0db0dfce265003c to your computer and use it in GitHub Desktop.
stress test for woovers-js pix qrcode
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
}
@ddanielsantos
Copy link
Author

sem wal mode

HTTP
    http_req_duration.......................................................: avg=1.28ms min=0s med=1.05ms max=12.56ms p(90)=2.39ms p(95)=4.16ms
      { expected_response:true }............................................: avg=1.28ms min=0s med=1.05ms max=12.56ms p(90)=2.39ms p(95)=4.16ms
    http_req_failed.........................................................: 0.00%  0 out of 506
    http_reqs...............................................................: 506    8.419006/s

com wal

HTTP
    http_req_duration.......................................................: avg=1.38ms min=0s med=1.55ms max=27.09ms p(90)=2.29ms p(95)=2.79ms
      { expected_response:true }............................................: avg=1.38ms min=0s med=1.55ms max=27.09ms p(90)=2.29ms p(95)=2.79ms
    http_req_failed.........................................................: 0.00%  0 out of 506
    http_reqs...............................................................: 506    8.41665/s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment