@deepkit /板凳
具有可视化和统计分析的零依赖基准实用程序。
功能
- 自动同步/异步检测
- 自适应迭代选择(1x到10M迭代)
- GC事件跟踪(当
--expose-gc使用) - 堆增量测量
- 统计分析(RME,方差)
- 彩色编码输出与块栏可视化
- 简单的API和基于类的
BenchSuiteAPI
安装
npm install @deepkit/bench
简单的API
对于快速的一次性基准测试:
import { benchmark, run } from '@deepkit/bench';
let counter = 0;
benchmark('increment', () => {
counter += 10;
});
benchmark('multiply', () => {
counter *= 2;
});
await run(1); // Run each benchmark for 1 second
运行:
node --import @deepkit/run benchmarks/test.ts
BenchSuite API
对于有组织的基准套件进行比较:
import { BenchSuite } from '@deepkit/bench';
const suite = new BenchSuite('serialization', 1, true); // name, maxTime, showSummary
suite.add('serialize', () => {
serialize<User>(user);
});
suite.add('deserialize', () => {
deserialize<User>(data);
});
await suite.runAsync();
输出
━━━ serialization ━━━
▁▂▃▄▅▆▇█▇▆▅▄▃▂▁▂▃▄▅▆ 32,456,789.12 ops/sec 30.821 ns/op ±1.23% serialize
▁▂▃▄▅▆▇█▇▆▅▄▃▂▁▂▃▄▅▆ 28,123,456.78 ops/sec 35.557 ns/op ±0.98% deserialize
Summary:
███████████████ 32.46M ops/sec fastest serialize
█████████████░░ 28.12M ops/sec x1.15 deserialize
格式化工具
import { formatBytes, formatHz, formatHzFull, formatMean, formatRme } from '@deepkit/bench';
formatHz(1_000_000); // "1.00M"
formatHzFull(1_000_000); // "1,000,000.00"
formatMean(0.000001); // "1.000 ns"
formatMean(0.001); // "1.000 µs"
formatMean(1); // "1.000 ms"
formatRme(1.5); // "±1.50%"
formatBytes(1024); // "1.00 KB"
可视化工具
import { getBlockBar, getBrailleBar } from '@deepkit/bench';
// Block bar (uses █▇▆▅▄▃▂▁ characters with HSV color gradient)
getBlockBar(samples, 20);
// Braille bar (more compact, uses Braille characters)
getBrailleBar(samples);
内存和GC实用程序
import { forceGC, getHeapUsage, warmup, warmupAsync } from '@deepkit/bench';
// Force garbage collection (requires --expose-gc)
forceGC();
// Get current heap usage in bytes
const heap = getHeapUsage();
// Warmup a function for V8 optimization
warmup(myFunction, 100);
// Warmup an async function
await warmupAsync(myAsyncFunction, 50);
API参考
类型
interface BenchResult {
hz: number; // Operations per second
elapsed: number; // Total elapsed time in seconds
rme: number; // Relative margin of error (percentage)
mean: number; // Mean time per operation in milliseconds
variance: number; // Variance of samples
iterations: number; // Number of iterations run
samples: number[]; // Sample timings (up to 50)
heapDiff: number; // Heap memory difference in bytes
async: boolean; // Whether this is an async benchmark
gcEvents: number[]; // GC events during benchmark (pause times in ms)
}
type BenchSuiteResult = { [name: string]: BenchResult };
interface BenchmarkOptions {
maxTime?: number; // Maximum time to run in seconds (default: 1)
}
BenchSuite
class BenchSuite {
static onComplete?: (name: string, result: BenchSuiteResult) => void;
constructor(
name: string,
defaultMaxTime?: number, // Default: 1
showSummary?: boolean, // Default: false
);
add(name: string, fn: () => void | Promise<void>, options?: BenchmarkOptions): this;
run(options?: { verbose?: boolean }): void;
runAsync(options?: { verbose?: boolean }): Promise<BenchSuiteResult>;
getResults(): BenchSuiteResult;
}
准确的基准测试技巧
- 使用
--expose-gc用于GC跟踪:node --expose-gc script.ts - 预热功能 在基准测试之前确保V8优化
- 避免副作用 在基准测试函数中,可能会扭曲结果
- 多次运行 考虑系统差异
- 使用更长的持续时间 (2-5秒)更稳定的结果