【问题标题】:Executing command in unix scripts and calculating timing [duplicate]在unix脚本中执行命令并计算时间[重复]
【发布时间】:2019-03-14 18:16:15
【问题描述】:

我对 bash 脚本很陌生。如何执行以下命令来捕获执行命令的时间。

我的交易记录在一个数组变量中。然后,我循环处理交易以执行 TradeService 远程调用。我想捕捉这次通话的时间并将其汇总到所有交易中。

  1. 在我的脚本中执行此调用并捕获时间的最佳方式是什么
  2. 如何在我的脚本中管理时间,我想以小时/分钟/秒显示它的时间聚合。
#!/bin/bash

DEALS="1739719, 1714630, 1733697, 1723940, 1666257, 1665239"

IFS=', ' read -ra array <<< "$DEALS"

for i in "${array[@]}"
do
        echo "Executing for DEALS $i"

RUN_TIME = time echo -n '{"source_system": "PROGA", "deal_id": $array[$i], "as_of": "2019-14-01T23:59:00Z"}' | java -jar ~/support/lib/polyglot.jar --command=call --endpoint=trades-server:10443 --full_method='TradeService/GetDeals' > /dev/null

echo "Runtime is $RUN_TIME"

done

【问题讨论】:

  • 您所问的上一个问题的重复内容是为什么您不能在作业中的 = 周围有空格。有关捕获time 的输出,另请参阅BashFAQ #32

标签: bash unix


【解决方案1】:

原答案

#!/bin/bash

DEALS="1739719, 1714630, 1733697, 1723940, 1666257, 1665239"

IFS=', ' read -ra array <<< "$DEALS"
total=0

for i in "${array[@]}" ; do
    echo "Executing for DEALS $i"

    startdeals=$(date +%s)
    java -jar ~/support/lib/polyglot.jar --command=call --endpoint=trades-server:10443 --full_method='TradeService/GetDeals' > /dev/null
    enddeals=$(date +%s)
    let "diff=$enddeals - $startdeals"
    let "total=$total + $diff"
    echo "Runtime for $i : $(date -d@$diff -u +%H:%M:%S)"
done

echo "Overall execution time: $(date -d@$total -u +%H:%M:%S)"

编辑: 正如@Charles Duffy 所指出的,现在我们使用$SECONDS 而不是date,假设执行时间为秒级。

#!/bin/bash

DEALS="1739719, 1714630, 1733697, 1723940, 1666257, 1665239"

IFS=', ' read -ra array <<< "$DEALS"

for i in "${array[@]}" ; do
    echo "Executing for DEALS $i"

    startdeals="$SECONDS"
    java -jar ~/support/lib/polyglot.jar --command=call --endpoint=trades-server:10443 --full_method='TradeService/GetDeals' > /dev/null
    enddeals="$SECONDS"
    diff=$(( enddeals - startdeals ))
    echo "Runtime for $i : $diff seconds."
done

echo "Overall execution time: $SECONDS seconds."

【讨论】:

  • 如果你只需要秒级的分辨率,为什么不直接使用$SECONDS 来获取shell 开始执行以来的秒数呢?这也比运行date 对性能的影响要小得多。
  • @CharlesDuffy 你是对的,相应编辑
  • 顺便说一句,我通常建议diff=$(( enddeals - startdeals )) 而不是非标准化的let(bash 向后兼容 ksh88,它早于 1992 年 POSIX sh 标准)。另见wiki.bash-hackers.org/scripting/obsolete
【解决方案2】:

您可以在操作前后使用date +%s 来查找以秒为单位的时间差。例如:

array=( 5 10 15 20 25 )
for val in "${array[@]}"
do
 starttime=$(date +%s)
 sleep "$val" # sleeping for '$val' seconds in each iteration
 #you should replace the above with the actual process.
 endtime=$(date +%s)
 echo "Time taken for the process is $((endtime-starttime))s"
done

这会给

Time taken for the process is 5s
Time taken for the process is 10s
Time taken for the process is 15s
Time taken for the process is 20s
Time taken for the process is 25s

但是,我应该承认它可以提供高达秒的精度。

+%s 格式字符串需要特别提及。 date 联机帮助页说:

自 1970 年 1 月 1 日 00:00:00 UTC 以来的 %s 秒

本质上,我们只是计算过去相对于参考点的经过时间(以秒为单位),然后计算结束时间和开始时间之间的差异。一旦您有以秒为单位的时间,请使用定义明确的数学来分隔小时和分钟。

正如@ChalresDuffy 在this 评论中指出的那样,您可以使用$SECONDS 内部变量来计算时间差。另外,如果需要微秒的粒度,可以使用$EPOCHREALTIME

【讨论】:

  • shell是bash,所以我们有$SECONDS,不是吗?
  • @CharlesDuffy 没错。而且效率也更高。 EPOCHREALTIME 也可能令人感兴趣。
猜你喜欢
  • 2012-03-29
  • 1970-01-01
  • 2018-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
  • 2012-06-26
相关资源
最近更新 更多