|
| 1 | +/** |
| 2 | + * Benchmark tracking for wizard runs. |
| 3 | + * |
| 4 | + * Usage: |
| 5 | + * const pipeline = createBenchmarkPipeline(spinner, options); |
| 6 | + * pipeline.onMessage(message); |
| 7 | + * pipeline.finalize(resultMessage, durationMs); |
| 8 | + */ |
| 9 | + |
| 10 | +import chalk from 'chalk'; |
| 11 | +import clack from '../../utils/clack'; |
| 12 | +import { logToFile, getLogFilePath, configureLogFile } from '../../utils/debug'; |
| 13 | +import { MiddlewarePipeline } from './pipeline'; |
| 14 | +import { PhaseDetector } from './phase-detector'; |
| 15 | +import { loadBenchmarkConfig } from './config'; |
| 16 | +import { createPluginsFromConfig } from './benchmarks'; |
| 17 | +import type { BenchmarkConfig } from './config'; |
| 18 | +import type { WizardOptions } from '../../utils/types'; |
| 19 | +import { AgentSignals } from '../agent-interface'; |
| 20 | + |
| 21 | +// ── Types ────────────────────────────────────────────────────────────── |
| 22 | + |
| 23 | +export interface StepUsage { |
| 24 | + name: string; |
| 25 | + usage: { |
| 26 | + input_tokens: number; |
| 27 | + output_tokens: number; |
| 28 | + cache_creation_input_tokens: number; |
| 29 | + cache_read_input_tokens: number; |
| 30 | + cache_creation?: { |
| 31 | + ephemeral_5m_input_tokens: number; |
| 32 | + ephemeral_1h_input_tokens: number; |
| 33 | + }; |
| 34 | + }; |
| 35 | + modelUsage: Record<string, unknown>; |
| 36 | + totalCostUsd: number; |
| 37 | + durationMs: number; |
| 38 | + durationApiMs: number; |
| 39 | + numTurns: number; |
| 40 | + contextTokensIn?: number; |
| 41 | + contextTokensOut?: number; |
| 42 | + compactions?: number; |
| 43 | + compactionPreTokens?: number[]; |
| 44 | +} |
| 45 | + |
| 46 | +export interface BenchmarkData { |
| 47 | + timestamp: string; |
| 48 | + steps: StepUsage[]; |
| 49 | + totals: { |
| 50 | + totalCostUsd: number; |
| 51 | + durationMs: number; |
| 52 | + inputTokens: number; |
| 53 | + outputTokens: number; |
| 54 | + numTurns: number; |
| 55 | + totalCompactions: number; |
| 56 | + totalCacheReadTokens: number; |
| 57 | + totalCacheCreation5mTokens: number; |
| 58 | + totalCacheCreation1hTokens: number; |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +// ── Factory ──────────────────────────────────────────────────────────── |
| 63 | + |
| 64 | +/** |
| 65 | + * Create a middleware pipeline configured for benchmarking. |
| 66 | + * Loads .benchmark-config.json from the install dir, falls back to defaults. |
| 67 | + */ |
| 68 | +export function createBenchmarkPipeline( |
| 69 | + spinner: ReturnType<typeof clack.spinner>, |
| 70 | + options: WizardOptions, |
| 71 | + configOverride?: BenchmarkConfig, |
| 72 | +): MiddlewarePipeline { |
| 73 | + const config = configOverride ?? loadBenchmarkConfig(options.installDir); |
| 74 | + |
| 75 | + configureLogFile({ |
| 76 | + path: config.output.logPath, |
| 77 | + enabled: config.output.logEnabled, |
| 78 | + }); |
| 79 | + |
| 80 | + const plugins = createPluginsFromConfig(config, { |
| 81 | + spinner, |
| 82 | + phased: false, |
| 83 | + outputPath: config.output.benchmarkPath, |
| 84 | + }); |
| 85 | + |
| 86 | + if (!config.output.suppressWizardLogs) { |
| 87 | + clack.log.info( |
| 88 | + `${chalk.cyan(AgentSignals.BENCHMARK)} Verbose logs: ${getLogFilePath()}`, |
| 89 | + ); |
| 90 | + clack.log.info( |
| 91 | + `${chalk.cyan( |
| 92 | + AgentSignals.BENCHMARK, |
| 93 | + )} Benchmark data will be written to: ${config.output.benchmarkPath}`, |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + logToFile( |
| 98 | + `${AgentSignals.BENCHMARK} Tracking enabled, starting with setup phase`, |
| 99 | + ); |
| 100 | + |
| 101 | + return new MiddlewarePipeline(plugins, { |
| 102 | + phaseDetector: new PhaseDetector(), |
| 103 | + autoDetectPhases: true, |
| 104 | + }); |
| 105 | +} |
0 commit comments