Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading kubectl plugin is slow #6843

Closed
lbolla opened this issue May 20, 2018 · 15 comments
Closed

Loading kubectl plugin is slow #6843

lbolla opened this issue May 20, 2018 · 15 comments
Labels
Area: plugin Issue or PR related to a plugin Performance Issue or PR about performance

Comments

@lbolla
Copy link
Contributor

lbolla commented May 20, 2018

The main reason is that it regenerates the completion script over and over, with kubectl completion zsh.

@alizdavoodi
Copy link

alizdavoodi commented May 22, 2018

I'm using kubectl plugin and every morning start terminal it's been hang up about 5 minute.
After remove the plugin from .zshrc everything working smoothly.
I used set -xv bash flag in .zshrc it hangs right here:

_complete kubectl 2>/dev/null
+/proc/self/fd/17:8793> _bash_comp kubectl

@gwthm-in
Copy link

gwthm-in commented Aug 20, 2018

@lbolla I don't think, it is completely fixed or something else is wrong with my setup?
In my machine, it still takes a lot of time to load. Here is the profiling screenshot.

screen shot 2018-08-20 at 8 02 21 am

That is on every new tab in Iterm. But on existing tab, it's not taking much.
screen shot 2018-08-20 at 8 07 04 am

@gwthm-in
Copy link

Avoid it by doing lazy load.

function kubectl() {
    if ! type __start_kubectl >/dev/null 2>&1; then
        source <(command kubectl completion zsh)
    fi

    command kubectl "$@"
}

as mentioned here

@lbolla
Copy link
Contributor Author

lbolla commented Aug 20, 2018

Do you have custom zsh cache settings? kubectl speedup works by caching a file in $ZSH_CACHE_DIR and skipping kubectl completion zsh which is the slowest bit.

@gwthm-in
Copy link

@lbolla I don't have any custom zsh cache settings. yes I do have the cached file in $ZSH_CACHE_DIR

@mati865
Copy link

mati865 commented Oct 23, 2018

Experienced the same on Fedora, the cache doesn't help because the slow part here is execution of function __kubectl_bash_source() from ${ZSH_CACHE_DIR}/kubectl_completion.

Upstream issue: kubernetes/kubernetes#59078

@akaihola
Copy link

akaihola commented Feb 6, 2019

...and if I understand correctly, the lazy loading trick doesn't help either because sourcing the cached kubectl_completion triggers loading anyway.

@mcornella mcornella reopened this Mar 28, 2019
@mcornella mcornella added Area: plugin Issue or PR related to a plugin Performance Issue or PR about performance labels May 4, 2019
@ghost
Copy link

ghost commented Feb 6, 2020

any news on improvements here?

num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    1         190.03   190.03   65.52%    189.83   189.83   65.45%  __kubectl_bash_source

@clebs
Copy link

clebs commented Sep 29, 2020

I am having the same problem in Mac OSX Catalina 10.15.7 since yesterday when I updated via omz update.
Startup time ranges from 5 to 10 seconds.

The debug startup freezes for some seconds in the following lines:

+/Users/xxxx/.oh-my-zsh/cache/kubectl_completion:12832> __kubectl_convert_bash_to_zsh
...
+/Users/xxxx/.oh-my-zsh/cache/minicube_completion:2673> __minikube_convert_bash_to_zsh

@robbyrussell
Copy link
Member

Anyone interested in submitting a PR for this?

@esemeniuc
Copy link
Contributor

Running the speed test from https://blog.mattclemente.com/2020/06/26/oh-my-zsh-slow-to-load/#how-to-test-your-shell-load-time,
I see the following (Arch linux, kernel 5.14, zsh 5.8, alacritty terminal, nvme ssd):

~ for plugin ($plugins); do
  timer=$(($(date +%s%N)/1000000))
  if [ -f $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh ]; then
    source $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh
  elif [ -f $ZSH/plugins/$plugin/$plugin.plugin.zsh ]; then
    source $ZSH/plugins/$plugin/$plugin.plugin.zsh
  fi
  now=$(($(date +%s%N)/1000000))
  elapsed=$(($now-$timer))
  echo $elapsed ms":" $plugin
done

22 ms: git
17 ms: fzf
7 ms: tmux
239 ms: kubectl
2 ms: docker

@marckhouzam
Copy link

I tried this on my Mac (using gdate instead of date) and I also got over 200 ms:

220 ms: kubectl

After investigating I noticed that the zsh plugin for kubectl uses a cache:

$ ls -l ${ZSH_CACHE_DIR}/kubectl_completion
4 Nov  2019 /Users/marckhouzam/.oh-my-zsh/cache/kubectl_completion

which was two years old and using the old kubectl completion logic.
After removing that file, I re-ran the timing script twice (the first time caused the plugin to regenerate the file so it was slower), and on the second and all future measurements I got something like

13 ms: kubectl

I took this opportunity to empty that cache for other plugins 😄

@marckhouzam
Copy link

Oh and you need to use kubectl 1.22 or higher for the faster zsh completion logic.

@esemeniuc
Copy link
Contributor

Thank you very much @marckhouzam!! It works 🥳

~ ls -l ${ZSH_CACHE_DIR}/kubectl_completion
-rw-r--r-- 1 eric users 459559 Mar  6  2021 /home/eric/.oh-my-zsh/cache/kubectl_completion
➜  ~ kubectl version
Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.3", GitCommit:"c92036820499fedefec0f847e2054d824aea6cd1", GitTreeState:"archive", BuildDate:"2021-10-28T06:55:39Z", GoVersion:"go1.17.2", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.2", GitCommit:"8b5a19147530eaac9476b0ab82980b4088bbc1b2", GitTreeState:"clean", BuildDate:"2021-09-15T21:32:41Z", GoVersion:"go1.16.8", Compiler:"gc", Platform:"linux/amd64"}
➜  ~ for plugin ($plugins); do
  timer=$(($(date +%s%N)/1000000))
  if [ -f $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh ]; then
    source $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh
  elif [ -f $ZSH/plugins/$plugin/$plugin.plugin.zsh ]; then
    source $ZSH/plugins/$plugin/$plugin.plugin.zsh
  fi
  now=$(($(date +%s%N)/1000000))
  elapsed=$(($now-$timer))
  echo $elapsed ms":" $plugin
done
37 ms: git
25 ms: fzf
3 ms: tmux
4 ms: kubectl
1 ms: docker
➜  ~

@mcornella mcornella moved this to Backlog in Main project Jan 2, 2022
@carlosala
Copy link
Member

carlosala commented Jan 2, 2024

This was fixed in 658eb01

@github-project-automation github-project-automation bot moved this from Backlog to Done in Main project Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: plugin Issue or PR related to a plugin Performance Issue or PR about performance
Projects
Archived in project
Development

No branches or pull requests