105 lines
3.3 KiB
JavaScript
105 lines
3.3 KiB
JavaScript
// Backend/tests/k6/register-students.js
|
|
// สคริปสำหรับ register นักเรียน 50 คน
|
|
// email: studenttest01-50@example.com
|
|
// username: student01-50
|
|
// password: admin123
|
|
|
|
import http from 'k6/http';
|
|
import { check, sleep } from 'k6';
|
|
import { Rate, Counter } from 'k6/metrics';
|
|
|
|
// Custom metrics
|
|
const errorRate = new Rate('errors');
|
|
const successCount = new Counter('successful_registrations');
|
|
const failCount = new Counter('failed_registrations');
|
|
|
|
// Configuration: Run 50 iterations sequentially (1 VU to avoid duplicate)
|
|
export const options = {
|
|
iterations: 50,
|
|
vus: 1, // 1 VU to ensure sequential registration (no duplicates)
|
|
thresholds: {
|
|
errors: ['rate<0.1'], // Error rate < 10%
|
|
http_req_duration: ['p(95)<3000'], // 95% of requests < 3s
|
|
},
|
|
};
|
|
|
|
const BASE_URL = __ENV.APP_URL || 'http://192.168.1.137:4000';
|
|
|
|
export default function () {
|
|
// Calculate student number (1-50) based on iteration
|
|
// __ITER is unique per iteration across all VUs
|
|
const studentNum = __ITER + 1;
|
|
const paddedNum = String(studentNum).padStart(2, '0');
|
|
|
|
const email = `studenttest${paddedNum}@example.com`;
|
|
const username = `student${paddedNum}`;
|
|
const password = 'admin123';
|
|
|
|
console.log(`Registering student: ${username} (${email})`);
|
|
|
|
const payload = JSON.stringify({
|
|
username: username,
|
|
email: email,
|
|
password: password,
|
|
first_name: `Student`,
|
|
last_name: `Test${paddedNum}`,
|
|
prefix: {
|
|
en: 'Mr.',
|
|
th: 'นาย'
|
|
},
|
|
phone: `08${paddedNum}000000${paddedNum}`,
|
|
});
|
|
|
|
const headers = {
|
|
'Content-Type': 'application/json',
|
|
};
|
|
|
|
const res = http.post(`${BASE_URL}/api/auth/register-learner`, payload, { headers });
|
|
|
|
const isSuccess = res.status === 200 || res.status === 201;
|
|
const isAlreadyExists = res.status === 400 && res.body && res.body.includes('already');
|
|
|
|
errorRate.add(!isSuccess && !isAlreadyExists);
|
|
|
|
if (isSuccess) {
|
|
successCount.add(1);
|
|
console.log(`✓ Successfully registered: ${username}`);
|
|
} else if (isAlreadyExists) {
|
|
console.log(`⚠ Already exists: ${username}`);
|
|
} else {
|
|
failCount.add(1);
|
|
console.log(`✗ Failed to register: ${username} - Status: ${res.status} - ${res.body}`);
|
|
}
|
|
|
|
check(res, {
|
|
'registration successful or already exists': (r) => r.status === 200 || r.status === 201 || r.status === 400,
|
|
'response time < 3s': (r) => r.timings.duration < 3000,
|
|
});
|
|
|
|
sleep(0.5); // Small delay between registrations
|
|
}
|
|
|
|
export function handleSummary(data) {
|
|
return {
|
|
'stdout': textSummary(data, { indent: ' ', enableColors: true }),
|
|
};
|
|
}
|
|
|
|
function textSummary(data, opts) {
|
|
const metrics = data.metrics;
|
|
const iterations = metrics.iterations ? metrics.iterations.values.count : 0;
|
|
const successRegs = metrics.successful_registrations ? metrics.successful_registrations.values.count : 0;
|
|
const failRegs = metrics.failed_registrations ? metrics.failed_registrations.values.count : 0;
|
|
|
|
return `
|
|
=====================================
|
|
Student Registration Summary
|
|
=====================================
|
|
Total Iterations: ${iterations}
|
|
Successful Registrations: ${successRegs}
|
|
Failed Registrations: ${failRegs}
|
|
Error Rate: ${(metrics.errors.values.rate * 100).toFixed(2)}%
|
|
Avg Response Time: ${metrics.http_req_duration.values.avg.toFixed(2)}ms
|
|
=====================================
|
|
`;
|
|
}
|