测试bash脚本的执行时间

发布时间:2019-12-31编辑:脚本学堂
显示脚本中某段代码执行时间。只需要把dateflag放置到代码段的前后,便可以显示出代码段的执行时间,非常方便对脚本性能的调试。

显示脚本中某段代码执行时间:
 

复制代码 代码如下:
export befor_date 
export now_date 
befor_date=$(date +%s -d "$(date +%F' '%T)") 
dateflag(){ 
 now_date=$(date +%s -d "$(date +%F' '%T)") 
 time_distance=$(expr ${now_date} - ${befor_date}) 
 hour_distance=$(expr ${time_distance} / 3600) 
 hour_remainder=$(expr ${time_distance} % 3600) 
 min_distance=$(expr ${hour_remainder} / 60) 
 min_remainder=$(expr ${hour_remainder} % 60) 
 echo -e "ttttTime:t${hour_distance}:${min_distance}:${min_remainder} "
 befor_date="${now_date}"
}

只需要把dateflag放置到代码段的前后,便可以显示出代码段的执行时间,非常方便对脚本性能的调试。
显示效果如下:Time: 0:0:0
============TCP&UDP========================
<<<<<192.168.2.3
TCP:3460 OK
Time: 0:0:10
TCP:3461 OK
Time: 0:0:10

<<<<<172.16.2.13
TCP:4040 OK
Time: 0:0:1
TCP:3200 OK
Time: 0:0:1
TCP:4340 OK
Time: 0:0:1
TCP:4041 NOK
Time: 0:0:1
TCP:3201 NOK
Time: 0:0:0
TCP:4341 NOK
Time: 0:0:1

<<<<<172.16.2.14
TCP:4044 NOK
Time: 0:0:1
TCP:3200 NOK
Time: 0:0:1
TCP:4344 NOK
Time: 0:0:1
TCP:4045 NOK
Time: 0:0:0
TCP:3201 NOK
Time: 0:0:1
TCP:4345 NOK
Time: 0:0:1

<<<<<172.16.2.1
TCP:3000 OK
Time: 0:0:1
TCP:3001 OK
Time: 0:0:1
TCP:3002 OK
Time: 0:0:1
TCP:3003 OK
Time: 0:0:1

<<<<<172.16.2.2
TCP:3000 OK
Time: 0:0:1
TCP:3001 OK
Time: 0:0:1
TCP:3002 OK
Time: 0:0:0
TCP:3003 OK
Time: 0:0:1

<<<<<192.168.2.1
TCP:3460 OK
Time: 0:0:11
TCP:3461 OK
Time: 0:0:12

<<<<<192.168.2.2
TCP:3460 OK
Time: 0:0:16
TCP:3461 OK
Time: 0:0:17