Linux下计算command执行时间的方法:
方法一:time cmd
#!/bin/bash
time fio --name=test --filename=/dev/nvme0n1 --ioengine=libaio --direct=1 --rw=write --bs=1024k --iodepth=128 --numjobs=1 --size=20G
直接在命令之前加上time 命令,cmd执行结束后,会连带执行结果与执行时间一起输出,不会影响原cmd执行结果,如此例的结果如下,其中real为cmd执行消耗的时间:
test: (g=0): rw=write, bs=(R) 1024KiB-1024KiB, (W) 1024KiB-1024KiB, (T) 1024KiB-1024KiB, ioengine=libaio, iodepth=128
fio-3.1
Starting 1 process
Jobs: 1 (f=1): [W(1)][100.0%][r=0KiB/s,w=3300MiB/s][r=0,w=3300 IOPS][eta 00m:00s]
test: (groupid=0, jobs=1): err= 0: pid=5038: Mon Jun 5 14:01:14 2023
write: IOPS=3326, BW=3327MiB/s (3488MB/s)(20.0GiB/6156msec)
slat (usec): min=17, max=258, avg=77.40, stdev=30.09
clat (usec): min=521, max=85079, avg=38374.01, stdev=12812.81
lat (usec): min=655, max=85199, avg=38451.58, stdev=12811.39
clat percentiles (usec):
| 1.00th=[ 586], 5.00th=[ 9634], 10.00th=[36439], 20.00th=[37487],
| 30.00th=[38011], 40.00th=[38011], 50.00th=[38011], 60.00th=[39060],
| 70.00th=[39060], 80.00th=[39060], 90.00th=[39060], 95.00th=[70779],
| 99.00th=[77071], 99.50th=[78119], 99.90th=[83362], 99.95th=[84411],
| 99.99th=[85459]
bw ( MiB/s): min= 3216, max= 3430, per=100.00%, avg=3328.50, stdev=67.11, samples=12
iops : min= 3216, max= 3430, avg=3328.50, stdev=67.11, samples=12
lat (usec) : 750=3.38%, 1000=0.04%
lat (msec) : 2=0.16%, 4=0.32%, 10=1.17%, 20=1.76%, 50=85.19%
lat (msec) : 100=7.98%
cpu : usr=19.16%, sys=10.90%, ctx=17052, majf=0, minf=11
IO depths : 1=0.1%, 2=0.1%, 4=0.1%, 8=0.1%, 16=0.1%, 32=0.2%, >=64=99.7%
submit : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0%
complete : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.1%
issued rwt: total=0,20480,0, short=0,0,0, dropped=0,0,0
latency : target=0, window=0, percentile=100.00%, depth=128
Run status group 0 (all jobs):
WRITE: bw=3327MiB/s (3488MB/s), 3327MiB/s-3327MiB/s (3488MB/s-3488MB/s), io=20.0GiB (21.5GB), run=6156-6156msec
Disk stats (read/write):
nvme0n1: ios=163/40599, merge=0/0, ticks=8/1452412, in_queue=1453156, util=97.66%
real 0m6.619s
user 0m1.326s
sys 0m0.849s
方法二:date +%s
start_time=`date +%s`
fio --name=test --filename=/dev/nvme0n1 --ioengine=libaio --direct=1 --rw=write --bs=1024k --iodepth=128 --numjobs=1 --size=20G
end_time=`date +%s`
echo "$((end_time-start_time))"
以此为例,date +%s 可以将时间转换为s,这样在cmd前后加上此命令抓取时间,那么前后差值就是cmd执行的时间了,这个用起来比较灵活,可以统计脚本一段
过程的总耗时
额外提一下,date +%s的效果即作用:
root@unassigned:~/Desktop/xian# date +%s
1685945376
这个就是输出的结果
这个命令可以用于获取当前时间的 Unix 时间戳(即距离1970年1月1日00:00:00 UTC的秒数)。
计算方式如下:
在 Unix 系统中,时间戳以 UTC(协调世界时)的形式表示。该时间是相对于时间 1970 年 1 月 1 日,00:00:00 UTC 的秒数。
因此,通过计算当前时间和1970年1月1日00:00:00 UTC的时间差,可以得到当前时间的 Unix 时间戳。