本文档介绍了 Deno 内置的基准测试系统,包括基准测试的发现、执行、测量和报告。该基准测试系统提供具有统计分析的性能测量能力,并支持多种输出格式,包括控制台和 JSON 报告。
有关测试框架的信息,请参阅 测试框架。有关开发工具的概述,请参阅 开发工具。
Deno 的基准测试系统遵循事件驱动架构,采用基于 Worker 的执行方式和灵活的报告器系统来格式化输出。
来源: cli/tools/bench/mod.rs429-498 cli/ops/bench.rs25-41 cli/tools/bench/mod.rs152-284
基准测试系统使用几个关键的数据结构来表示基准测试元数据、结果和执行状态。
| 结构 | 目的 | 关键字段 |
|---|---|---|
BenchDescription | 基准测试元数据 | id, name, origin, baseline, group, ignore, only, warmup |
BenchStats | 性能测量 | n, min, max, avg, p75, p99, p995, p999, high_precision |
BenchResult | 执行结果 | Ok(BenchStats) 或 Failed(Box<JsError>) |
BenchPlan | 执行计划 | total, origin, used_only, names |
BenchReport | 最终结果摘要 | total, failed, failures, measurements |
来源: cli/tools/bench/mod.rs78-87 cli/tools/bench/mod.rs89-103 cli/tools/bench/mod.rs104-129
使用特定的命名模式和文件扩展名来发现基准测试文件
来源: cli/tools/bench/mod.rs404-427 cli/tools/bench/mod.rs418-427
基准测试执行遵循从发现到报告的结构化流程
来源: cli/tools/bench/mod.rs287-402 cli/tools/bench/mod.rs181-284
操作系统将 JavaScript 基准测试代码与 Rust 执行基础设施连接起来。
来源: cli/ops/bench.rs95-134 cli/ops/bench.rs135-144 cli/ops/bench.rs146-152
报告系统为基准测试结果提供了多种输出格式。
| 报告器类型 | 输出格式 | 用例 |
|---|---|---|
ConsoleReporter | 人类可读的控制台输出 | 交互式开发 |
JsonReporter | 结构化 JSON | CI/CD 集成,数据分析 |
来源: cli/tools/bench/mod.rs142-150 cli/tools/bench/mod.rs322-389
基准测试系统通过文件监视功能支持持续执行。
来源: cli/tools/bench/mod.rs500-641 cli/util/file_watcher.rs247-268
基准测试系统与 Deno 更广泛的构建和模块系统集成,以进行依赖管理和类型检查。
CheckSpecifiersOptions 对基准测试模块进行类型检查CliMainWorkerFactory 集成以创建 WorkerPermissionsContainer 进行安全控制ModuleGraphCreator 进行监视模式下的依赖分析来源: cli/tools/bench/mod.rs466-476 cli/tools/bench/mod.rs568-575