Can I profile my .zshrc/.zshenv?

后端 未结 3 1247
天命终不由人
天命终不由人 2020-12-23 02:05

It seems like my shell is taking way too long to start up. Is there any way to profile it so I can figure out what\'s slowing it down so much?

3条回答
  •  天命终不由人
    2020-12-23 02:47

    Try adding this at the beginning of the file:

    # 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
    

    and this at the end:

    # turn off tracing
    unsetopt xtrace
    # restore stderr to the value saved in FD 3
    exec 2>&3 3>&-
    

    And you should get a detailed log showing the epoch_second.nanosecond time of the execution of each line. Note that GNU date (and OS support) is required to have nanosecond output.

    Edit:

    added comments

    Edit 2:

    If you have zsh 4.3.12 or later, you should be able to set PS4 like this instead of using the date command:

    zmodload zsh/datetime
    setopt promptsubst
    PS4='+$EPOCHREALTIME %N:%i> '
    

    which should work on both Linux and OS X to give you nanosecond precision.

提交回复
热议问题