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

Improve startup speed

Open marckhouzam opened this issue 4 months ago • 0 comments

There is currently an issue with this change where the config.yaml file does not get cleaned up of "contexts" after running tanzu cluster version with cluster:v0.25.4. I will investigate when time allows

Unit tests are not added yet. I wanted to get opinions first.

What this PR does / why we need it

This PR avoids writing to the config file on startup if not necessary.

Writing to the config file to update the server list has a cost for each context. I ended up with 27 contexts configured, which triggered writing to file over 50 times which added 0.65 seconds on my M1 Mac, making the startup jump from around 0.35 seconds to a full second. Having the CLI take a full second to execute any command implies that each call to shell completion takes at least 1 second, which is slow for the user.

Updating the server list does not actually need to be done all the time. Instead, this commit checks if there is actually any missing servers by comparing the list of contexts with the list of servers, and only if there are missing servers will it trigger the update.

This reduces the execution time for commands that don't access the backend; this means that shell completion for commands and flags becomes noticeably more responsive when a user has multiple contexts configured..

Which issue(s) this PR fixes

Fixes #823

Describe testing done for PR

Before the PR:

# On commit e7127fcf5
$ time tz __complete ''>/dev/null
Completion ended with directive: ShellCompDirectiveNoFileComp
tanzu __complete '' > /dev/null  0.85s user 0.08s system 90% cpu 1.025 total

With this PR:

$ time tz __complete ''>/dev/null
Completion ended with directive: ShellCompDirectiveNoFileComp
tanzu __complete '' > /dev/null  0.14s user 0.03s system 43% cpu 0.378 total

I also ran some normal shell completion down the command tree, and the responsiveness is noticeably better.

Functional tests about contexts and servers to be added

Release note

Improve shell completion responsiveness

Additional information

Special notes for your reviewer

marckhouzam avatar Oct 04 '24 03:10 marckhouzam