如何测试Linux命令运行时间?
良许在工作中,写过一个 shell 脚本,这个脚本可以从 4 个 ntp 服务器轮流获取时间,然后将最可靠的时间设置为系统时间。
因为我们对于时间的要求比较高,需要在短时间内就获取到正确的时间。所以我们就需要对这个脚本运行时间进行测试,看看从开始运行到正确设置时间需要花费多少时间。
其实在工作中,还有很多情况下需要测试一个脚本或者程序运行多少时间,特别是对于时间性要求比较高的系统更是如此。
对于时间的测试,我们可以用到一个命令:time
。下面我们就详细看看如何使用 time 命令来对脚本/命令进行测时。
1. time 命令基本用法
time 命令最基本的用法,就是 time + 命令
,比如:
$ time ping baidu.com ping baidu.com (123.125.114.144) 56(84) bytes of data. 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=1 ttl=56 time=2.83 ms 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=2 ttl=56 time=2.77 ms ………… ^c --- baidu.com ping statistics --- 8 packets transmitted, 8 received, 0% packet loss, time 10818ms rtt min/avg/max/mdev = 2.765/2.808/2.862/0.039 ms real 0m11.173s user 0m0.004s sys 0m0.002s
在结果里,real
表示从我们执行 ping 命令到最终按 ctrl+c 终止这段时间所耗费的时间;user
及 sys
分别表示 ping 命令在用户空间及内核空间所运行的时间。
2. 将时间信息写入文件
如果我们想把时间信息直接写入到文件,而不是显示在屏幕上,那么我们可以使用 -o
选项,并指定写入的文件路径。
$ /usr/bin/time -o /home/alvin/time-output.txt ping baidu.com
执行这个命令后,ping 命令的输出结果依然会在终端里,而 time 命令的结果就写入到我们所指定的 time-output.txt 文件里。
-o
选项表示输出文件不存在就创建,如果存在的话就直接覆盖重写。如果我们不想覆盖重写,而是想追加在文件后面,我们可以使用 -a
选项。
$ /usr/bin/time -a /home/smart/time-output.txt ping linoxide.com
3. 显示更详细的时间信息
time 命令不带选项的话,显示的信息量比较少,如果我们想获得更详细的信息,那么我们可以使用 -v
选项。
$ /usr/bin/time -v ping baidu.com ping baidu.com (123.125.114.144) 56(84) bytes of data. 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=1 ttl=56 time=2.75 ms 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=2 ttl=56 time=2.76 ms 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=3 ttl=56 time=2.85 ms 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=4 ttl=56 time=2.77 ms ^c --- baidu.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3300ms rtt min/avg/max/mdev = 2.751/2.785/2.851/0.075 ms command being timed: "ping baidu.com" user time (seconds): 0.00 system time (seconds): 0.00 percent of cpu this job got: 0% elapsed (wall clock) time (h:mm:ss or m:ss): 0:03.64 average shared text size (kbytes): 0 average unshared data size (kbytes): 0 average stack size (kbytes): 0 average total size (kbytes): 0 maximum resident set size (kbytes): 2140 average resident set size (kbytes): 0 major (requiring i/o) page faults: 0 minor (reclaiming a frame) page faults: 626 voluntary context switches: 10 involuntary context switches: 0 swaps: 0 file system inputs: 0 file system outputs: 0 socket messages sent: 0 socket messages received: 0 signals delivered: 0 page size (bytes): 4096 exit status: 0
这个结果信息就相当详细了,我们可以获取到足够多我们所需要的信息。
4. 自定义输出格式
默认情况下,time 命令只输出 real,usr,sys 三个内容,如果我们想要个性化一些,算定义它的输出格式,time 命令也是支持的。time 命令支持的格式有很多,如下所示:
c - name and command line arguments used d - average size of the process's unshared data area in kilobytes e - elapsed time in a clock format f - number of page faults i - number of file system inputs by the process k - average total memory use of the process in kilobytes m - maximum resident set the size of the process during the lifetime in kilobytes o - number of file system outputs by the process p - percentage of cpu that the job received r - number of minor or recoverable page faults s - total number of cpu seconds used by the system in kernel mode u - total number of cpu seconds used by user mode w - number of times the process was swapped out of main memory x - average amount of shared text in the process z - system's page size in kilobytes c - number of times the process was context-switched e - elapsed real time used by the process in seconds k - number of signals delivered to the process p - average unshared stack size of the process in kilobytes r - number of socket messages received by the process s - number of socket messages sent by the process t - average resident set size of the process in kilobytes w - number of time the process was context-switched voluntarily x - exit status of the command
如果我们想要输出以下这样的格式:
elapsed time = 0:01:00, inputs 2, outputs 1
我们可以这样自定义:
$ /usr/bin/time -f "elapsed time = %e, inputs %i, outputs %o" ping baidu.com ping baidu.com (220.181.38.148) 56(84) bytes of data. 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=54 time=1.82 ms 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=54 time=1.86 ms ^c --- baidu.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3003ms rtt min/avg/max/mdev = 1.825/1.859/1.879/0.056 ms elapsed time = 0:03.92, inputs 0, outputs 0
如果你想让输出的结果有换行,可以在对应的地方添加 \n
,比如:
$ /usr/bin/time -f "elapsed time = %e \n inputs %i \n outputs %o" ping baidu.com
这样输出的结果就类似于这样:
elapsed time = 0:03.92 inputs 0 outputs 0
看完的都是真爱,点个赞再走呗?您的「三连」就是良许持续创作的最大动力!
- 关注原创公众号「良许linux」,第一时间获取最新linux干货!
- 公众号后台回复【资料】【面试】【简历】获取精选一线大厂面试、自我提升、简历等资料。
- 关注我的博客: