Скрипт ZSH и быстрое профилирование? - PullRequest
4 голосов
/ 17 января 2012

Этот ответ " Как профилировать скрипт оболочки bash? ", кажется, почти идеально охватывает то, что я пытаюсь выполнить здесь. В настоящее время у меня есть некоторые сценарии 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. Мой код рендеринга подсказок использует настройки oh-my-zsh. Я мог бы добавить соответствующий код к подсказке, я полагаю, или я открыт для других предложений.

Ответы [ 2 ]

2 голосов
/ 09 января 2016

Вызов date для каждой команды вызовет fork и exec, что добавляет дополнительные издержки, которые могут помешать вашим измерениям.

Вместо этого вы можете использовать

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

для записи временных меток с меньшими издержками (с точностью до миллисекунды).

Некоторые примечания по обработке полученных журналов см. http://blog.xebia.com/profiling-zsh-shell-scripts/

2 голосов
/ 18 января 2012

Возможно, вам придется сделать

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.
...