6

This answer, "How to profile a bash shell script?", seems to nearly perfectly cover what I'm trying to accomplish here. I currently have some zsh scripts that modify the prompt, however I think some updates to oh-my-zsh have evoked some issues that I need to hunt down. The sluggishness from time to time is unbearable.

To this end, how would you adapt the prompt sections in this example answer to work with zsh vs bash?

Presently I have modified /etc/zshenv such that it has the initial suggested code from the example:

PS4='+ $(date "+%s.%N")\011 '
exec 3>&2 2>/tmp/bashstart.$$.log
set -x

And my ~/.zshrc has the following appended to it's tail:

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

Of course these are not valid for ZSH shell customization. My prompt rendering code utilizes oh-my-zsh customizations. I could prepend the appropriate code to the prompt I suppose or I'm open to other suggestions.

Community
  • 1
  • 1
ylluminate
  • 12,102
  • 17
  • 78
  • 152
  • please consider editing your post to include 'the prompt sections in this example answer' that you find relevant, and indicate where it is not working for you now. Good luck. – shellter Jan 17 '12 at 18:59
  • Essentially I just need these translated to zsh equivalencies in order to prepend my prompt once I hunt the customization down. :D I haven't customized it for about a year so I have to dig section back up where I buried it within the oh-my-zsh customizations. – ylluminate Jan 17 '12 at 19:30

2 Answers2

4

Calling date for each command will fork and exec, which adds overhead which may interfere with your measurements.

Instead, you could use

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

to log timestamps with lower overhead (up to millisecond precision).

For some notes on processing the resulting logs, see http://blog.xebia.com/profiling-zsh-shell-scripts/

Arnout Engelen
  • 6,709
  • 1
  • 25
  • 36
2

You may need to do

setopt prompt_subst

if it's not already.

Also, in order to interpret the octal escape for tab, use $'':

PS4=$'+ $(date "+%s.%N")\011 '

You may also find some of these escapes to be useful:

   %?     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.
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439