|
1 | 1 | # Case Study — Nx graph time |
2 | 2 |
|
3 | | -## Measured graphs |
| 3 | + |
| 4 | +## Tools and Setup |
| 5 | + |
| 6 | +### Setup Detection |
| 7 | + |
| 8 | +We use commands to capture the setup specification: |
| 9 | +- `nx report` - to capture the Nx, Node.js, and OS versions. |
| 10 | +- `nx reset` - to reset the Nx daemon and clear the cache before running measurements. |
| 11 | + |
| 12 | +### Environment |
| 13 | + |
| 14 | +We disable the Nx daemon and the Nx cache to ensure consistent measurements: |
| 15 | + |
| 16 | +- `NX_DAEMON=false` - Disables the Nx daemon, which can cache results and affect performance measurements. |
| 17 | +- `NX_CACHE=false` - Disables the Nx computation cache, ensuring that all computations are performed fresh without cached results. |
| 18 | +- `NX_TUI=false` - Disables the Nx TUI (Text User Interface) to avoid any potential interference with performance measurements. |
| 19 | + |
| 20 | +### CPU Profiling |
| 21 | + |
| 22 | +We use the native Node.js CPU profiling capabilities through the `--cpu-prof` and `--cpu-prof-dir` flags. |
| 23 | +The tool [@push-based/cpu-prof](https://www.npmjs.com/package/@push-based/cpu-prof) 🚀 provides a convenient CLI to run commands with CPU profiling enabled and to merge multiple CPU profiles into one. |
| 24 | + |
| 25 | +## Measurements |
4 | 26 |
|
5 | 27 | Use native Node.js CPU profiling to analyze where time is spent when calculating Nx graphs. |
6 | 28 |
|
7 | | -### Project Graph |
| 29 | +The following commands are executed from the root of a Nx workspace: |
| 30 | +- `npx nx graph --file=nx-project-graph.json` - project graph generation |
| 31 | + - `npx nx graph --groupByFolder --file=nx-project-graph-grouped.json` - project graph with grouped folders |
| 32 | + - Remove plugins form nx.json, then run `npx nx graph --file=nx-project-graph-no-plugins.json` - project graph without plugins |
| 33 | +- `npx nx graph --view=tasks --targets=<targets> --file=nx-task-graph-build.json` - task graph for specific target |
| 34 | +- `npx nx graph --affected --base=main --file=/tmp/nx-affected-graph-main.json` - affected graph calculation for specified base |
| 35 | + |
| 36 | +### Project Graph (nx graph) |
8 | 37 |
|
9 | | -Save setup specification: |
| 38 | +1. Save setup specification: |
10 | 39 |
|
11 | 40 | ```bash |
12 | | -nx report > ./profiles/nx-show-projects/nx-report.md |
| 41 | +mkdir -p ./profiles/nx-project-graph && \ |
| 42 | +nx reset && nx report > ./profiles/nx-project-graph/nx-report.md |
13 | 43 | ``` |
14 | 44 |
|
15 | | -Measure the project graph creation: |
| 45 | +2. Measure the project graph creation: |
16 | 46 |
|
17 | 47 | ```bash |
18 | 48 | # Prerequisite (once): install local deps so ./node_modules/.bin/nx exists |
19 | 49 | NX_DAEMON=false NX_CACHE=false \ |
20 | 50 | npx -y @push-based/cpu-prof \ |
21 | | ---cpu-prof-dir ./profiles/nx-show-projects \ |
22 | | -node ./node_modules/nx/bin/nx.js show projects --json |
| 51 | +--cpu-prof-dir ./profiles/nx-project-graph \ |
| 52 | +node ./node_modules/nx/bin/nx.js graph --file=nx-project-graph.json |
| 53 | +``` |
| 54 | + |
| 55 | + |
| 56 | +### Project Graph (grouped by folder) |
| 57 | + |
| 58 | +1. Save setup specification: |
| 59 | + |
| 60 | +```bash |
| 61 | +mkdir -p ./profiles/nx-project-graph-grouped && \ |
| 62 | +nx reset && nx report > ./profiles/nx-project-graph-grouped/nx-report.md |
| 63 | +``` |
| 64 | + |
| 65 | +2. Measure the project graph creation with grouped folders: |
| 66 | + |
| 67 | +```bash |
| 68 | +# Prerequisite (once): install local deps so ./node_modules/.bin/nx exists |
| 69 | +NX_DAEMON=false NX_CACHE=false \ |
| 70 | +npx -y @push-based/cpu-prof \ |
| 71 | +--cpu-prof-dir ./profiles/nx-project-graph-grouped \ |
| 72 | +node ./node_modules/nx/bin/nx.js graph --groupByFolder --file=nx-project-graph-grouped.json |
| 73 | +``` |
| 74 | + |
| 75 | + |
| 76 | +### Project Graph (no plugins) |
| 77 | + |
| 78 | +1. Save setup specification: |
| 79 | + |
| 80 | +```bash |
| 81 | +mkdir -p ./profiles/nx-project-graph-no-plugins && \ |
| 82 | +nx reset && nx report > ./profiles/nx-project-graph-no-plugins/nx-report.md |
| 83 | +``` |
| 84 | + |
| 85 | +2. Measure the project graph creation without plugins: |
| 86 | + |
| 87 | +```bash |
| 88 | +# Prerequisite (once): install local deps so ./node_modules/.bin/nx exists |
| 89 | +# Before running, remove plugins from nx.json |
| 90 | +NX_DAEMON=false NX_CACHE=false \ |
| 91 | +npx -y @push-based/cpu-prof \ |
| 92 | +--cpu-prof-dir ./profiles/nx-project-graph-no-plugins \ |
| 93 | +node ./node_modules/nx/bin/nx.js graph --file=nx-project-graph-no-plugins.json |
| 94 | +``` |
| 95 | + |
| 96 | + |
| 97 | +### Task Graph for specific target(s) |
| 98 | + |
| 99 | +1. Save setup specification: |
| 100 | + |
| 101 | +```bash |
| 102 | +mkdir -p ./profiles/nx-task-graph-build && \ |
| 103 | +nx reset && nx report > ./profiles/nx-task-graph-build/nx-report.md |
| 104 | +``` |
| 105 | + |
| 106 | +2. Measure the task graph creation for specific target(s): |
| 107 | + |
| 108 | +```bash |
| 109 | +# Prerequisite (once): install local deps so ./node_modules/.bin/nx exists |
| 110 | +NX_DAEMON=false NX_CACHE=false \ |
| 111 | +npx -y @push-based/cpu-prof \ |
| 112 | +--cpu-prof-dir ./profiles/nx-task-graph-build \ |
| 113 | +node ./node_modules/nx/bin/nx.js graph --view=tasks --targets=<targets> --file=nx-task-graph-build.json |
23 | 114 | ``` |
24 | 115 |
|
25 | | -### Task Graph |
26 | 116 |
|
27 | | -Save setup specification: |
| 117 | +### Affected Graph (base=main) |
| 118 | + |
| 119 | +1. Save setup specification: |
28 | 120 |
|
29 | 121 | ```bash |
30 | | -nx report > ./profiles/nx-show-project/nx-report.md |
| 122 | +mkdir -p ./profiles/nx-affected-graph-main && \ |
| 123 | +nx reset && nx report > ./profiles/nx-affected-graph-main/nx-report.md |
31 | 124 | ``` |
32 | 125 |
|
33 | | -Measure the task graph creation: |
| 126 | +2. Measure the affected graph calculation for the specified base: |
34 | 127 |
|
35 | 128 | ```bash |
36 | 129 | # Prerequisite (once): install local deps so ./node_modules/.bin/nx exists |
37 | 130 | NX_DAEMON=false NX_CACHE=false \ |
38 | 131 | npx -y @push-based/cpu-prof \ |
39 | | ---cpu-prof-dir ./profiles/nx-show-project \ |
40 | | -node ./node_modules/nx/bin/nx.js show project <project-name> --json |
| 132 | +--cpu-prof-dir ./profiles/nx-affected-graph-main \ |
| 133 | +node ./node_modules/nx/bin/nx.js graph --affected --base=main --file=/tmp/nx-affected-graph-main.json |
| 134 | +``` |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +### Project Graph |
| 139 | + |
| 140 | +1. Save setup specification: |
| 141 | + |
| 142 | +```bash |
| 143 | +mkdir -p ./profiles/nx-show-projects && \ |
| 144 | +nx reset && nx report > ./profiles/nx-show-projects/nx-report.md |
41 | 145 | ``` |
42 | 146 |
|
43 | | -| |
| 147 | +2. Measure the project graph creation: |
| 148 | + |
| 149 | +```bash |
| 150 | +# Prerequisite (once): install local deps so ./node_modules/.bin/nx exists |
| 151 | +NX_DAEMON=false NX_CACHE=false \ |
| 152 | +npx -y @push-based/cpu-prof \ |
| 153 | +--cpu-prof-dir ./profiles/nx-show-projects \ |
| 154 | +node ./node_modules/nx/bin/nx.js show projects --json |
| 155 | +``` |
0 commit comments