@deepkit/bench

中文文档
1.1.0发布于 2026/11/01MITTypeScriptESM + CJS零依赖

Deepkit板凳

中文使用文档
文档目录12 节
  1. 功能
  2. 安装
  3. 简单的API
  4. BenchSuite API
  5. 输出
  6. 格式化工具
  7. 可视化工具
  8. 内存和GC实用程序
  9. API参考
  10. 类型
  11. BenchSuite
  12. 准确的基准测试技巧

@deepkit /板凳

具有可视化和统计分析的零依赖基准实用程序。

功能

  • 自动同步/异步检测
  • 自适应迭代选择(1x到10M迭代)
  • GC事件跟踪(当 --expose-gc 使用)
  • 堆增量测量
  • 统计分析(RME,方差)
  • 彩色编码输出与块栏可视化
  • 简单的API和基于类的 BenchSuite API

安装

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

准确的基准测试技巧

  1. 使用 --expose-gc 用于GC跟踪: node --expose-gc script.ts
  2. 预热功能 在基准测试之前确保V8优化
  3. 避免副作用 在基准测试函数中,可能会扭曲结果
  4. 多次运行 考虑系统差异
  5. 使用更长的持续时间 (2-5秒)更稳定的结果

版本与发布记录

@deepkit/bench 共发布 5 个版本,首次发布于 2025/04/03;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.1.0latest2026/11/012 天前
1.0.192025/09/221.1 年前
1.0.182025/09/221.1 年前
1.0.152025/06/261.4 年前
1.0.52025/04/031.6 年前

关于 @deepkit/bench 的常见问题

@deepkit/bench 是什么?

Deepkit板凳

如何安装 @deepkit/bench?

在项目目录运行 npm install @deepkit/bench,或使用 pnpm add / yarn add / bun add。

@deepkit/bench 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@deepkit/bench 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@deepkit/bench 可以商用吗?

@deepkit/bench 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@deepkit/bench 还在维护吗?

最新版本 1.1.0 发布于 2026/11/01(2 天前);至今共发布 5 个版本,其中过去 12 个月发布了 1 个;首次发布于 2025/04/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。