For AI agents: the complete documentation index is available at https://rstest.rs/zh/llms.txt, the full documentation bundle is available at https://rstest.rs/zh/llms-full.txt, and this page is available as Markdown at https://rstest.rs/zh/guide/debug/profiling.md.
close
  • 简体中文
  • 性能分析

    通过 --trace、DEBUG=rstest、Rsdoctor、samply 和 Node.js 内置 profiler 定位测试耗时与内存瓶颈。

    使用 Agent Skills

    使用 Coding Agent 排查性能回归时,可以安装 rstest-debugging skill,引导 Agent 在修改配置或测试代码前先区分瓶颈位于构建启动还是测试执行阶段:

    npx skills add rstackjs/agent-skills --skill rstest-debugging

    常见性能瓶颈

    先使用 --trace 运行一次测试以区分构建耗时与测试执行耗时,再按对应分支排查:

    1. 构建耗时占主导(常见于 jsdom 或 happy-dom 等类浏览器环境,默认会打包 node_modules 依赖而非像 node 环境那样 externalize):
    2. 测试执行耗时占主导(启动快,但单个文件或用例慢):

    使用 --trace

    --trace 会把一次运行按 phase、suite、case 拆分成 slices。当 reporter 末尾的 Duration 单值不足以判断时间分布时,可以使用它定位瓶颈。

    npx rstest run --trace

    --trace 是可选开关。开启后每次运行会记录带 status 和 retry count 标注的 suite 与 case slices,在每个 phase 边界采样一次 heap 计数,并为每个测试文件的各个 phase 生成一个 slice:

    • prepare:初始化 worker 运行时、全局 API,以及启用覆盖率时的 coverage provider。
    • envSetup:初始化 test environment。node 环境下,以及非 isolate 的 worker 已锁定该环境时,耗时接近 0。
    • load:接收该测试入口的构建产物。host 侧生成产物的耗时会记录为 host span。
    • setupFiles:执行配置的 setup 文件。
    • collect:执行测试模块并构建 suite 与 case 树,包含延迟执行和异步的 describe 回调。
    • tests:运行用例及其 hooks。
    • coverage:从 provider 收集覆盖率数据。未启用覆盖率,或该文件在此之前 bail 或抛错时,不会出现。
    • teardown:清理 coverage provider 和 worker 状态;启用 isolate 时还会清理 test environment。

    Node pool 还会记录 file:load 汇总区间,覆盖 worker 内的产物加载、setup 文件执行、测试模块求值与 suite 收集(对应 TestFileResult.loadDuration)。

    浏览器模式只记录 prepare 和 tests 两个 phase,由 host 根据页面发来的消息计时。

    Rstest 会向 <rootPath>/.rstest/ 写入两个文件,并在 watch 模式每次重新运行时替换它们:

    • trace-<timestamp>.summary.md:对最慢的 phase、文件和测试用例排序的 markdown 摘要,相同内容会同时打印到终端。
    • trace-<timestamp>.json:Perfetto-compatible 的 trace,用于可视化 UI。

    建议先看摘要。当你还需要观察多个 worker 之间的工作如何重叠时,再打开 trace。

    Perfetto UI 中显示在 rstest 仓库执行 npx rstest --trace 产出的 trace

    在 TTY 下,Rstest 会启动一个本地 helper server,使打印出的链接直接在 Perfetto UI 中打开 trace。在 CI 下不会启动该 server,此时可以将 trace 文件作为 artifact 上传,再拖入 ui.perfetto.dev 查看。

    Tip

    借助 Coding Agent 排查性能问题时,把两个文件都交给它:摘要给出排序结果,trace 包含完整事件数据。可配合 rstest-debugging 技能使用。

    使用 Rsdoctor

    Rsdoctor 是一款为 Rspack 生态量身打造的构建分析工具。

    当你需要调试 Rstest 的构建产物或构建过程时,可以借助 Rsdoctor 来提升排查问题的效率。

    快速上手

    在 Rstest 中,你可以通过 @rsdoctor/core 开启 Rsdoctor 分析:

    1. 安装 Rsdoctor:
    npm
    yarn
    pnpm
    bun
    deno
    npm add @rsdoctor/core -D
    1. 在 CLI 命令前添加 RSDOCTOR=true 环境变量:
    package.json
    {
      "scripts": {
        "test:rsdoctor": "RSDOCTOR=true rstest run"
      }
    }

    由于 Windows 不支持上述用法,你也可以使用 cross-env 来设置环境变量,这可以确保在不同的操作系统中都能正常使用:

    package.json
    {
      "scripts": {
        "test:rsdoctor": "cross-env RSDOCTOR=true rstest run"
      },
      "devDependencies": {
        "cross-env": "^7.0.0"
      }
    }

    在项目内执行上述命令后,Rstest 会自动注册 Rsdoctor 的插件,并在构建完成后打开本次构建的分析页面,请参考 Rsdoctor 文档 来了解完整功能。

    rsdoctor-rstest-outputs

    Profiler

    可以先确认 --trace 是否已经能定位问题,它不需要额外工具。需要更底层的采样数据时,按分析目标选择:

    • CPU 耗时分布:使用 samply(可同时分析 Rstest 主进程与 forks / vmForks worker 进程)或 --cpu-prof。
    • 内存分配:使用 Node.js profiling 中的 --heap-prof(samply 不采集内存数据)。
    • 断点 / 单步调试:见调试测试 worker。

    使用 samply

    Note

    在 macOS 上对 Node.js 代码进行 profiling 需要 Node.js 22.16+。

    samply 支持同时对 Rstest 主进程和测试 worker 进行性能分析:

    samply record -- node --perf-prof --perf-basic-prof --interpreted-frames-native-stack ./node_modules/@rstest/core/bin/rstest.js run

    Rstest 会将主进程上的 --perf-prof、--perf-basic-prof 和 --interpreted-frames-native-stack 透传给 forks 和 vmForks worker。对于 threads 和 vmThreads,worker 与主进程共享同一个 Node.js 进程,主进程上的这些 V8 参数已经覆盖整个进程(不要通过 --pool.execArgv 传入它们,Node.js worker_threads 会抛出 ERR_WORKER_INVALID_EXEC_ARGV)。

    执行结束后,samply 会自动在 Firefox Profiler 中打开采样结果,选择 Node.js 线程即可查看 JavaScript 侧的耗时分布。

    rstest-samply-profiling

    Node.js profiling

    Rstest 会将主进程上的 --cpu-prof*、--heap-prof* 和 --diagnostic-dir 透传给所有 pool(forks、vmForks、threads 和 vmThreads)的 worker。

    采集 CPU profile:

    node --cpu-prof --cpu-prof-dir=./cpu-prof ./node_modules/@rstest/core/bin/rstest.js run

    采集堆内存 profile:

    node --heap-prof --heap-prof-dir=./heap-prof ./node_modules/@rstest/core/bin/rstest.js run

    生成的 .cpuprofile 或 .heapprofile 文件可使用 Visual Studio Code 或 Chrome DevTools 打开分析。

    Tip

    主进程上的 --inspect* 参数会被 worker 过滤,仅作用于 Rstest 主进程。如需单步调试测试 worker 代码,请使用 --pool.execArgv=--inspect-brk,详见调试测试 worker。