这个答案,“How to profile a bash shell script?”,似乎几乎完美地涵盖了我在这里想要完成的任务。 我目前有一些 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 自定义。 我可以将适当的代码添加到我认为的提示中,或者我愿意接受其他建议。
为每个命令调用
date
将分叉并执行,这会增加开销,可能会干扰您的测量。
相反,你可以使用
PS4=$'+ %D{%s.%6.}\011 '
以较低的开销记录时间戳(高达毫秒精度)。
有关处理结果日志的一些说明,请参阅 http://blog.xebia.com/profiling-zsh-shell-scripts/
你可能需要做
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.