| 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`
|
| }
|
|
|