自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何測(cè)試Linux命令運(yùn)行時(shí)間?

系統(tǒng) Linux
對(duì)于時(shí)間的測(cè)試,我們可以用到一個(gè)命令:time 。下面我們就詳細(xì)看看如何使用 time 命令來(lái)對(duì)腳本/命令進(jìn)行測(cè)時(shí)。

在工作中,我曾寫(xiě)過(guò)一個(gè) Shell 腳本,這個(gè)腳本可以從 4 個(gè) NTP 服務(wù)器輪流獲取時(shí)間,然后將最可靠的時(shí)間設(shè)置為系統(tǒng)時(shí)間。

因?yàn)槲覀儗?duì)于時(shí)間的要求比較高,需要在短時(shí)間內(nèi)就獲取到正確的時(shí)間。所以我們就需要對(duì)這個(gè)腳本運(yùn)行時(shí)間進(jìn)行測(cè)試,看看從開(kāi)始運(yùn)行到正確設(shè)置時(shí)間需要花費(fèi)多少時(shí)間。

[[321129]]

其實(shí)在工作中,還有很多情況下需要測(cè)試一個(gè)腳本或者程序運(yùn)行多少時(shí)間,特別是對(duì)于時(shí)間性要求比較高的系統(tǒng)更是如此。

對(duì)于時(shí)間的測(cè)試,我們可以用到一個(gè)命令:time 。下面我們就詳細(xì)看看如何使用 time 命令來(lái)對(duì)腳本/命令進(jìn)行測(cè)時(shí)。

1. time 命令基本用法

time 命令最基本的用法,就是 time + 命令 ,比如:

  1. $ time ping baidu.com 
  2. PING baidu.com (123.125.114.144) 56(84) bytes of data. 
  3. 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=1 ttl=56 time=2.83 ms 
  4. 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=2 ttl=56 time=2.77 ms 
  5. ………… 
  6. ^C 
  7. --- baidu.com ping statistics --- 
  8. 8 packets transmitted, 8 received, 0% packet loss, time 10818ms 
  9. rtt min/avg/max/mdev = 2.765/2.808/2.862/0.039 ms 
  10.  
  11. real    0m11.173s 
  12. user    0m0.004s 
  13. sys     0m0.002s 

在結(jié)果里,real 表示從我們執(zhí)行 ping 命令到最終按 ctrl+c 終止這段時(shí)間所耗費(fèi)的時(shí)間;user 及 sys 分別表示 ping 命令在用戶(hù)空間及內(nèi)核空間所運(yùn)行的時(shí)間。

2. 將時(shí)間信息寫(xiě)入文件

如果我們想把時(shí)間信息直接寫(xiě)入到文件,而不是顯示在屏幕上,那么我們可以使用 -o 選項(xiàng),并指定寫(xiě)入的文件路徑。

  1. $ /usr/bin/time -o /home/alvin/time-output.txt ping baidu.com 

執(zhí)行這個(gè)命令后,ping 命令的輸出結(jié)果依然會(huì)在終端里,而 time 命令的結(jié)果就寫(xiě)入到我們所指定的 time-output.txt 文件里。

-o 選項(xiàng)表示輸出文件不存在就創(chuàng)建,如果存在的話(huà)就直接覆蓋重寫(xiě)。如果我們不想覆蓋重寫(xiě),而是想追加在文件后面,我們可以使用 -a 選項(xiàng)。

  1. $ /usr/bin/time -a /home/smart/time-output.txt ping linoxide.com 

3. 顯示更詳細(xì)的時(shí)間信息

time 命令不帶選項(xiàng)的話(huà),顯示的信息量比較少,如果我們想獲得更詳細(xì)的信息,那么我們可以使用 -v 選項(xiàng)。

  1. $ /usr/bin/time -v ping baidu.com 
  2. PING baidu.com (123.125.114.144) 56(84) bytes of data. 
  3. 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=1 ttl=56 time=2.75 ms 
  4. 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=2 ttl=56 time=2.76 ms 
  5. 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=3 ttl=56 time=2.85 ms 
  6. 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=4 ttl=56 time=2.77 ms 
  7. ^C 
  8. --- baidu.com ping statistics --- 
  9. 4 packets transmitted, 4 received, 0% packet loss, time 3300ms 
  10. rtt min/avg/max/mdev = 2.751/2.785/2.851/0.075 ms 
  11.         Command being timed: "ping baidu.com" 
  12.         User time (seconds): 0.00 
  13.         System time (seconds): 0.00 
  14.         Percent of CPU this job got: 0% 
  15.         Elapsed (wall clock) time (h:mm:ss or m:ss): 0:03.64 
  16.         Average shared text size (kbytes): 0 
  17.         Average unshared data size (kbytes): 0 
  18.         Average stack size (kbytes): 0 
  19.         Average total size (kbytes): 0 
  20.         Maximum resident set size (kbytes): 2140 
  21.         Average resident set size (kbytes): 0 
  22.         Major (requiring I/O) page faults: 0 
  23.         Minor (reclaiming a frame) page faults: 626 
  24.         Voluntary context switches: 10 
  25.         Involuntary context switches: 0 
  26.         Swaps: 0 
  27.         File system inputs: 0 
  28.         File system outputs: 0 
  29.         Socket messages sent: 0 
  30.         Socket messages received: 0 
  31.         Signals delivered: 0 
  32.         Page size (bytes): 4096 
  33.         Exit status: 0 

這個(gè)結(jié)果信息就相當(dāng)詳細(xì)了,我們可以獲取到足夠多我們所需要的信息。

4. 自定義輸出格式

默認(rèn)情況下,time 命令只輸出 real,usr,sys 三個(gè)內(nèi)容,如果我們想要個(gè)性化一些,算定義它的輸出格式,time 命令也是支持的。time 命令支持的格式有很多,如下所示:

  1. C - Name and command line arguments used 
  2. D - Average size of the process's unshared data area in kilobytes 
  3. E - Elapsed time in a clock format 
  4. F - Number of page faults 
  5. I - Number of file system inputs by the process 
  6. K - Average total memory use of the process in kilobytes 
  7. M - Maximum resident set the size of the process during the lifetime in Kilobytes 
  8. O - Number of file system outputs by the process 
  9. P - Percentage of CPU that the job received 
  10. R - Number of minor or recoverable page faults 
  11. S - Total number of CPU seconds used by the system in kernel mode 
  12. U - Total number of CPU seconds used by user mode 
  13. W - Number of times the process was swapped out of main memory 
  14. X - Average amount of shared text in the process 
  15. Z - System's page size in kilobytes 
  16. c - Number of times the process was context-switched 
  17. e - Elapsed real time used by the process in seconds 
  18. k - Number of signals delivered to the process 
  19. p - Average unshared stack size of the process in kilobytes 
  20. r - Number of socket messages received by the process 
  21. s - Number of socket messages sent by the process 
  22. t - Average resident set size of the process in kilobytes 
  23. w - Number of time the process was context-switched voluntarily 
  24. x - Exit status of the command 

如果我們想要輸出以下這樣的格式:

  1. Elapsed Time = 0:01:00, Inputs 2, Outputs 1 

我們可以這樣自定義:

  1. $ /usr/bin/time -f "Elapsed Time = %E, Inputs %I, Outputs %O" ping baidu.com 
  2. PING baidu.com (220.181.38.148) 56(84) bytes of data. 
  3. 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=54 time=1.82 ms 
  4. 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=54 time=1.86 ms 
  5. ^C 
  6. --- baidu.com ping statistics --- 
  7. 4 packets transmitted, 4 received, 0% packet loss, time 3003ms 
  8. rtt min/avg/max/mdev = 1.825/1.859/1.879/0.056 ms 
  9. Elapsed Time = 0:03.92, Inputs 0, Outputs 0 

如果你想讓輸出的結(jié)果有換行,可以在對(duì)應(yīng)的地方添加 \n ,比如:

  1. $ /usr/bin/time -f "Elapsed Time = %E \n Inputs %I \n Outputs %O" ping baidu.com 

這樣輸出的結(jié)果就類(lèi)似于這樣:

  1. Elapsed Time = 0:03.92 
  2. Inputs 0 
  3. Outputs 0 

 

 

責(zé)任編輯:趙寧寧 來(lái)源: 今日頭條
相關(guān)推薦

2021-08-18 08:32:09

代碼運(yùn)行時(shí)間示波器

2021-06-24 10:28:19

uptimed命令Linux

2022-11-04 09:09:54

Linux服務(wù)器

2018-04-08 14:27:45

Linuxuptime系統(tǒng)運(yùn)行時(shí)間

2019-10-14 09:14:37

Linuxbash命令

2022-01-19 08:50:53

設(shè)備樹(shù)Linux文件系統(tǒng)

2015-07-20 15:44:46

Swift框架MJExtension反射

2016-08-23 10:17:42

2017-01-19 20:28:44

2010-01-27 14:14:48

C++程序運(yùn)行時(shí)間

2024-03-28 08:17:46

JestJS服務(wù)端

2024-03-21 09:15:58

JS運(yùn)行的JavaScrip

2019-07-12 09:30:12

DashboardDockerDNS

2021-09-11 15:38:23

容器運(yùn)行鏡像開(kāi)放

2022-10-08 00:00:00

V8channel對(duì)象

2014-03-14 13:29:24

VDI配置文件

2021-05-10 07:30:07

Linux統(tǒng)計(jì)程序

2021-08-11 11:02:17

Linuxtime命令

2021-05-11 11:52:23

Linuxtime命令

2009-06-17 15:46:36

Java運(yùn)行時(shí)本機(jī)內(nèi)存
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)