#!/usr/bin/env bash # Measures what the Nx daemon costs and saves in this container: a few `nx` commands timed by hyperfine with and # without a warm daemon, then the daemon's memory. Ends with a Markdown report. # # nx-daemon-bench [runs] [warmup] (defaults: 10 runs after 3 warmups, per side and round) # # The commands: # - `nx show projects`: only the project graph, which the daemon keeps in memory. # - the same after a project changed: before each run a probe project is added or removed, so the graph is rebuilt. # - `:build` with `--skip-nx-cache`, so it times the tasks rather than cache hits. # - `:build` again with the cache, every task a hit: once with its outputs on disk (the daemon leaves outputs # that already match alone), once with the project's own git-ignored outputs deleted before each run, so they are # restored from the cache. # - `nx run-many -t verify` on a warm cache, with 3 runs after 1 warmup per side and round, since one takes ~40s. # # Worktree containers share the host's cores, so load drifts during a run: each command is timed in two rounds, in the # order no daemon, daemon, daemon, no daemon, and each side's runs are pooled, so a steady drift lands on both sides # equally. # # Memory is the resident set (RSS) of the daemon process, then of the daemon with its child processes (Nx runs plugins # in workers spawned by the daemon), read 3 times a minute apart after the timed runs and once more after an uncached # `nx run-many -t build`, followed by `docker stats` for every container of this worktree's compose project. Stops the # daemon at the end, as the devcontainer runs without it. set -euo pipefail probe="$(git rev-parse --show-toplevel)/nx-daemon-bench-probe" # Run by hyperfine before each run of the changed-graph case. if [ "${1:-}" = --toggle-probe ]; then if [ -e "$probe" ]; then rm -r "$probe" else mkdir "$probe" echo '{ "name": "nx-daemon-bench-probe" }' >"$probe/project.json" fi exit fi # Run by hyperfine before each run of the restored-build case, with the outputs to delete. if [ "${1:-}" = --delete-outputs ]; then shift rm -rf "$@" exit fi project="${1:?usage: nx-daemon-bench [runs] [warmup]}" runs="${2:-10}" warmup="${3:-3}" self="$(realpath "$0")" out="$(mktemp -d)" trap 'rm -rf "$probe"' EXIT daemon_pid() { nx daemon | sed -n 's/.*Process ID: //p' } # RSS in MB of the daemon alone, then summed with every process below it. memory() { local pid tree rss total pid="$(daemon_pid)" tree="$(ps -e -o pid=,ppid= | awk -v root="$pid" ' { parent[$1] = $2 } END { for (p in parent) { q = p; while (q && q != root) q = parent[q]; if (q == root) print p } }')" rss="$(ps -o rss= -p "$pid")" # shellcheck disable=SC2086 # one PID per word total="$(ps -o rss= -p "$(echo $tree | tr ' ' ,)" | awk '{ s += $1 } END { print s }')" echo "$((rss / 1024)) MB, $((total / 1024)) MB with children" >>"$out/memory.txt" printf '%s daemon %4d MB with children %4d MB (%d processes)\n' \ "$(date -u +%H:%M:%S)" $((rss / 1024)) $((total / 1024)) "$(echo "$tree" | wc -l)" } # The project's own build outputs that git ignores (so never a tracked file such as a generated tsconfig.json), from # the cacheable tasks in its build's task graph, one per line. build_outputs() { NX_DAEMON=false nx run "${project}:build" --graph=stdout | node --input-type=module -e ' import { execFileSync } from "node:child_process"; import { existsSync } from "node:fs"; const [project] = process.argv.slice(1); let json = ""; for await (const chunk of process.stdin) json += chunk; const { graph, tasks } = JSON.parse(json); const { root, targets } = graph.nodes[project].data; const outputs = Object.values(tasks.tasks) .filter(({ target }) => target.project === project && targets[target.target].cache !== false) .flatMap(task => task.outputs) .filter(path => path.startsWith(`${root}/`) && existsSync(path)); for (const path of new Set(outputs)) { try { execFileSync("git", ["check-ignore", "--quiet", path]); console.log(path); } catch {} } ' "$project" } # One hyperfine set: `measure [prepare]`, side no-daemon or daemon. measure() { local name="$1" side="$2" round="$3" set_runs="$4" set_warmup="$5" command="$6" prepare="${7:-}" local args=(--style basic --warmup "$set_warmup" --runs "$set_runs" --export-json "$out/$name.$side.$round.json") if [ -n "$prepare" ]; then args+=(--prepare "$prepare"); fi if [ "$side" = daemon ]; then nx daemon --start >/dev/null; else nx daemon --stop >/dev/null; fi hyperfine "${args[@]}" --env "NX_DAEMON=$([ "$side" = daemon ] && echo true || echo false)" \ --command-name "$name, $side, round $round" "$command" } # Every case in both rounds: `bench