For AI agents: the complete documentation index is available at https://rstest.rs/llms.txt, the full documentation bundle is available at https://rstest.rs/llms-full.txt, and this page is available as Markdown at https://rstest.rs/guide/debug/profiling.md.
close
  • English
  • Profiling

    Use --trace, DEBUG=rstest, Rsdoctor, samply, and Node.js built-in profilers to measure where test time and memory go.

    Using Agent Skills

    Install the rstest-debugging skill when investigating performance regressions with a coding agent. It guides the agent to separate build startup from test execution before changing config or test code:

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

    Common performance bottlenecks

    Run --trace first to split build time from test execution time, then follow the matching branch:

    1. Build time dominates (common in jsdom or happy-dom, where node_modules dependencies are bundled by default instead of externalized like in node):
    2. Test execution dominates (fast startup, slow files or cases):

    Using --trace

    --trace breaks a run into per-phase, per-suite, and per-case slices. Use it when the reporter's single Duration number is not enough to tell where the time is spent.

    npx rstest run --trace

    The flag is opt-in. Each run records per-suite and per-case slices annotated with status and retry count, a heap counter sampled at each phase boundary, and one slice per phase of every test file:

    • prepare: set up the worker runtime, the global API, and the coverage provider when coverage is enabled.
    • envSetup: install the test environment. Close to zero for node, and when a non-isolated worker is already pinned to the environment.
    • load: receive the built assets of the test entry. The host-side cost of producing them is reported as a host span instead.
    • setupFiles: evaluate the configured setup files.
    • collect: evaluate the test module and build the suite and case tree, including deferred and async describe callbacks.
    • tests: run the cases and their hooks.
    • coverage: collect coverage data from the provider. Absent when coverage is disabled, or when the file bailed or threw before this point.
    • teardown: clean up the coverage provider and the worker state, plus the test environment when isolate is enabled.

    Node pools also emit a file:load summary slice covering worker-side asset loading, setup files, test module evaluation, and suite collection (matching TestFileResult.loadDuration).

    Browser Mode records only prepare and tests, measured on the host from the messages the page sends.

    Rstest writes two files to <rootPath>/.rstest/, and replaces them on every watch rerun:

    • trace-<timestamp>.summary.md: a markdown summary ranking the slowest phases, files, and test cases. The same content is printed to the terminal.
    • trace-<timestamp>.json: a Perfetto-compatible trace for the visual UI.

    Start with the summary. Open the trace when you also need to see how work overlapped across workers.

    Perfetto UI showing the trace produced by running npx rstest --trace inside the rstest repository

    In a TTY, Rstest starts a local helper server so that the printed link opens the trace directly in the Perfetto UI. In CI, the helper server is not started. Upload the trace file as an artifact and drag it into ui.perfetto.dev instead.

    Tip

    To investigate a slowdown with a Coding Agent, hand it both files: the summary for the ranking, the trace for the full event data. They pair with the rstest-debugging skill.

    Using Rsdoctor

    Rsdoctor is a build analysis tool that can visually display the compilation time of each loaders and plugins.

    When you need to debug Rstest's build outputs or build processes, you can use Rsdoctor for troubleshooting.

    Quick start

    In Rstest, you can enable @rsdoctor/core analysis as follows:

    1. Install Rsdoctor:
    npm
    yarn
    pnpm
    bun
    deno
    npm add @rsdoctor/core -D
    1. Add RSDOCTOR=true env variable before the CLI command:
    package.json
    {
      "scripts": {
        "test:rsdoctor": "RSDOCTOR=true rstest run"
      }
    }

    As Windows does not support the above usage, you can also use cross-env to set environment variables. This ensures compatibility across different systems:

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

    After running the above commands, Rstest will automatically register the Rsdoctor plugin, and after the build is completed, it will open the build analysis page. For complete features, please refer to Rsdoctor document.

    rsdoctor-rstest-outputs

    Profiler

    Check --trace first since it requires no external tooling. When you need lower-level data, pick a profiler based on what you are measuring:

    • CPU time distribution: use samply (recommended for profiling both the main process and forks/vmForks workers together) or --cpu-prof.
    • Memory allocation: use --heap-prof in Node.js profiling (samply does not collect heap data).
    • Step-through debugging: see Debugging test workers.

    Using samply

    Note

    Profiling Node.js code on macOS requires Node.js 22.16+.

    samply profiles the Rstest main process and test workers together.

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

    Rstest forwards host --perf-prof, --perf-basic-prof, and --interpreted-frames-native-stack flags to forks and vmForks workers. For threads and vmThreads, workers share the host Node.js process, so host-level --perf-prof* flags already apply process-wide (do not pass them via --pool.execArgv, as Node.js worker_threads rejects them with ERR_WORKER_INVALID_EXEC_ARGV).

    After the run finishes, samply opens the profile in the Firefox Profiler. Select the Node.js thread to inspect JavaScript execution time.

    rstest-samply-profiling

    Node.js profiling

    Rstest forwards --cpu-prof*, --heap-prof*, and --diagnostic-dir from the host node process to workers in all pools (forks, vmForks, threads, and vmThreads).

    To collect CPU profiles:

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

    To collect heap profiles:

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

    Open the generated .cpuprofile or .heapprofile files in Visual Studio Code or Chrome DevTools.

    Tip

    Host --inspect* flags are stripped from workers and attach only to the main Rstest process. To inspect or step through worker code, use --pool.execArgv=--inspect-brk; see Debugging test workers.