Files
ruvnet__ruflo/v3/@claude-flow/memory/framework/benchmark.ts
rUv 7c811c1c96 v3: Major cleanup, benchmarking, and documentation
## Cleanup (~1000KB removed)
- Remove /implementation/ planning docs (no longer needed)
- Remove duplicate /coordination/, /core/, /shared/, /types/ folders
- Remove stale status/tracking MDs from @claude-flow modules
- Remove /docs/reports/ status documents

## Performance Benchmarking
- Add scripts/quick-benchmark.mjs for fast performance validation
- Add __tests__/setup.ts (required by vitest config)
- Add @claude-flow/memory/framework/benchmark.ts utilities
- All benchmarks passing targets:
  - Event Bus: 6ms for 100k events (<50ms target)
  - Map Lookup: 16ms for 100k gets (<20ms target)
  - O(n) to O(1) optimization: 978x speedup confirmed

## Documentation
- Complete rewrite of v3/README.md with:
  - Architecture overview (10 ADRs)
  - Module architecture diagram
  - Full directory structure
  - Usage examples for all 10 modules
  - Performance targets and achievements
  - Installation and testing instructions

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-05 03:00:34 +00:00

113 lines
2.8 KiB
TypeScript

/**
* Benchmark Framework
*
* Utilities for running performance benchmarks.
*/
export interface BenchmarkResult {
name: string;
iterations: number;
totalTime: number;
avgTime: number;
minTime: number;
maxTime: number;
opsPerSecond: number;
meetsTarget: boolean;
}
export interface BenchmarkOptions {
iterations?: number;
warmupIterations?: number;
targetTime?: number;
}
export class BenchmarkRunner {
private results: BenchmarkResult[] = [];
async run(name: string, fn: () => void | Promise<void>, options: BenchmarkOptions = {}): Promise<BenchmarkResult> {
const { iterations = 1000, warmupIterations = 100, targetTime } = options;
// Warmup
for (let i = 0; i < warmupIterations; i++) {
await fn();
}
// Actual benchmark
const times: number[] = [];
const start = performance.now();
for (let i = 0; i < iterations; i++) {
const iterStart = performance.now();
await fn();
times.push(performance.now() - iterStart);
}
const totalTime = performance.now() - start;
const avgTime = totalTime / iterations;
const minTime = Math.min(...times);
const maxTime = Math.max(...times);
const opsPerSecond = 1000 / avgTime;
const result: BenchmarkResult = {
name,
iterations,
totalTime,
avgTime,
minTime,
maxTime,
opsPerSecond,
meetsTarget: targetTime ? avgTime <= targetTime : true,
};
this.results.push(result);
return result;
}
getResults(): BenchmarkResult[] {
return [...this.results];
}
printSummary(): void {
console.log('\n=== Benchmark Summary ===\n');
for (const result of this.results) {
const status = result.meetsTarget ? '✅' : '❌';
console.log(`${status} ${result.name}`);
console.log(` Avg: ${formatTime(result.avgTime)}`);
console.log(` Min: ${formatTime(result.minTime)}`);
console.log(` Max: ${formatTime(result.maxTime)}`);
console.log(` Ops/sec: ${result.opsPerSecond.toFixed(2)}`);
console.log('');
}
}
}
export function formatTime(ms: number): string {
if (ms < 0.001) {
return `${(ms * 1000000).toFixed(2)}ns`;
} else if (ms < 1) {
return `${(ms * 1000).toFixed(2)}μs`;
} else if (ms < 1000) {
return `${ms.toFixed(2)}ms`;
} else {
return `${(ms / 1000).toFixed(2)}s`;
}
}
export function meetsTarget(actual: number, target: number): boolean {
return actual <= target;
}
export async function benchmark(
name: string,
fn: () => void | Promise<void>,
options: BenchmarkOptions = {}
): Promise<BenchmarkResult> {
const runner = new BenchmarkRunner();
return runner.run(name, fn, options);
}
export function createBenchmarkSuite(name: string): BenchmarkRunner {
console.log(`\n📊 Benchmark Suite: ${name}\n`);
return new BenchmarkRunner();
}