当 zsh 超过某个可配置的阈值时,有没有办法在 zsh 中打印出执行时间(挂墙时间)?

kts*_*ter 6 zsh profiling

@mpy 的评论是正确的。所以我要重新表述我的问题。我真的很喜欢 zsh 中的 REPORTTIME 功能,但是根据zsh doc,它仅在用户 + 系统时间大于 $REPORTTIME 时报告时间。有没有办法让 zsh 在挂墙时间大于某个数字时报告时间,但在挂墙时间低于该数字时不报告时间?

原始问题:我真的很喜欢 zsh 中的 REPORTTIME 功能,但是根据zsh doc,它只会在命令结果非零时输出。但是在某些情况下,某些命令需要一段时间并失败,我想知道花了多长时间。有谁知道即使结果失败也能打印出命令时间的方法?

小智 9

的功能REPORTTIME似乎是硬编码来比较的usertime+systime。该功能的相关 zsh 源代码REPORTTIME

#ifdef HAVE_GETRUSAGE
    reporttime -= j->procs->ti.ru_utime.tv_sec + j->procs->ti.ru_stime.tv_sec;
    if (j->procs->ti.ru_utime.tv_usec +
    j->procs->ti.ru_stime.tv_usec >= 1000000)
    reporttime--;
    return reporttime <= 0;
#else
    {
    clktck = get_clktck();
    return ((j->procs->ti.ut + j->procs->ti.st) / clktck >= reporttime);
    }
#endif
Run Code Online (Sandbox Code Playgroud)

作为替代解决方案,您可以修改 zshrc 以获得与REPORTTIME使用总运行时间相似的功能。

REPORTTIME_TOTAL=5

# Displays the execution time of the last command if set threshold was exceeded
cmd_execution_time() {
  local stop=$((`date "+%s + %N / 1_000_000_000.0"`))
  let local "elapsed = ${stop} - ${cmd_start_time}"
  (( $elapsed > $REPORTTIME_TOTAL )) && print -P "%F{yellow}${elapsed}s%f"
}

# Get the start time of the command
preexec() {
  cmd_start_time=$((`date "+%s + %N / 1.0e9"`))
}

# Output total execution
precmd() {
  if (($+cmd_start_time)); then
    cmd_execution_time
  fi
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这个命令只给出了总运行时间。它不会将执行时间分解为用户时间和系统时间。