tanzu-cli icon indicating copy to clipboard operation
tanzu-cli copied to clipboard

Inefficient startup

Open marckhouzam opened this issue 4 months ago • 0 comments

Bug description

The Tanzu CLI has an inefficient startup which becomes slower and slower as the number of configured contexts increases.

pkg/config/init.go makes a call to SyncContextsAndServers() in its init() function; this means SyncContextsAndServers() is called every time the CLI is executed. Within the SyncContextsAndServers() function, there is a loop that iterates over all the configured contexts, and for each one a call to config.SetContext() is made which will in turn persist the configuration. For each tanzu context, the configuration is persisted once, meaning writing to the config.yaml file and the config-ng.yaml file. For non-tanzu contexts, the configuration is actually persisted twice, which means writing to files 4 times.

I somehow accumulated 29 contexts in my configuration, which, because of the logic described above, make every command of the CLI noticeably slower.

Furthermore, the point of calling SyncContextsAndServers() is too handle certain scenarios where old plugins can modify the config.yaml file incorrectly, and the SyncContextsAndServers() will fix it. However, it the vast, vast majority of cases, this synchronization does nothing, although it still writes to the 2 configuration files multiple time and drastically slows down the CLI.

Below shows that with 27 contexts, a simple help printout takes a full second, while if I remove all contexts, it drops to close to 0.2 seconds.

$ tz version
version: v1.5.0
buildDate: 2024-09-26
sha: 9294e28
arch: arm64

$ tz context list | wc -l
[i] Use '--wide' to view additional columns.
      29

$ time tz -h> /dev/null
tanzu -h > /dev/null  0.91s user 0.07s system 105% cpu 0.924 total
$ mv ~/.config/tanzu/config* ~
overwrite /Users/kmarc/config-ng.yaml? (y/n [n]) y
overwrite /Users/kmarc/config.yaml? (y/n [n]) y
$ time tz -h> /dev/null
tanzu -h > /dev/null  0.06s user 0.02s system 37% cpu 0.222 total

Expected behavior

The execution time of CLI commands should not be affected so dramatically by the number of contexts configured.

Steps to reproduce the bug / Relevant debug output

Run time tanzu -h >/dev/null and keep creating more contexts and see the time increase steadily. Any speed slower than 0.5 seconds starts being very noticeable when doing shell completion.

Output of tanzu version

This started with CLI v1.2.0 and still affecting main today.

Environment where the bug was observed (cloud, OS, etc)

Locally on my ARM64 Mac.

marckhouzam avatar Oct 07 '24 02:10 marckhouzam