// Sub2API Gateway 稳定性测试脚本 (Soak Test) // 长时间运行以检测内存泄漏和性能衰减 // 运行: k6 run --env BASE_URL=http://localhost:8080 --env API_KEY=your_key gateway-soak-test.js import http from 'k6/http'; import { check, sleep } from 'k6'; import { Rate, Trend } from 'k6/metrics'; const errorRate = new Rate('error_rate'); const responseTime = new Trend('response_time'); // 稳定性测试配置 - 中等负载长时间运行 export const options = { stages: [ { duration: '5m', target: 50 }, // 渐进加压 { duration: '1h', target: 50 }, // 稳定运行1小时 { duration: '5m', target: 100 }, // 提高负载 { duration: '1h', target: 100 }, // 稳定运行1小时 { duration: '5m', target: 0 }, // 减压 ], thresholds: { http_req_duration: ['p(95)<2000'], // 95%请求<2s http_req_failed: ['rate<0.001'], // 错误率<0.1% error_rate: ['rate<0.001'], }, }; const BASE_URL = __ENV.BASE_URL || 'http://localhost:8080'; const API_KEY = __ENV.API_KEY || 'sk-test-key'; export default function () { // 轻量级请求以保持持续负载 const res = http.get(`${BASE_URL}/v1/models`, { headers: { 'Authorization': `Bearer ${API_KEY}`, }, }); const success = check(res, { 'status_is_200': (r) => r.status === 200, }); errorRate.add(!success); responseTime.add(res.timings.duration); sleep(1); // 每秒一个请求 }