ZSH脚本和提示符性能分析?

6
这篇答案 "如何对bash shell脚本进行剖析?",似乎几乎完美地涵盖了我在这里尝试实现的内容。目前我有一些修改提示符的zsh脚本,但是我认为某些对oh-my-zsh的更新引发了一些问题,我需要找出原因。时不时的迟缓无法忍受。
为此,您将如何调整此示例答案中的提示部分,以便与zsh而不是bash一起使用?
目前,我已修改/etc/zshenv,使其具有来自示例的初始建议代码:
PS4='+ $(date "+%s.%N")\011 '
exec 3>&2 2>/tmp/bashstart.$$.log
set -x

我的~/.zshrc文件在末尾添加了以下内容:

set +x
exec 2>&3 3>&-

当然,这些内容不能用于ZSH shell的自定义。我的提示渲染代码使用了oh-my-zsh的自定义功能。我可以在提示符前添加适当的代码,或者我也可以接受其他建议。


请考虑编辑您的帖子,以包括您认为相关的“示例答案中的提示部分”,并指出目前哪些部分无法正常工作。祝好运。 - shellter
基本上我只需要将它们翻译成zsh的等效命令,以便在找到定制选项后将其添加到我的提示符前面。 :D 我大约一年没有自定义它了,所以我必须挖掘出我将其埋在oh-my-zsh定制中的部分。 - ylluminate
2个回答

4

为每个命令调用date将会分叉和执行,这会增加开销,可能会干扰您的测量。

相反,您可以使用

PS4=$'+ %D{%s.%6.}\011 '

以更低的开销记录时间戳(高达毫秒精度)。

有关处理生成日志的一些注意事项,请参见http://blog.xebia.com/profiling-zsh-shell-scripts/


2
您可能需要进行以下操作:
setopt prompt_subst

如果还没有这样做的话,可以使用$''来解释制表符的八进制转义。
PS4=$'+ $(date "+%s.%N")\011 '

您可能会发现以下一些转义字符很有用:
   %?     The return status of the last command executed just before the prompt.

   %_     The  status  of  the parser, i.e. the shell constructs (like `if' and `for') that have been started on the command
          line. If given an integer number that many strings will be printed; zero or negative or no integer means print  as
          many  as  there  are.   This  is  most useful in prompts PS2 for continuation lines and PS4 for debugging with the
          XTRACE option; in the latter case it will also work non-interactively.

   %i     The line number currently being executed in the script, sourced file, or shell function given by %N.  This is most
          useful for debugging as part of $PS4.

   %I     The line number currently being executed in the file %x.  This is similar to %i, but the line number is  always  a
          line number in the file where the code was defined, even if the code is a shell function.

   %L     The current value of $SHLVL.

   %N     The  name  of  the  script, sourced file, or shell function that zsh is currently executing, whichever was started
          most recently.  If there is none, this is equivalent to the parameter $0.  An integer may follow the `%' to  spec‐
          ify  a number of trailing path components to show; zero means the full path.  A negative integer specifies leading
          components.

   %x     The name of the file containing the source code currently being executed.  This behaves as %N except that function
          and eval command names are not shown, instead the file where they were defined.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接