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

163 lines
4.4 KiB
JavaScript

// Sub2API Performance Thresholds
// 性能阈值定义
// ============= HTTP 请求阈值 =============
export const httpThresholds = {
// 全局 HTTP 请求阈值
httpReqFailed: ['rate<0.01'], // 错误率 < 1%
httpReqDuration: ['p(95)<1000'], // P95 < 1s
httpReqDurationP99: ['p(99)<3000'], // P99 < 3s
// 按路径的阈值
'http_req_duration{path:/health}': ['p(95)<100'], // 健康检查 P95 < 100ms
'http_req_duration{path:/api/v1/auth/login}': ['p(95)<500'], // 登录 P95 < 500ms
'http_req_duration{path:/api/v1/keys}': ['p(95)<300'], // API Key 列表 P95 < 300ms
};
// ============= Gateway 请求阈值 =============
export const gatewayThresholds = {
// Gateway 请求整体阈值
'gateway_req_duration{p(95)}': ['<2000'], // P95 < 2s
'gateway_req_duration{p(99)}': ['<5000'], // P99 < 5s
'gateway_req_failed': ['rate<0.05'], // 错误率 < 5%
// TTFT (Time To First Token)
'gateway_ttft{p(95)}': ['<3000'], // TTFT P95 < 3s
'gateway_ttft{p(99)}': ['<5000'], // TTFT P99 < 5s
// 按平台/模型分类
'gateway_latency_openai{p(95)}': ['<1500'], // OpenAI P95 < 1.5s
'gateway_latency_claude{p(95)}': ['<2000'], // Claude P95 < 2s
'gateway_latency_gemini{p(95)}': ['<1500'], // Gemini P95 < 1.5s
};
// ============= 数据库连接池阈值 =============
export const dbConnectionThresholds = {
// 连接数不应超过配置的 80%
dbConnectionsActive: ['value<0.8*max'], // 活跃连接 < 80%
dbConnectionsIdle: ['value>5'], // 空闲连接 > 5
};
// ============= Redis 连接池阈值 =============
export const redisConnectionThresholds = {
redisConnectionsTotal: ['value<0.8*pool_size'], // 总连接 < 80%
redisConnectionsIdle: ['value>3'], // 空闲连接 > 3
};
// ============= 速率限制阈值 =============
export const rateLimitThresholds = {
rateLimitHits: ['rate<0.1'], // 速率限制命中率应 < 10%
};
// ============= 缓存命中率 =============
export const cacheHitThresholds = {
cacheHitRate: ['rate>0.8'], // 缓存命中率 > 80%
cacheMissRate: ['rate<0.2'], // 缓存未命中率 < 20%
};
// ============= SLA 级别定义 =============
export const slaLevels = {
// 黄金 SLA: 最严格的要求
gold: {
availability: 0.9995, // 99.95%
latencyP95: 500, // ms
latencyP99: 1500, // ms
errorRate: 0.001, // 0.1%
ttftP99: 3000, // ms
},
// 白银 SLA: 标准要求
silver: {
availability: 0.99, // 99%
latencyP95: 1000, // ms
latencyP99: 3000, // ms
errorRate: 0.01, // 1%
ttftP99: 5000, // ms
},
// 青铜 SLA: 最低要求
bronze: {
availability: 0.95, // 95%
latencyP95: 2000, // ms
latencyP99: 5000, // ms
errorRate: 0.05, // 5%
ttftP99: 10000, // ms
},
};
// ============= 导出便捷函数 =============
/**
* 获取指定 SLA 级别的阈值配置
*/
export function getThresholdsForSLA(level = 'silver') {
const sla = slaLevels[level];
if (!sla) {
console.warn(`Unknown SLA level: ${level}, using silver`);
return slaLevels.silver;
}
return {
httpReqFailed: [`rate<${sla.errorRate}`],
httpReqDuration: [`p(95)<${sla.latencyP95}`],
httpReqDurationP99: [`p(99)<${sla.latencyP99}`],
};
}
/**
* 检查指标是否满足指定 SLA 级别
*/
export function meetsSLA(metrics, level = 'silver') {
const sla = slaLevels[level];
const checks = [
{
name: '可用性',
value: metrics.availability,
threshold: sla.availability,
pass: metrics.availability >= sla.availability,
},
{
name: 'P95 延迟',
value: metrics.latencyP95,
threshold: sla.latencyP95,
unit: 'ms',
pass: metrics.latencyP95 <= sla.latencyP95,
},
{
name: 'P99 延迟',
value: metrics.latencyP99,
threshold: sla.latencyP99,
unit: 'ms',
pass: metrics.latencyP99 <= sla.latencyP99,
},
{
name: '错误率',
value: metrics.errorRate,
threshold: sla.errorRate,
pass: metrics.errorRate <= sla.errorRate,
},
];
return {
level,
sla,
checks,
allPassed: checks.every(c => c.pass),
summary: checks.map(c => ({
name: c.name,
value: c.value,
threshold: c.threshold,
unit: c.unit || '%',
passed: c.pass,
})),
};
}