Files
Developer 349d783fd1 refactor: clean up project structure
- Remove old review reports (keep latest only)
- Move docs/ to deploy/docs-backup/
- Move performance-testing/ to deploy/
- Clean up test output files
- Organize root directory
2026-04-06 23:36:03 +08:00

102 lines
3.3 KiB
JavaScript

// Sub2API Health Check Performance Test
// 健康检查接口性能测试
import http from 'k6/http';
import { check, sleep, group } from 'k6';
import { Rate, Trend, Counter } from 'k6/metrics';
import { getBaseUrl } from '../common/utils.js';
// ============= 自定义指标 =============
const healthCheckDuration = new Trend('health_check_duration');
const healthErrorRate = new Rate('health_errors');
// ============= 测试配置 =============
export const options = {
scenarios: {
health_load: {
executor: 'ramping-vus',
startVUs: 5,
stages: [
{ duration: '30s', target: 20 },
{ duration: '2m', target: 50 },
{ duration: '30s', target: 0 },
],
},
},
thresholds: {
'health_check_duration': ['p(95)<100', 'p(99)<200'],
'health_errors': ['rate<0.001'],
},
};
// ============= 测试场景 =============
export default function () {
group('Health Check', () => {
const start = Date.now();
// 健康检查端点
const res = http.get(`${getBaseUrl()}/health`, {
tags: { name: 'health_check' },
});
const duration = Date.now() - start;
healthCheckDuration.add(duration);
check(res, {
'Health: status is 200': (r) => r.status === 200,
'Health: has status field': (r) => r.json('status') !== undefined,
'Health: response time < 100ms': () => duration < 100,
});
healthErrorRate.add(res.status !== 200);
// 可选:详细健康检查
if (__VU % 10 === 0) {
const detailedRes = http.get(`${getBaseUrl()}/health/detailed`, {
tags: { name: 'health_detailed' },
});
check(detailedRes, {
'Detailed Health: status is 200': (r) => r.status === 200,
});
}
});
// 健康检查通常不需要太长的思考时间
sleep(0.01);
}
export function handleSummary(data) {
return {
'health-performance-report.json': JSON.stringify(data, null, 2),
'health-performance-summary.txt': `
================================================================================
Sub2API Health Check 性能测试报告
================================================================================
测试时间: ${new Date().toISOString()}
测试持续: ${(data.state.testRunDurationMs / 1000 / 60).toFixed(2)} 分钟
--------------------------------------------------------------------------------
核心指标
--------------------------------------------------------------------------------
总请求数: ${data.metrics?.requests_total?.values?.count || 0}
错误率: ${((data.metrics?.health_errors?.values?.rate || 0) * 100).toFixed(3)}%
平均响应时间: ${data.metrics?.health_check_duration?.values?.avg?.toFixed(2) || 0} ms
P50 响应时间: ${data.metrics?.health_check_duration?.values?.['p(50)']?.toFixed(2) || 0} ms
P95 响应时间: ${data.metrics?.health_check_duration?.values?.['p(95)']?.toFixed(2) || 0} ms
P99 响应时间: ${data.metrics?.health_check_duration?.values?.['p(99)']?.toFixed(2) || 0} ms
最大响应时间: ${data.metrics?.health_check_duration?.values?.max?.toFixed(2) || 0} ms
================================================================================
测试完成
================================================================================
`,
};
}