import http from 'k6/http'; import { check, sleep } from 'k6'; const BASE_URL = 'http://localhost:8084'; export const options = { stages: [ { duration: '2m', target: 10 }, // 2分钟内增加到10用户 { duration: '5m', target: 10 }, // 保持10用户5分钟 { duration: '2m', target: 50 }, // 2分钟内增加到50用户 { duration: '5m', target: 50 }, // 保持50用户5分钟 { duration: '2m', target: 100 }, // 2分钟内增加到100用户 { duration: '5m', target: 100 }, // 保持100用户5分钟 { duration: '2m', target: 0 }, // 2分钟内降到0用户 ], thresholds: { http_req_duration: ['p(95)<500'], // 95%的请求响应时间<500ms http_req_failed: ['rate<0.01'], // 错误率<1% }, }; export default function () { // 测试1: 健康检查 let healthRes = http.get(`${BASE_URL}/actuator/health`); check(healthRes, { '健康检查状态码200': (r) => r.status === 200, '健康检查响应时间<100ms': (r) => r.timings.duration < 100, }); // 测试2: 登录 let loginRes = http.post( `${BASE_URL}/api/auth/login`, JSON.stringify({ username: 'admin', password: 'admin123', }), { headers: { 'Content-Type': 'application/json' }, } ); check(loginRes, { '登录状态码200': (r) => r.status === 200, '登录响应时间<500ms': (r) => r.timings.duration < 500, '登录返回token': (r) => JSON.parse(r.body).token !== undefined, }); // 测试3: 获取用户列表 const token = JSON.parse(loginRes.body).token; let usersRes = http.get( `${BASE_URL}/api/users?page=0&size=10`, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, } ); check(usersRes, { '用户列表状态码200': (r) => r.status === 200, '用户列表响应时间<300ms': (r) => r.timings.duration < 300, '用户列表返回数据': (r) => JSON.parse(r.body).content !== undefined, }); // 测试4: 获取角色列表 let rolesRes = http.get( `${BASE_URL}/api/roles?page=0&size=10`, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, } ); check(rolesRes, { '角色列表状态码200': (r) => r.status === 200, '角色列表响应时间<300ms': (r) => r.timings.duration < 300, '角色列表返回数据': (r) => JSON.parse(r.body).content !== undefined, }); // 测试5: 获取字典列表 let dictRes = http.get( `${BASE_URL}/api/dicts?page=0&size=10`, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, } ); check(dictRes, { '字典列表状态码200': (r) => r.status === 200, '字典列表响应时间<300ms': (r) => r.timings.duration < 300, '字典列表返回数据': (r) => JSON.parse(r.body).content !== undefined, }); // 测试6: 获取系统配置 let configRes = http.get( `${BASE_URL}/api/configs?page=0&size=10`, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, } ); check(configRes, { '系统配置状态码200': (r) => r.status === 200, '系统配置响应时间<300ms': (r) => r.timings.duration < 300, '系统配置返回数据': (r) => JSON.parse(r.body).content !== undefined, }); // 测试7: 获取通知列表 let noticeRes = http.get( `${BASE_URL}/api/notices?page=0&size=10`, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, } ); check(noticeRes, { '通知列表状态码200': (r) => r.status === 200, '通知列表响应时间<300ms': (r) => r.timings.duration < 300, '通知列表返回数据': (r) => JSON.parse(r.body).content !== undefined, }); // 测试8: 获取操作日志 let opLogRes = http.get( `${BASE_URL}/api/operation-logs?page=0&size=10`, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, } ); check(opLogRes, { '操作日志状态码200': (r) => r.status === 200, '操作日志响应时间<300ms': (r) => r.timings.duration < 300, '操作日志返回数据': (r) => JSON.parse(r.body).content !== undefined, }); sleep(1); // 每个虚拟用户之间间隔1秒 }