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/

型別檢查選項 Jump to heading

--check Jump to heading

設定型別檢查行為。此子命令預設會對本機模組執行型別檢查,因此加入 --check 是多餘的。如果提供 "all" 值,則會包含遠端模組。或者,可以使用 'deno check' 子命令。

--no-check Jump to heading

略過型別檢查。如果提供 "remote" 值,則會忽略來自遠端模組的診斷錯誤。

相依性管理選項 Jump to heading

--cached-only Jump to heading

要求遠端相依性已快取。

--frozen Jump to heading

如果 lockfile 過期則輸出錯誤。

--import-map Jump to heading

從本機檔案或遠端 URL 載入 import map 檔案。

--lock Jump to heading

檢查指定的鎖定檔。(如果未提供值,則預設為 "./deno.lock")。

--no-lock Jump to heading

停用自動探索鎖定檔。

--no-npm Jump to heading

不解析 npm 模組。

--no-remote Jump to heading

不解析遠端模組。

--node-modules-dir Jump to heading

設定 npm 套件的 node modules 管理模式。

--reload Jump to heading

簡短旗標:-r

重新載入原始碼快取(重新編譯 TypeScript)無值:重新載入所有內容 jsr:@std/http/file-server,jsr:@std/assert/assert-equals:重新載入特定模組 npm::重新載入所有 npm 模組 npm:chalk:重新載入特定 npm 模組。

--vendor Jump to heading

切換本機 vendor 資料夾用於遠端模組,以及 node_modules 資料夾用於 npm 套件的用法。

選項 Jump to heading

--allow-scripts Jump to heading

允許為給定的套件執行 npm 生命周期腳本。注意:腳本僅在使用 node_modules 目錄 (--node-modules-dir) 時執行。

--cert Jump to heading

從 PEM 編碼檔案載入憑證授權單位。

--config Jump to heading

簡短旗標:-c

設定 deno 的不同方面,包括 TypeScript、程式碼檢查和程式碼格式化。通常,設定檔會命名為 deno.jsondeno.jsonc 並自動偵測;在這種情況下,此旗標不是必要的。

--env-file Jump to heading

從本機檔案載入環境變數。僅使用具有給定金鑰的第一個環境變數。現有的程序環境變數不會被覆寫,因此如果環境中已存在同名的變數,則其值將被保留。如果您的 .env 檔案中存在同一個環境變數的多個宣告,則會套用第一個遇到的宣告。這由您作為引數傳遞的檔案順序決定。

--ext Jump to heading

設定提供的檔案的內容類型。

--filter Jump to heading

執行基準名稱中包含此字串或 regexp 模式的基準測試。

--ignore Jump to heading

忽略檔案。

--json Jump to heading

UNSTABLE:以 JSON 格式輸出基準測試結果。

--location Jump to heading

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

--no-config Jump to heading

停用自動載入設定檔。

--no-run Jump to heading

快取基準模組,但不執行基準測試。

--seed Jump to heading

設定亂數產生器種子。

--v8-flags Jump to heading

若要查看所有可用的旗標清單,請使用 --v8-flags=--help。旗標也可以透過 DENO_V8_FLAGS 環境變數設定。使用此旗標設定的任何旗標都會附加在 DENO_V8_FLAGS 環境變數之後。

檔案監看選項 Jump to heading

--no-clear-screen Jump to heading

在監看模式下,不要清除終端機螢幕。

--watch Jump to heading

監看檔案變更並自動重新啟動程序。僅監看進入點模組圖中的本機檔案。

--watch-exclude Jump to heading

從監看模式排除提供的檔案/模式。

快速開始 Jump to heading

首先,讓我們建立一個檔案 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

撰寫效能基準測試 Jump to heading

若要定義基準測試,您需要透過呼叫 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");
});

非同步函式 Jump to heading

您也可以透過傳遞一個傳回 Promise 的基準測試函式來基準測試非同步程式碼。為此,您可以在定義函式時使用 async 關鍵字

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

關鍵區段 Jump to heading

有時,基準測試案例需要包含設定和拆解程式碼,這些程式碼可能會污染基準測試結果。例如,如果您想測量讀取小檔案需要多長時間,您需要開啟檔案、讀取它,然後關閉它。如果檔案夠小,則開啟和關閉檔案所需的時間可能會超過讀取檔案本身所需的時間。

為了協助處理這種情況,您可以使用 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

分組與基準 Jump to heading

註冊基準測試案例時,可以使用 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()

您可以在同一個檔案中指定多個群組。

執行效能基準測試 Jump to heading

若要執行基準測試,請使用包含基準測試函式的檔案呼叫 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

篩選 Jump to heading

有多個選項可以篩選您正在執行的基準測試。

命令列篩選 Jump to heading

可以使用命令列 --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 正則表示式的語法糖一樣。

基準定義篩選 Jump to heading

在基準測試本身中,您有兩個篩選選項。

篩選排除(忽略這些基準) Jump to heading

有時您想要根據某種條件忽略基準測試(例如,您只想在 Windows 上執行基準測試)。為此,您可以使用基準測試定義中的 ignore 布林值。如果設定為 true,則會跳過基準測試。

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

篩選包含(僅執行這些基準) Jump to heading

有時,您可能正處於大型基準測試類別中的效能問題中,並且您想要專注於該單一基準測試,並暫時忽略其餘基準測試。為此,您可以使用 only 選項來告知基準測試框架僅執行將此選項設定為 true 的基準測試。多個基準測試可以設定此選項。雖然基準測試執行將報告每個基準測試的成功或失敗,但如果任何基準測試標記為 only,則整體基準測試執行將始終失敗,因為這僅是暫時措施,它會停用幾乎所有基準測試。

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

JSON 輸出 Jump to heading

若要以 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
      }
    }
  ]
}

您找到需要的資訊了嗎?

隱私權政策