@benchsdk/runner

中文文档
0.8.1发布于 2026/11/04MITTypeScriptESM + CJS构建来源已验证近 30 天下载 7896

Benchmark framework: task + step + config primitives and the local orchestrator that drives @benchsdk/worker

原始使用文档
文档目录7 节
  1. What it provides
  2. Install
  3. Usage
  4. Context channels
  5. Platform data commands
  6. Examples and full guide
  7. License

@benchsdk/runner

Benchmark framework for authoring *.bench.ts files and operating benchmark workers. It reports to the benchmarks platform via @benchsdk/api; @benchsdk/client is a backwards-compatibility re-export shim.

What it provides

  • defineBenchmarkConfig / defineTask — A *.bench.ts file exports exactly two things: a config (defineBenchmarkConfig, the orchestration knobs + participants + an optional onComplete hook) and a task (defineTask, the workload for one iteration). There is no "mode": the orchestration shape (sequential / staggered / burst) emerges from the iterations, concurrency, and staggerDelayMs knobs, and groupBy ('participant' | 'round') selects the ordering across participants.
  • bench run <file> — The CLI entrypoint. It imports the module, reads its config and task, applies CLI overrides, and drives the run. Benchmark files declare; they never call the runner themselves.
  • bench check <file> — Validates environment variables, API connectivity, participant availability, and scoring weights before a run.
  • runBenchmarkWorker(options) — One-shot operator helper that runs a single participant's worker without a *.bench.ts file.
  • TaskError / NoAvailableParticipantsError — Structured errors: throw TaskError from a task to attach a code / data / pre-measured steps; bench run treats NoAvailableParticipantsError (every participant env-gated out) as a clean no-op exit.

Install

pnpm add @benchsdk/runner @benchsdk/client

Usage

A benchmark file exports a config and a task — nothing else:

import { defineBenchmarkConfig, defineTask } from '@benchsdk/runner';
import { providers } from './providers.js';
import { writeLegacyResults } from './legacy-results.js';

export const config = defineBenchmarkConfig({
  benchmarkSlug: 'sandbox-tti-local',
  benchmarkName: 'Sandbox TTI (local)',
  iterations: 100,       // total tasks per participant
  concurrency: 1,        // 1 = sequential, N = burst, N + staggerDelayMs = staggered
  participants: providers,
  // Aggregate post-run work (the one thing a single task can't see) lives here.
  onComplete: (outcome) => writeLegacyResults(outcome.participants),
});

export const task = defineTask(async ({ participant, step, measure, log }) => {
  log('creating sandbox', { level: 'info', meta: { participant: participant.name } });
  // Named steps via `ctx.step`: values flow between steps with closures and
  // cleanup runs in a `finally`. Each step is a first-class platform record
  // with its own timing/status. A step returning `{ stdout, stderr, exitCode }`
  // writes that output to the worker log unless `captureOutput: false` is passed.
  const sandbox = await step('create', () => participant.createCompute().sandbox.create());
  try {
    const t0 = performance.now();
    await step('exec', () => sandbox.runCommand('node -v'));
    measure({ ttiMs: performance.now() - t0 });   // metrics → the platform
  } finally {
    await step('destroy', () => sandbox.destroy());
  }
});

Run it with the CLI (flags override the config knobs):

bench run benchmarks/sandbox/sandbox-tti.bench.ts --iterations 100 --concurrency 20 --provider e2b,modal

bench run requires platform auth — BENCHMARKS_PLATFORM_API_KEY, BENCHMARKS_PLATFORM_TOKEN, or a token saved via bench auth login — even for --dry-run / --no-ingest / BENCHSDK_NO_INGEST=1; those flags only skip uploading, they do not skip auth.

To load a TypeScript benchmark without a build step, run the CLI under a TS loader:

tsx node_modules/@benchsdk/runner/dist/bin.js run sandbox-tti.bench.ts

Context channels

Inside a task the context exposes three separate channels:

  • step(name, fn, options?) — returns fn's value to your code (thread live objects between steps); records the step's timing/status on the platform. Return values are never auto-recorded as data. Set captureOutput: false to return an outcome-shaped object (stdout, stderr, exitCode, ...) without writing it to the worker log. Use parallelInvocations (formerly concurrency) to invoke a step multiple times in parallel.
  • measure(data) — explicit metric channel. Called inside a step() it merges into that step's data; called at task top-level it merges into the task record. A task with no explicit steps is recorded as one implicit 'task' step carrying its measurements.
  • log(message, metaOrOptions?) — human-readable narration to the run timeline. metaOrOptions can be a metadata JSON object or { level: 'debug' | 'info' | 'warn' | 'error', meta?: JsonObject }.

Platform data commands

bench is a unified CLI. Besides bench run, it can authenticate and query the platform:

bench check <file.bench.ts>
bench auth login
bench benchmarks list
bench runs list <slug>
bench results <slug> --run <runId>
bench artifacts list <slug> <runId>
bench export <slug> --out ./exports

See the benchsdk-cli skill for the full CLI reference, OAuth device-code login, config/credentials files, and CI use.

Examples and full guide

For a step-by-step authoring guide and runnable examples covering every capability, see WRITING_BENCHMARKS.md and the examples/ directory.

License

MIT

版本与发布记录

@benchsdk/runner 共发布 12 个版本,首次发布于 2026/08/18;过去 12 个月发布了 12 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.8.1latest2026/11/043 天前
0.8.02026/10/2910 天前
0.7.02026/10/2811 天前
0.6.62026/10/2514 天前
0.6.52026/10/2315 天前
0.6.32026/10/2216 天前

关于 @benchsdk/runner 的常见问题

@benchsdk/runner 是什么?

Benchmark framework: task + step + config primitives and the local orchestrator that drives @benchsdk/worker

如何安装 @benchsdk/runner?

在项目目录运行 npm install @benchsdk/runner,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 bench,可以用 npx bench 直接运行。

@benchsdk/runner 支持 TypeScript 吗?

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

@benchsdk/runner 支持 ESM 还是 CommonJS?

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

@benchsdk/runner 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@benchsdk/runner 可以商用吗?

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

@benchsdk/runner 的下载量有多少?

根据 npm 官方下载统计,@benchsdk/runner 在 2026/10/08 – 2026/11/06 这 30 天内被下载 7,896 次,日均约 263 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@benchsdk/runner 还在维护吗?

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