1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| import http from 'k6/http'; import { check, sleep } from 'k6';
export let options = { stages: [ { duration: '30s', target: 50 }, { duration: '1m', target: 100 }, { duration: '20s', target: 0 }, ], thresholds: { http_req_duration: ['p(95)<500'], http_req_failed: ['rate<0.01'], }, };
export function setup() { console.log('Test setup: 初始化测试数据'); }
export default function () { const url = 'http://localhost:3000/api/users'; const payload = JSON.stringify({ email: `testuser_${__VU}@example.com`, password: 'test123', }); const headers = { 'Content-Type': 'application/json' };
const res = http.post(url, payload, { headers });
check(res, { 'Status is 201': (r) => r.status === 201, 'Response has ID': (r) => JSON.parse(r.body).id !== undefined, });
sleep(Math.random() * 1 + 0.5); }
export function teardown() { console.log('Test teardown: 清理测试数据'); }
|