Datasets:
File size: 5,066 Bytes
9f5cf6f | 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | import type {
BenchmarkConfig,
BenchmarkReport,
QualityGateResult,
ScenarioSummary,
} from '../types.js'
import { clamp } from '../utils/stats.js'
function average(values: number[]): number {
if (values.length === 0) return 0
return values.reduce((sum, value) => sum + value, 0) / values.length
}
function computeScore(
summaries: ScenarioSummary[],
config: BenchmarkConfig,
): BenchmarkReport['aggregate']['score'] {
const p95s = summaries
.map(summary => summary.durationMs?.p95)
.filter((value): value is number => typeof value === 'number' && value > 0)
const avgP95 = average(p95s)
const latency = clamp(100 - avgP95 / 20, 0, 100)
const stability = clamp(
average(summaries.map(summary => summary.successRate)),
0,
100,
)
const qualityFailures = summaries.filter(summary =>
summary.tags.includes('correctness'),
).reduce((sum, item) => sum + item.failedRuns, 0)
const quality = clamp(100 - qualityFailures * 10, 0, 100)
const costProxy = clamp(100 - avgP95 / 40, 0, 100)
const total =
latency * config.weights.latency +
stability * config.weights.stability +
quality * config.weights.quality +
costProxy * config.weights.cost
return {
latency,
stability,
quality,
cost: costProxy,
total,
}
}
export function evaluateQualityGate(
summaries: ScenarioSummary[],
config: BenchmarkConfig,
): QualityGateResult {
const reasons: string[] = []
for (const summary of summaries) {
if (summary.successRate < config.thresholds.minimumSuccessRatePct) {
reasons.push(
`${summary.id} successRate ${summary.successRate.toFixed(2)}% < ${config.thresholds.minimumSuccessRatePct}%`,
)
}
const maxP95 = config.thresholds.maximumP95MsByCategory[summary.category]
if (
maxP95 !== undefined &&
summary.durationMs &&
summary.durationMs.p95 > maxP95
) {
reasons.push(
`${summary.id} p95 ${summary.durationMs.p95.toFixed(2)}ms > ${maxP95}ms`,
)
}
if (summary.id === 'B09') {
const missing =
summary.examples[0]?.missingRelativeImports as number | undefined
if (
typeof missing === 'number' &&
missing > config.thresholds.maximumMissingImports
) {
reasons.push(
`B09 missing_relative_imports=${missing} exceeds ${config.thresholds.maximumMissingImports}`,
)
}
}
}
return {
passed: reasons.length === 0,
reasons,
}
}
export function buildBenchmarkReport({
rootDir,
generatedAt,
config,
scenarios,
observability,
}: {
rootDir: string
generatedAt: string
config: BenchmarkConfig
scenarios: ScenarioSummary[]
observability: {
bootstrapState?: Record<string, unknown>
process?: Record<string, unknown>
}
}): BenchmarkReport {
const totalRuns = scenarios.reduce((sum, item) => sum + item.totalRuns, 0)
const failedScenarios = scenarios.filter(item => item.failedRuns > 0).length
const skippedScenarios = scenarios.filter(
item => item.skippedRuns === item.totalRuns,
).length
const successRate =
scenarios.length === 0
? 0
: average(scenarios.map(item => item.successRate))
const score = computeScore(scenarios, config)
const qualityGate = evaluateQualityGate(scenarios, config)
return {
version: 1,
generatedAt,
rootDir,
mode: config.mode,
runKind: config.runKind,
config,
scenarios,
aggregate: {
totalScenarios: scenarios.length,
failedScenarios,
skippedScenarios,
totalRuns,
successRate,
score,
},
observability,
qualityGate,
}
}
export function buildComparisonText(
baseline: BenchmarkReport,
current: BenchmarkReport,
): string {
const lines: string[] = []
lines.push('# Benchmark Comparison')
lines.push('')
lines.push(`- Baseline: ${baseline.generatedAt}`)
lines.push(`- Current: ${current.generatedAt}`)
lines.push(
`- Total score: ${baseline.aggregate.score.total.toFixed(2)} -> ${current.aggregate.score.total.toFixed(2)}`,
)
lines.push(
`- Success rate: ${baseline.aggregate.successRate.toFixed(2)}% -> ${current.aggregate.successRate.toFixed(2)}%`,
)
lines.push('')
lines.push('## Scenario deltas')
const baselineMap = new Map(baseline.scenarios.map(item => [item.id, item]))
for (const scenario of current.scenarios) {
const older = baselineMap.get(scenario.id)
if (!older) {
lines.push(`- ${scenario.id}: new scenario`)
continue
}
const oldP95 = older.durationMs?.p95 ?? 0
const newP95 = scenario.durationMs?.p95 ?? 0
const delta = newP95 - oldP95
lines.push(
`- ${scenario.id}: p95 ${oldP95.toFixed(2)}ms -> ${newP95.toFixed(2)}ms (delta ${delta.toFixed(2)}ms), success ${older.successRate.toFixed(2)}% -> ${scenario.successRate.toFixed(2)}%`,
)
}
return `${lines.join('\n')}\n`
}
|