我可以分析我的 .zshrc/.zshenv 吗?

发布于 2024-10-06 07:28:28 字数 61 浏览 4 评论 0原文

看来我的 shell 启动时间太长了。有什么方法可以对其进行分析,以便我可以找出是什么导致速度减慢如此之多?

It seems like my shell is taking way too long to start up. Is there any way to profile it so I can figure out what's slowing it down so much?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把时间冻结 2024-10-13 07:28:28

尝试在文件开头添加以下内容:

# set the trace prompt to include seconds, nanoseconds, script name and line number
# This is GNU date syntax; by default Macs ship with the BSD date program, which isn't compatible
PS4='+$(date "+%s:%N") %N:%i> '
# save file stderr to file descriptor 3 and redirect stderr (including trace 
# output) to a file with the script's PID as an extension
exec 3>&2 2>/tmp/startlog.$
# set options to turn on tracing and expansion of commands contained in the prompt
setopt xtrace prompt_subst

并在末尾添加以下内容:

# turn off tracing
unsetopt xtrace
# restore stderr to the value saved in FD 3
exec 2>&3 3>&-

您应该会获得详细的日志,显示每行执行的 epoch_second.nanosecond 时间。请注意,GNU date(和操作系统支持)需要纳秒输出。

编辑:

添加注释

编辑 2:

如果您有 zsh 4.3.12 或更高版本,您应该能够像这样设置 PS4 而不是使用 date 命令:

zmodload zsh/datetime
setopt promptsubst
PS4='+$EPOCHREALTIME %N:%i> '

这应该可以工作在 Linux 和 OS X 上为您提供纳秒精度。

Try adding this at the beginning of the file:

# set the trace prompt to include seconds, nanoseconds, script name and line number
# This is GNU date syntax; by default Macs ship with the BSD date program, which isn't compatible
PS4='+$(date "+%s:%N") %N:%i> '
# save file stderr to file descriptor 3 and redirect stderr (including trace 
# output) to a file with the script's PID as an extension
exec 3>&2 2>/tmp/startlog.$
# set options to turn on tracing and expansion of commands contained in the prompt
setopt xtrace prompt_subst

and this at the end:

# turn off tracing
unsetopt xtrace
# restore stderr to the value saved in FD 3
exec 2>&3 3>&-

And you should get a detailed log showing the epoch_second.nanosecond time of the execution of each line. Note that GNU date (and OS support) is required to have nanosecond output.

Edit:

added comments

Edit 2:

If you have zsh 4.3.12 or later, you should be able to set PS4 like this instead of using the date command:

zmodload zsh/datetime
setopt promptsubst
PS4='+$EPOCHREALTIME %N:%i> '

which should work on both Linux and OS X to give you nanosecond precision.

毁虫ゝ 2024-10-13 07:28:28

Zsh 有一个分析模块。在 ~/.zshrc~/.zshenv 的开头添加它,如下所示:

# Uncomment to use the profiling module
# zmodload zsh/zprof

取消注释,启动一个新的交互式 shell,然后运行命令 zprof。您将获得一份详细的项目列表,您可以查看这些项目,以更好地了解性能瓶颈所在:

num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    1           7.29     7.29   19.60%      7.29     7.29   19.60%  _p9k_init_ssh
 2)    2           6.72     3.36   18.06%      6.63     3.31   17.81%  gitstatus_start_impl
 3)    1           4.00     4.00   10.75%      4.00     4.00   10.75%  _p9k_must_init
 4)    3          19.28     6.43   51.80%      3.82     1.27   10.28%  (anon)
 5)    1           1.63     1.63    4.37%      1.63     1.63    4.37%  _p9k_restore_state_impl
 6)    1          16.92    16.92   45.46%      1.61     1.61    4.32%  _p9k_precmd_impl
 7)    1           7.13     7.13   19.16%      0.87     0.87    2.33%  _p9k_set_prompt
 8)   22           6.26     0.28   16.83%      0.84     0.04    2.27%  _p9k_build_segment
 9)   17           0.82     0.05    2.19%      0.82     0.05    2.19%  _p9k_param
10)    6           2.24     0.37    6.02%      0.74     0.12    1.98%  _p9k_left_prompt_segment
11)    5           0.59     0.12    1.60%      0.59     0.12    1.60%  add-zsh-hook
12)    1           3.05     3.05    8.20%      0.58     0.58    1.56%  prompt_dir

以下是来自 加速 zsh 如果开始时间感觉不稳定,你会很感激:

for i in $(seq 1 10); do /usr/bin/time zsh -i -c exit; done

运行该作业,你应该看到类似的输出:

        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys

利润。

Zsh has a profiling module. At the beginning of ~/.zshrc or ~/.zshenv add it like:

# Uncomment to use the profiling module
# zmodload zsh/zprof

Uncomment it, start a new interactive shell then run the command zprof. You will get a detailed list of items you can review to better understand where your performance bottlenecks are:

num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    1           7.29     7.29   19.60%      7.29     7.29   19.60%  _p9k_init_ssh
 2)    2           6.72     3.36   18.06%      6.63     3.31   17.81%  gitstatus_start_impl
 3)    1           4.00     4.00   10.75%      4.00     4.00   10.75%  _p9k_must_init
 4)    3          19.28     6.43   51.80%      3.82     1.27   10.28%  (anon)
 5)    1           1.63     1.63    4.37%      1.63     1.63    4.37%  _p9k_restore_state_impl
 6)    1          16.92    16.92   45.46%      1.61     1.61    4.32%  _p9k_precmd_impl
 7)    1           7.13     7.13   19.16%      0.87     0.87    2.33%  _p9k_set_prompt
 8)   22           6.26     0.28   16.83%      0.84     0.04    2.27%  _p9k_build_segment
 9)   17           0.82     0.05    2.19%      0.82     0.05    2.19%  _p9k_param
10)    6           2.24     0.37    6.02%      0.74     0.12    1.98%  _p9k_left_prompt_segment
11)    5           0.59     0.12    1.60%      0.59     0.12    1.60%  add-zsh-hook
12)    1           3.05     3.05    8.20%      0.58     0.58    1.56%  prompt_dir

And here's a one-liner from speeding up zsh you'll appreciate if start time feels sporadic:

for i in $(seq 1 10); do /usr/bin/time zsh -i -c exit; done

Run that job and you should see output like:

        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys
        0.05 real         0.02 user         0.02 sys

Profit.

我爱人 2024-10-13 07:28:28

您可以在 ~/.zshrc 中的第一个可疑点(或开头)启动计时器:

integer t0=$(date '+%s')  # move this around
... maybe something suspect ...

# End of zshrc
function {
    local -i t1 startup
    t1=$(date '+%s')
    startup=$(( t1 - t0 ))
    [[ $startup -gt 1 ]] && print "Hmm, poor shell startup time: $startup"
}
unset t0

如果我看到启动太慢,这会提醒我,并将其保留为永久包装。

对于更复杂的测量,有一个名为 zprof 的 zsh 模块。就像将 ~/.zshrc 的内容临时包装在 zmodload zsh/zprofzprof 中一样简单。这将转储一些易于解释的详细分析表。

更多信息请参见 zshmodules(1) 联机帮助页。

当我发现特别慢的东西(rbenv init、vcs_info check-for-changes、antigen、nvm、zsh-mime-setup、解释器版本检查等)时,我添加 SLOW 注释提醒,并尝试寻找解决方法。缓慢的启动可能会造成很大的痛苦,所以我倾向于避免使用我不了解其内部工作原理的 zsh 包/框架字。 compinit 是我愿意忍受的最慢的事情,大约占总启动时间的一半。

You can start your timer at the first suspicious point in your ~/.zshrc (or at the beginning):

integer t0=$(date '+%s')  # move this around
... maybe something suspect ...

# End of zshrc
function {
    local -i t1 startup
    t1=$(date '+%s')
    startup=$(( t1 - t0 ))
    [[ $startup -gt 1 ]] && print "Hmm, poor shell startup time: $startup"
}
unset t0

This alerts me if ever I see a too-slow startup, and I leave it in as a permanent wrapper.

For more sophisticated measurements, there is a zsh module called zprof. It's as simple as temporarily wrapping the contents of your ~/.zshrc in a zmodload zsh/zprof and zprof. This will dump some verbose profiling tables that are easy enough to interpret.

More info in zshmodules(1) manpage.

When I find things that are particularly slow (rbenv init, vcs_info check-for-changes, antigen, nvm, zsh-mime-setup, interpreter version checking, etc) I add SLOW comments as reminders, and try to find workarounds. Slow startups can cause a lot grief, so I tend to avoid zsh packages/framewords whose inner workings I don't grok. compinit is the slowest thing I'm willing to live with and is ~half of total startup time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文