nx: Running job that hits cache and has cached outputs slowed down after upgrading to 16.9.0

Current Behavior

Hi!

In our monorepo we have build jobs that produce output files. When running build jobs with command nx run-many --target build, and every job hits local cache, the performance profile obtained following instructions from here looks as follows:

image

In total, build takes ~900 ms. Based on my testing we get similar numbers starting from nx version 16.9.0 and reproduces also in 17.0.0-beta.6.

Expected Behavior

Previously, the job was significantly faster under similar conditions, where every build job got local cache hit. Here is the same example when using nx version 16.8.1.

image

In total, build took ~20 ms. Therefore, the time it takes to complete job when getting a cache hit has increased by an order of magnitude.

GitHub Repo

No response

Steps to Reproduce

  1. Benchmark the time it takes to complete a job that hits cache and has cached output when nx version 16.8.1 is installed
  2. Repeat 1. with nx version 16.9.0 and observe the difference

Nx Report

>  NX   Report complete - copy this into the issue template

   Node   : 18.15.0
   OS     : linux-x64
   npm    : 9.5.0
   
   nx             : 16.9.0
   @nx/workspace  : 16.5.5
   @nx/devkit     : 16.8.1
   @nrwl/tao      : 16.6.0
   typescript     : 5.1.6
   ---------------------------------------
   Local workspace plugins:
         @XXX/local-nx-plugins
   ---------------------------------------
   The following packages should match the installed version of nx
     - @nx/workspace@16.5.5
     - @nrwl/workspace@16.5.5
     - @nx/devkit@16.8.1
     - @nrwl/devkit@16.8.1
     - @nrwl/tao@16.6.0
   
   To fix this, run `nx migrate nx@16.9.0`

Failure Logs

No response

Package Manager Version

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

We have observed this slowing down only in jobs that produce output files that are stored to cache.

My colleague - a MAC user, I use linux - reported even bigger slowing down in similar setting. For him running the build with every individual build job hitting local cache may take up to 10 seconds.

About this issue

  • Original URL
  • State: open
  • Created 8 months ago
  • Reactions: 4
  • Comments: 35 (12 by maintainers)

Most upvoted comments

Also seeing very slow cache restore times. A monorepo with with 100+ projects takes well over 30 seconds to run through a full suite of cache restores. With NX_DAEMON=false it takes less than two seconds.

Same issue here.

  • nx v17.2.8
  • NX_DAEMON=true – 128 seconds to run build across all projects
  • NX_DAMEON=false – down to 22.83 seconds.

Notable difference in output. NX_DAEMON=true, all task runs report as [existing outputs match the cache, left as is]. NX_DAEMON=false, most task runs report as [local cache]

What is the difference between local cache and existing outputs match the cache? I assume local cache is a cache hit based on matching hash, and existing outputs means the tasks actually ran, but the outputs are identical. I guess the only time you are saving is file writes in the latter scenario? In the former, no compilation takes place which is the majority of the cpu cycles being eaten.

Average number of files in dist/ is 27. The distribution is:

11
11
113
37
15
27
5
3
23

Here are the new logs with Nx 16.9.0:

[NX Daemon Server] - 2023-11-10T13:38:23.683Z - Established a connection. Number of open connections: 1
[NX Daemon Server] - 2023-11-10T13:38:23.683Z - Closed a connection. Number of open connections: 0
[NX Daemon Server] - 2023-11-10T13:38:23.684Z - Established a connection. Number of open connections: 1
[NX Daemon Server] - 2023-11-10T13:38:23.686Z - [REQUEST]: Client Request for Project Graph Received
[NX Daemon Server] - 2023-11-10T13:38:23.687Z - [REQUEST]: Responding to the client. project-graph
[NX Daemon Server] - 2023-11-10T13:38:23.690Z - Time taken for 'total for creating and serializing project graph' 0.4065070152282715ms
[NX Daemon Server] - 2023-11-10T13:38:23.693Z - Done responding to the client project-graph
[NX Daemon Server] - 2023-11-10T13:38:23.693Z - Handled REQUEST_PROJECT_GRAPH. Handling time: 1. Response time: 6.
[NX Daemon Server] - 2023-11-10T13:38:24.029Z - [REQUEST]: Responding to the client. handleHashTasks
[NX Daemon Server] - 2023-11-10T13:38:24.032Z - Done responding to the client handleHashTasks
[NX Daemon Server] - 2023-11-10T13:38:24.032Z - Handled HASH_TASKS. Handling time: 20. Response time: 3.
fileOutputs: 0.807ms
normalizeOutputs: 0.042ms
[ 'libs/logger/dist' ]
[NX Daemon Server] - 2023-11-10T13:38:24.101Z - [REQUEST]: Responding to the client. outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:24.101Z - Done responding to the client outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:24.101Z - Handled OUTPUTS_HASHES_MATCH. Handling time: 1. Response time: 0.
fileOutputs: 0.416ms
normalizeOutputs: 0.015ms
[
  'libs/service-bus-resources/dist/index.d.ts',
  'libs/service-bus-resources/dist/index.d.ts.map',
  'libs/service-bus-resources/dist/tsconfig.tsbuildinfo'
]
[NX Daemon Server] - 2023-11-10T13:38:24.104Z - [REQUEST]: Responding to the client. outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:24.104Z - Done responding to the client outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:24.104Z - Handled OUTPUTS_HASHES_MATCH. Handling time: 1. Response time: 0.
fileOutputs: 1.306ms
normalizeOutputs: 0.119ms
[ 'libs/common-utils/dist' ]
[NX Daemon Server] - 2023-11-10T13:38:24.107Z - [REQUEST]: Responding to the client. outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:24.107Z - Done responding to the client outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:24.107Z - Handled OUTPUTS_HASHES_MATCH. Handling time: 2. Response time: 0.
fileOutputs: 0.364ms
normalizeOutputs: 0.024ms
[ 'libs/logger/dist' ]
[NX Daemon Server] - 2023-11-10T13:38:24.109Z - [REQUEST]: Responding to the client. recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:24.110Z - Done responding to the client recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:24.110Z - Handled RECORD_OUTPUTS_HASH. Handling time: 0. Response time: 1.
fileOutputs: 0.235ms
normalizeOutputs: 0.021ms
[
  'libs/service-bus-resources/dist/index.d.ts',
  'libs/service-bus-resources/dist/index.d.ts.map',
  'libs/service-bus-resources/dist/tsconfig.tsbuildinfo'
]
[NX Daemon Server] - 2023-11-10T13:38:24.112Z - [REQUEST]: Responding to the client. recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:24.112Z - Done responding to the client recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:24.112Z - Handled RECORD_OUTPUTS_HASH. Handling time: 1. Response time: 0.
fileOutputs: 0.903ms
normalizeOutputs: 0.12ms
[ 'libs/common-utils/dist' ]
[NX Daemon Server] - 2023-11-10T13:38:24.115Z - [REQUEST]: Responding to the client. recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:24.115Z - Done responding to the client recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:24.115Z - Handled RECORD_OUTPUTS_HASH. Handling time: 2. Response time: 0.
fileOutputs: 0.633ms
normalizeOutputs: 0.04ms
[ 'libs/auth-client/dist' ]
[NX Daemon Server] - 2023-11-10T13:38:24.117Z - [REQUEST]: Responding to the client. outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:24.117Z - Done responding to the client outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:24.117Z - Handled OUTPUTS_HASHES_MATCH. Handling time: 1. Response time: 0.
fileOutputs: 1.159s
normalizeOutputs: 0.011ms
[]
[NX Daemon Server] - 2023-11-10T13:38:25.278Z - [REQUEST]: Responding to the client. outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:25.278Z - Done responding to the client outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:25.278Z - Handled OUTPUTS_HASHES_MATCH. Handling time: 1159. Response time: 0.
fileOutputs: 0.718ms
normalizeOutputs: 0.056ms
[ 'libs/wlo-database/dist' ]
[NX Daemon Server] - 2023-11-10T13:38:25.281Z - [REQUEST]: Responding to the client. outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:25.281Z - Done responding to the client outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:25.281Z - Handled OUTPUTS_HASHES_MATCH. Handling time: 1. Response time: 0.
fileOutputs: 0.522ms
normalizeOutputs: 0.031ms
[ 'libs/auth-client/dist' ]
[NX Daemon Server] - 2023-11-10T13:38:25.283Z - [REQUEST]: Responding to the client. recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:25.283Z - Done responding to the client recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:25.283Z - Handled RECORD_OUTPUTS_HASH. Handling time: 1. Response time: 0.
fileOutputs: 1.144s
normalizeOutputs: 0.01ms
[]
[NX Daemon Server] - 2023-11-10T13:38:26.428Z - [REQUEST]: Responding to the client. recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:26.429Z - Done responding to the client recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:26.429Z - Handled RECORD_OUTPUTS_HASH. Handling time: 1144. Response time: 1.
fileOutputs: 0.99ms
normalizeOutputs: 0.066ms
[ 'libs/wlo-database/dist' ]
[NX Daemon Server] - 2023-11-10T13:38:26.432Z - [REQUEST]: Responding to the client. recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:26.432Z - Done responding to the client recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:26.432Z - Handled RECORD_OUTPUTS_HASH. Handling time: 1. Response time: 0.
fileOutputs: 0.95ms
normalizeOutputs: 0.08ms
[ 'libs/config-engine/dist' ]
[NX Daemon Server] - 2023-11-10T13:38:26.435Z - [REQUEST]: Responding to the client. outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:26.435Z - Done responding to the client outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:26.435Z - Handled OUTPUTS_HASHES_MATCH. Handling time: 1. Response time: 0.
fileOutputs: 4.77ms
normalizeOutputs: 0.351ms
[ 'libs/common-ui/dist' ]
[NX Daemon Server] - 2023-11-10T13:38:26.442Z - [REQUEST]: Responding to the client. outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:26.442Z - Done responding to the client outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:26.442Z - Handled OUTPUTS_HASHES_MATCH. Handling time: 5. Response time: 0.
fileOutputs: 0.584ms
normalizeOutputs: 0.114ms
[ 'libs/common-test-utils/dist' ]
[NX Daemon Server] - 2023-11-10T13:38:26.444Z - [REQUEST]: Responding to the client. outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:26.445Z - Done responding to the client outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:26.445Z - Handled OUTPUTS_HASHES_MATCH. Handling time: 0. Response time: 1.
fileOutputs: 0.499ms
normalizeOutputs: 0.03ms
[ 'libs/config-engine/dist' ]
[NX Daemon Server] - 2023-11-10T13:38:26.447Z - [REQUEST]: Responding to the client. recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:26.447Z - Done responding to the client recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:26.447Z - Handled RECORD_OUTPUTS_HASH. Handling time: 1. Response time: 0.
fileOutputs: 2.441ms
normalizeOutputs: 0.242ms
[ 'libs/common-ui/dist' ]
[NX Daemon Server] - 2023-11-10T13:38:26.451Z - [REQUEST]: Responding to the client. recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:26.451Z - Done responding to the client recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:26.451Z - Handled RECORD_OUTPUTS_HASH. Handling time: 3. Response time: 0.
fileOutputs: 0.443ms
normalizeOutputs: 0.024ms
[ 'libs/common-test-utils/dist' ]
[NX Daemon Server] - 2023-11-10T13:38:26.453Z - [REQUEST]: Responding to the client. recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:26.453Z - Done responding to the client recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:26.453Z - Handled RECORD_OUTPUTS_HASH. Handling time: 1. Response time: 0.
fileOutputs: 1.206s
normalizeOutputs: 0.003ms
[]
[NX Daemon Server] - 2023-11-10T13:38:27.661Z - [REQUEST]: Responding to the client. outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:27.661Z - Done responding to the client outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:27.661Z - Handled OUTPUTS_HASHES_MATCH. Handling time: 1206. Response time: 0.
fileOutputs: 1.166s
normalizeOutputs: 0.019ms
[]
[NX Daemon Server] - 2023-11-10T13:38:28.829Z - [REQUEST]: Responding to the client. outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:28.829Z - Done responding to the client outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:28.829Z - Handled OUTPUTS_HASHES_MATCH. Handling time: 1166. Response time: 0.
fileOutputs: 1.149s
normalizeOutputs: 0.003ms
[]
[NX Daemon Server] - 2023-11-10T13:38:29.980Z - [REQUEST]: Responding to the client. outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:29.980Z - Done responding to the client outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:29.980Z - Handled OUTPUTS_HASHES_MATCH. Handling time: 1149. Response time: 0.
fileOutputs: 1.144s
normalizeOutputs: 0.003ms
[]
[NX Daemon Server] - 2023-11-10T13:38:31.127Z - [REQUEST]: Responding to the client. recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:31.127Z - Done responding to the client recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:31.127Z - Handled RECORD_OUTPUTS_HASH. Handling time: 1145. Response time: 0.
fileOutputs: 1.147s
normalizeOutputs: 0.003ms
[]
[NX Daemon Server] - 2023-11-10T13:38:32.275Z - [REQUEST]: Responding to the client. recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:32.275Z - Done responding to the client recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:32.275Z - Handled RECORD_OUTPUTS_HASH. Handling time: 1147. Response time: 0.
fileOutputs: 1.180s
normalizeOutputs: 0.002ms
[]
[NX Daemon Server] - 2023-11-10T13:38:33.457Z - [REQUEST]: Responding to the client. recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:33.457Z - Done responding to the client recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:33.457Z - Handled RECORD_OUTPUTS_HASH. Handling time: 1180. Response time: 0.
fileOutputs: 1.172s
normalizeOutputs: 0.002ms
[]
[NX Daemon Server] - 2023-11-10T13:38:34.631Z - [REQUEST]: Responding to the client. outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:34.631Z - Done responding to the client outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:34.631Z - Handled OUTPUTS_HASHES_MATCH. Handling time: 1173. Response time: 0.
fileOutputs: 1.146s
normalizeOutputs: 0.002ms
[]
[NX Daemon Server] - 2023-11-10T13:38:35.779Z - [REQUEST]: Responding to the client. outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:35.779Z - Done responding to the client outputsHashesMatch
[NX Daemon Server] - 2023-11-10T13:38:35.779Z - Handled OUTPUTS_HASHES_MATCH. Handling time: 1147. Response time: 0.
fileOutputs: 1.160s
normalizeOutputs: 0.002ms
[]
[NX Daemon Server] - 2023-11-10T13:38:36.940Z - [REQUEST]: Responding to the client. recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:36.940Z - Done responding to the client recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:36.940Z - Handled RECORD_OUTPUTS_HASH. Handling time: 1160. Response time: 0.
fileOutputs: 1.174s
normalizeOutputs: 0.002ms
[]
[NX Daemon Server] - 2023-11-10T13:38:38.115Z - [REQUEST]: Responding to the client. recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:38.115Z - Done responding to the client recordOutputsHash
[NX Daemon Server] - 2023-11-10T13:38:38.115Z - Handled RECORD_OUTPUTS_HASH. Handling time: 1174. Response time: 0.
[NX Daemon Server] - 2023-11-10T13:38:38.135Z - Closed a connection. Number of open connections: 0

πŸ‘ the issues could be related. Let’s see if Cammisuli or someone else from Nx has further advise and if these two issues should be split to separate github issues or not

I understand what you mean, but my problem here is that when I turn on / off the daemon, with all the caches are hit. Why is the running speed much slower when the daemon process is turned on? This question is the same as this origin issue.

The cpuprofile shows there are multiple idle when daemon process is turned on.

  • daemon off image

  • daemon on image

Since daemon on is the default beheviour, I wonder if this meets your expect? I know the daemon will spawn some processes to watch on the file changes, but now this optimization for large projects seems to have slowed down the running speed of small and medium-sized projects a lot.

I could have been more clear. I meant the output configurations in nx.json. Here are the results I get:

Nx 16.8.1

nx run-many --target=type-check --output-style=static takes 2.0 seconds with configuration

"type-check": {
      "inputs": ["type-check", "^type-check"],
      "dependsOn": ["^type-check"],
      "outputs": ["{projectRoot}/dist/"]
    },

And, nx run-many --target=type-check --output-style=static takes 2.0 seconds with configuration

"type-check": {
      "inputs": ["type-check", "^type-check"],
      "dependsOn": ["^type-check"]
    },

The execution time is the same with full cache hit.

Nx 16.9.0

nx run-many --target=type-check --output-style=static takes 31.2 seconds (!) with configuration

"type-check": {
      "inputs": ["type-check", "^type-check"],
      "dependsOn": ["^type-check"],
      "outputs": ["{projectRoot}/dist/"]
    },

And, nx run-many --target=type-check --output-style=static takes 2.0 seconds with configuration

"type-check": {
      "inputs": ["type-check", "^type-check"],
      "dependsOn": ["^type-check"]
    },

Having the outputs defined takes 10x more time!

du -sh node_modules/.cache/nx says 4.6M so the size of the output files should not be the problem.

@Cammisuli if it helps, I can drop few console.time statements somewhere in node_modules/nx to help pin point the problem area. Just let me know where