美文网首页
bash计算命令的执行时间毫秒

bash计算命令的执行时间毫秒

作者: CodingCode | 来源:发表于2020-05-09 03:11 被阅读0次

    统计命令的执行时间。

    # output function
    # param 1: startTimestamp, format $(date +%s%N)
    # param 2: endTimestamp, format $(date +%s%N)
    # param 3: command ID
    # param 4: command result
    # param 5: command output
    function output() {
      typeset START=$(date -d @$(($1/1000000000)) +"%Y-%m-%d %T")
      typeset END=$(date -d @$(($2/1000000000)) +"%Y-%m-%d %T")
      typeset ID=${3:-ID}
      typeset STATUS=${4:-SUCCESS}
      typeset OUTPUT=${5:-}
    
      typeset ELAPSE=$((($2- $1)/1000000))
      echo "id=${ID}, status=${STATUS}, time=(${START} - ${END} - ${ELAPSE}ms), output=[${OUTPUT}]"
    }
    
    # mark command begin timestamp
    startTime=$(date +%s%N)
    
    CMDID, CMDSTATUS, CMDOUTPUT = ... execute command and get result ...
    
    # mark command stop timestamp
    endTime=$(date +%s%N)
    
    # output command execute timestamp
    
    output  $startTime $endTime $CMDID $CMDSTATUS $CMDOUTPUT
    

    例如输出:

    id=ID, status=Success, time=(2020-05-08 15:11:05 - 2020-05-08 15:11:06 - 298ms), output=[...]
    

    相关文章

      网友评论

          本文标题:bash计算命令的执行时间毫秒

          本文链接:https://www.haomeiwen.com/subject/muzcnhtx.html