我可以剖析我的.zshrc / .zshenv吗?
看起来我的shell开始太长了。 有没有什么办法来分析它,所以我可以弄清楚是什么放慢了这么多?
尝试在文件的开头添加这个:
# 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 (和OS支持)需要纳秒输出。 
编辑:
补充评论
编辑2:
 如果你有zsh 4.3.12或更高版本,你应该可以像这样设置PS4而不是使用date命令: 
 zmodload zsh/datetime setopt promptsubst PS4='+$EPOCHREALTIME %N:%i> ' 
这应该在Linux和OS X上工作,以提供纳秒精度。
 您可以在~/.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模块 。 这很简单,只需将zmodload zsh/zprof和zprof中的~/.zshrc zmodload zsh/zprof的内容临时包装zprof 。 这将转储一些很容易解释的详细分析表。 
 更多关于zshmodules(1)信息。 
 当我发现特别慢的东西(rbenv init,vcs_info检查更改,抗原,nvm,zsh-mime-setup,解释器版本检查等)时,我将SLOW注释添加为提醒 ,并尝试查找解决方法。 缓慢的初创公司可能会导致很多的悲伤,所以我倾向于避免zsh包/框架的内部工作,我不赞成。  compinit是我愿意忍受的最慢的事情,是总启动时间的一半。