在工作中,我曾经写过一个shell脚本,可以轮流从4个NTP服务器获取时间,然后将最可靠的时间设置为系统时间。因为我们对时间的要求比较高,需要在短时间内获取到正确的时间。所以我们需要测试这个脚本的运行时间,看看从开始运行到正确设置时间需要多长时间。其实在工作中,还是有很多情况需要测试一个脚本或者程序运行多长时间,尤其是对时间要求比较高的系统。对于时间测试,我们可以使用一个命令:time。让我们仔细看看如何使用time命令来测量脚本/命令的时间。1、时间命令的基本用法时间命令最基本的用法是时间+命令,例如:$timepingbaidu.comPINGbaidu.com(123.125.114.144)56(84)bytesofdata.64bytesfrom123.125.114.144(123.125.114.144):icmp_seq=1ttl=56time=2.83ms64bytesfrom123.125.114.144(123.125.114.144):icmp_seq=2ttl=56time=2.77ms………^C---baidu.compingstatistics---8packetstransmitted,8received,0%packetloss,time108min/8msavg/max/mdev=2.765/2.808/2.862/0.039msreal0m11.173suser0m0.004ssys0m0.002s结果中real表示从执行ping命令到按ctrl+c终止所用的时间;user和sys分别表示ping命令在用户空间和内核空间运行的时间。2.将时间信息写入文件如果我们想将时间信息直接写入文件而不是显示在屏幕上,那么我们可以使用-o选项并指定写入的文件路径。$/usr/bin/time-o/home/alvin/time-output.txtpingbaidu.com执行这条命令后,ping命令的输出结果仍然会在终端,而time命令的结果会被写入到指定的time-output.txt文件。-o选项表示输出文件不存在则创建,存在则直接覆盖。如果我们不想覆盖rewrite,而是想追加到文件末尾,我们可以使用-a选项。$/usr/bin/time-a/home/smart/time-output.txtpinglinoxide.com3.显示更详细的时间信息。如果time命令没有选项,显示的信息量比较少。如果我们想获得更详细的信息,那么我们可以使用-v选项。$/usr/bin/time-vpingbaidu.comPINGbaidu.com(123.125.114.144)56(84)bytesofdata.64bytesfrom123.125.114.144(123.125.114.144):icmp_seq=1ttl=56time=2.75ms64bytesfrom123.124.124(153.114.114114.144):ICMP_SEQ=2TTL=56TIME=2.76MS64BYTESFROM123.125.114.144(123.125.114.144):ICMP_SEQ=3TTTL=3TTTL=56Time=2.85mms64bytesfrom164bytesfrom123.123.125.114.1144.144=4.1446.1446.14464.1446.14.1446.14.144(icm.114)--baidu.compingstatistics---4packetstransmitted,4received,0%packetloss,time3300msrttmin/avg/max/mdev=2.751/2.785/2.851/0.075msCommandbeingtimed:"pingbaidu.com"Usertime(seconds):0.00Systemtime(seconds):0.00PercentofCPUthisjobgot:0%Elapsed(wallclock)time(h:mm:ssorm:ss):0:03.64Averagesharedtextsize(kbytes):0Averageunshareddatasize(kbytes):0Averagestacksize(kbytes):0Averagetotalsize(kbytes):0Maximumresidentsetsize(kbytes):2140Averageresidentsetsize(kbytes)):0Major(requiringI/O)pagefaults:0Minor(reclaimingaframe)pagefaults:626Voluntarycontextswitches:10Involuntarycontextswitches:0Swaps:0Filesysteminputs:0Filesystemoutputs:0Socketmessagesent:0Socketmessagesreceived:0Signalsdelivered:0Pagesize(bytes):4096Exitstatus:0这个结果信息比较详细,我们可以得到我们需要的足够信息4.自定义输出格式默认情况下,time命令只输出real,usr,和系统。如果我们想对其进行个性化设置,我们可以定义它的输出格式,同时也支持时间命令。time命令支持的格式有很多,如下所示:C-NameandcommandlineargumentsusedD-Averagesizeoftheprocess'sunshareddataareainkilobytesE-ElapsedtimeinaclockformatF-NumberofpagefaultsI-NumberoffilesysteminputsbytheprocessK-AveragetotalmemoryuseoftheprocessinkilobytesM-MaximumresidentsetthesizeoftheprocessduringthelifetimeinKilobytesO-NumberoffilesystemoutputsbytheprocessP-PercentageofCPUthatthejobreceivedR-NumberofminororrecoverablepagefaultsS-TotalnumberofCPUsecondsusedbythesysteminkernelmodeU-TotalnumberofCPUsecondsusedbyusermodeW-NumberoftimestheprocesswasswappedoutofmainmemoryX-AverageamountofsharedtextintheprocessZ-System'spagesizeinkilobytesc-Numberoftimestheprocesswascontext-switchede-Elapsedrealtimeusedbytheprocessinsecondssk-Numberofsignalsdeliveredtotheprocessp-Averageunsharedstacksizeoftheprocessinkilobytesr-Numberofsocketmessagesreceivedbytheprocesss-Numberofsocketmessagessentbytheprocesst-Averageresidentsetsizeoftheprocessinkilobytesw-进程wasc的次数ontext-switchedvoluntarilyx-Exitstatusofthecommand如果我们要输出如下格式:ElapsedTime=0:01:00,Inputs2,Outputs1,我们可以这样自定义:$/usr/bin/time-f"ElapsedTime=%E,Inputs%I,Outputs%O"pingbaidu.comPINGbaidu.com(220.181.38.148)56(84)bytesofdata.64bytesfrom220.181.38.148(220.181.38.148):icmp_seq=1ttl=54time=1.82ms64bytesfrom220.181.3218.143(4)2ttl=54time=1.86ms^C---baidu.compingstatistics---4packetstransmitted,4received,0%packetloss,time3003msrttmin/avg/max/mdev=1.825/1.859/1.879/0.056msElapsedTime=0:03.92,Inputs0,Outputs0如果想要输出有换行符,可以在相应的地方加上\n,例如:$/usr/bin/time-f"ElapsedTime=%E\nInputs%I\nOutputs%O"pingbaidu.comoutputlike这个结果类似于:ElapsedTime=0:03.92Inputs0Outputs0
