deno.com
本页内容

deno bench,基准测试工具

命令行用法

deno bench [OPTIONS] [files]... [-- [SCRIPT_ARG]...]

使用 Deno 内置的基准测试工具运行基准测试。

评估给定文件,运行所有使用 'Deno.bench()' 声明的基准测试,并将结果报告到标准输出。

deno bench src/fetch_bench.ts src/signal_bench.ts

如果您指定一个目录而不是文件,路径将扩展到所有符合 glob 模式 {*_,*.,}bench.{js,mjs,ts,mts,jsx,tsx} 的文件。

deno bench src/

类型检查选项 跳转到标题

--check 跳转到标题

设置类型检查行为。此子命令默认对本地模块进行类型检查,因此添加 --check 是多余的。如果提供值 "all",则会包含远程模块。或者,可以使用 'deno check' 子命令。

--no-check 跳转到标题

跳过类型检查。如果提供值 "remote",则会忽略来自远程模块的诊断错误。

依赖管理选项 跳转到标题

--cached-only 跳转到标题

要求远程依赖项已缓存。

--frozen 跳转到标题

如果锁定文件过期,则报错。

--import-map 跳转到标题

从本地文件或远程 URL 加载导入映射文件。

--lock 跳转到标题

检查指定的锁文件。(如果未提供值,则默认为 "./deno.lock")。

--no-lock 跳转到标题

禁用锁定文件的自动发现。

--no-npm 跳转到标题

不解析 npm 模块。

--no-remote 跳转到标题

不解析远程模块。

--node-modules-dir 跳转到标题

设置 npm 包的 node 模块管理模式。

--reload 跳转到标题

短标记:-r

重新加载源代码缓存(重新编译 TypeScript)。不带值:重新加载所有模块。jsr:@std/http/file-server,jsr:@std/assert/assert-equals:重新加载特定模块。npm::重新加载所有 npm 模块。npm:chalk:重新加载特定的 npm 模块。

--vendor 跳转到标题

切换远程模块的本地 vendor 文件夹使用和 npm 包的 node_modules 文件夹使用。

选项 跳转到标题

--allow-scripts 跳转到标题

允许为给定包运行 npm 生命周期脚本。注意:脚本只在使用 node_modules 目录时执行(--node-modules-dir)。

--cert 跳转到标题

从 PEM 编码文件中加载证书颁发机构。

--conditions 跳转到标题

使用此参数指定 npm 包导出的自定义条件。您也可以使用 DENO_CONDITIONS 环境变量。

--config 跳转到标题

短标记:-c

配置 Deno 的不同方面,包括 TypeScript、linting 和代码格式化。通常配置文件会命名为 deno.jsondeno.jsonc 并自动检测;在这种情况下,此标志不是必需的。

--env-file 跳转到标题

从本地文件加载环境变量。只使用具有给定键的第一个环境变量。现有的进程环境变量不会被覆盖,因此如果环境中已存在同名变量,则会保留其值。如果您的 .env 文件中存在同一环境变量的多个声明,则应用第一个遇到的声明。这取决于您作为参数传递的文件的顺序。

--ext 跳转到标题

设置所提供文件的内容类型。

--filter 跳转到标题

在基准测试名称中使用此字符串或正则表达式模式运行基准测试。

--ignore 跳转到标题

忽略文件。

--json 跳转到标题

不稳定:以 JSON 格式输出基准测试结果。

--location 跳转到标题

某些 Web API 使用的 `globalThis.location` 值。

--no-config 跳转到标题

禁用自动加载配置文件。

--no-run 跳转到标题

缓存基准测试模块,但不运行基准测试。

--permit-no-files 跳转到标题

如果未找到文件,则不返回错误代码。

--preload 跳转到标题

在主模块之前执行的文件列表。

--seed 跳转到标题

设置随机数生成器种子。

--v8-flags 跳转到标题

要查看所有可用标志的列表,请使用 --v8-flags=--help。标志也可以通过 DENO_V8_FLAGS 环境变量设置。使用此标志设置的任何标志都将附加在 DENO_V8_FLAGS 环境变量之后。

文件监听选项 跳转到标题

--no-clear-screen 跳转到标题

在监视模式下不清除终端屏幕。

--watch 跳转到标题

监听文件更改并自动重启进程。只监听入口点模块图中本地文件。

--watch-exclude 跳转到标题

从监视模式中排除提供的文件/模式。

快速入门 跳转到标题

首先,让我们创建一个文件 url_bench.ts 并使用 Deno.bench() 函数注册一个基准测试。

// url_bench.ts
Deno.bench("URL parsing", () => {
  new URL("https://deno.land");
});

其次,使用 deno bench 子命令运行基准测试。

deno bench url_bench.ts
cpu: Apple M1 Max
runtime: deno 1.21.0 (aarch64-apple-darwin)

file:///dev/deno/url_bench.ts
benchmark        time (avg)             (min … max)       p75       p99      p995
--------------------------------------------------- -----------------------------
URL parsing   17.29 µs/iter  (16.67 µs … 153.62 µs)  17.25 µs  18.92 µs  22.25 µs

编写基准测试 跳转到标题

要定义基准测试,您需要通过调用 Deno.bench API 来注册它。此 API 有多个重载,以提供最大的灵活性和轻松在不同形式之间切换(例如,当您需要快速专注于单个基准测试进行调试时,可以使用 only: true 选项)。

// Compact form: name and function
Deno.bench("hello world #1", () => {
  new URL("https://deno.land");
});

// Compact form: named function.
Deno.bench(function helloWorld3() {
  new URL("https://deno.land");
});

// Longer form: bench definition.
Deno.bench({
  name: "hello world #2",
  fn: () => {
    new URL("https://deno.land");
  },
});

// Similar to compact form, with additional configuration as a second argument.
Deno.bench("hello world #4", { permissions: { read: true } }, () => {
  new URL("https://deno.land");
});

// Similar to longer form, with bench function as a second argument.
Deno.bench(
  { name: "hello world #5", permissions: { read: true } },
  () => {
    new URL("https://deno.land");
  },
);

// Similar to longer form, with a named bench function as a second argument.
Deno.bench({ permissions: { read: true } }, function helloWorld6() {
  new URL("https://deno.land");
});

异步函数 跳转到标题

您也可以通过传入一个返回 Promise 的基准测试函数来对异步代码进行基准测试。为此,您可以在定义函数时使用 async 关键字。

Deno.bench("async hello world", async () => {
  await 1;
});

关键部分 跳转到标题

有时基准测试用例需要包含设置和拆卸代码,这些代码可能会污染基准测试结果。例如,如果您想测量读取一个小文件所需的时间,您需要打开文件、读取它,然后关闭它。如果文件足够小,打开和关闭文件所需的时间可能超过读取文件本身所需的时间。

为了帮助处理这种情况,您可以使用 Deno.BenchContext.startDeno.BenchContext.end 来告诉基准测试工具您想要测量的关键部分。这两个调用之间部分之外的所有内容都将从测量中排除。

Deno.bench("foo", async (b) => {
  // Open a file that we will act upon.
  using file = await Deno.open("a_big_data_file.txt");

  // Tell the benchmarking tool that this is the only section you want
  // to measure.
  b.start();

  // Now let's measure how long it takes to read all of the data from the file.
  await new Response(file.readable).arrayBuffer();

  // End measurement here.
  b.end();
});

上述示例需要 --allow-read 标志来运行基准测试:deno bench --allow-read file_reading.ts

分组和基准线 跳转到标题

注册基准测试用例时,可以使用 Deno.BenchDefinition.group 选项将其分配给一个组。

// url_bench.ts
Deno.bench("url parse", { group: "url" }, () => {
  new URL("https://deno.land");
});

将多个用例分配到单个组并比较它们与“基准线”用例的性能非常有用。

在此示例中,我们将检查 Date.now() 相较于 performance.now() 的性能表现,为此,我们将使用 Deno.BenchDefinition.baseline 选项将第一个用例标记为“基准线”。

// time_bench.ts
Deno.bench("Date.now()", { group: "timing", baseline: true }, () => {
  Date.now();
});

Deno.bench("performance.now()", { group: "timing" }, () => {
  performance.now();
});
$ deno bench time_bench.ts
cpu: Apple M1 Max
runtime: deno 1.21.0 (aarch64-apple-darwin)

file:///dev/deno/time_bench.ts
benchmark              time (avg)             (min … max)       p75       p99      p995
--------------------------------------------------------- -----------------------------
Date.now()         125.24 ns/iter (118.98 ns … 559.95 ns) 123.62 ns 150.69 ns 156.63 ns
performance.now()    2.67 µs/iter     (2.64 µs … 2.82 µs)   2.67 µs   2.82 µs   2.82 µs

summary
  Date.now()
   21.29x times faster than performance.now()

您可以在同一个文件中指定多个组。

运行基准测试 跳转到标题

要运行基准测试,请使用包含您的基准测试函数的文件调用 deno bench。您也可以省略文件名,在这种情况下,当前目录中(递归地)所有符合 glob 模式 {*_,*.,}bench.{ts, tsx, mts, js, mjs, jsx} 的基准测试都将运行。如果您传递一个目录,该目录中所有符合此 glob 模式的文件都将运行。

该 glob 模式扩展为

  • 名为 bench.{ts, tsx, mts, js, mjs, jsx} 的文件,
  • 或以 .bench.{ts, tsx, mts, js, mjs, jsx} 结尾的文件,
  • 或以 _bench.{ts, tsx, mts, js, mjs, jsx} 结尾的文件。
# Run all benches in the current directory and all sub-directories
deno bench

# Run all benches in the util directory
deno bench util/

# Run just my_bench.ts
deno bench my_bench.ts

⚠️ 如果您想将额外的 CLI 参数传递给基准测试文件,请使用 -- 来告知 Deno 剩余参数是脚本参数。

# Pass additional arguments to the bench file
deno bench my_bench.ts -- -e --foo --bar

deno bench 使用与 deno run 相同的权限模型,因此在基准测试期间写入文件系统将需要,例如,--allow-write

要查看 deno bench 的所有运行时选项,您可以参考命令行帮助。

deno help bench

过滤 跳转到标题

有多种选项可以过滤您正在运行的基准测试。

命令行过滤 跳转到标题

可以使用命令行 --filter 选项单独或分组运行基准测试。

过滤标志接受字符串或模式作为值。

假设有以下基准测试:

Deno.bench({
  name: "my-bench",
  fn: () => {/* bench function zero */},
});
Deno.bench({
  name: "bench-1",
  fn: () => {/* bench function one */},
});
Deno.bench({
  name: "bench2",
  fn: () => {/* bench function two */},
});

此命令将运行所有这些基准测试,因为它们都包含“bench”一词。

deno bench --filter "bench" benchmarks/

另一方面,以下命令使用模式,将运行第二个和第三个基准测试。

deno bench --filter "/bench-*\d/" benchmarks/

要让 Deno 知道您想使用模式,请像 JavaScript 正则表达式的语法糖一样,用斜杠将您的过滤器包裹起来。

基准定义过滤 跳转到标题

在基准测试本身内部,您有两种过滤选项。

过滤掉(忽略这些基准测试) 跳转到标题

有时您希望根据某些条件(例如,您只想在 Windows 上运行基准测试)忽略基准测试。为此,您可以在基准定义中使用 ignore 布尔值。如果设置为 true,则基准测试将被跳过。

Deno.bench({
  name: "bench windows feature",
  ignore: Deno.build.os !== "windows",
  fn() {
    // do windows feature
  },
});

过滤入(只运行这些基准测试) 跳转到标题

有时您可能正在处理一个大型基准测试类中的性能问题,并且希望暂时只关注该单个基准测试而忽略其余部分。为此,您可以使用 only 选项来告诉基准测试工具只运行设置此项为 true 的基准测试。多个基准测试可以设置此选项。虽然基准测试运行会报告每个基准测试的成功或失败,但如果任何基准测试被标记为 only,则整个基准测试运行将始终失败,因为这只是一个临时措施,它会禁用几乎所有其他基准测试。

Deno.bench({
  name: "Focus on this bench only",
  only: true,
  fn() {
    // bench complicated stuff
  },
});

JSON 输出 跳转到标题

要以 JSON 格式检索输出,请使用 --json 标志。

$ deno bench --json bench_me.js
{
  "runtime": "Deno/1.31.0 x86_64-apple-darwin",
  "cpu": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz",
  "benches": [
    "origin": "file:///dev/bench_me.js",
    "group": null,
    "name": "Deno.UnsafePointerView#getUint32",
    "baseline": false,
    "result": {
      "ok": {
        "n": 49,
        "min": 1251.9348,
        "max": 1441.2696,
        "avg": 1308.7523755102038,
        "p75": 1324.1055,
        "p99": 1441.2696,
        "p995": 1441.2696,
        "p999": 1441.2696
      }
    }
  ]
}

您找到所需内容了吗?

隐私政策