如何測試Linux命令運行時間?

良許在工做中,寫過一個 Shell 腳本,這個腳本能夠從 4 個 NTP 服務器輪流獲取時間,而後將最可靠的時間設置爲系統時間。linux

由於咱們對於時間的要求比較高,須要在短期內就獲取到正確的時間。因此咱們就須要對這個腳本運行時間進行測試,看看從開始運行到正確設置時間須要花費多少時間。面試

其實在工做中,還有不少狀況下須要測試一個腳本或者程序運行多少時間,特別是對於時間性要求比較高的系統更是如此。服務器

對於時間的測試,咱們能夠用到一個命令:time 。下面咱們就詳細看看如何使用 time 命令來對腳本/命令進行測時。app

1. time 命令基本用法

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

$ 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 終止這段時間所耗費的時間;usersys 分別表示 ping 命令在用戶空間及內核空間所運行的時間。ide

2020 精選 阿里/騰訊等一線大廠 面試、簡歷、進階、電子書 公衆號「良許Linux」後臺回覆「資料」免費獲取測試

2. 將時間信息寫入文件

若是咱們想把時間信息直接寫入到文件,而不是顯示在屏幕上,那麼咱們可使用 -o 選項,並指定寫入的文件路徑。ui

$ /usr/bin/time -o /home/alvin/time-output.txt ping baidu.com複製代碼

執行這個命令後,ping 命令的輸出結果依然會在終端裏,而 time 命令的結果就寫入到咱們所指定的 time-output.txt 文件裏。this

-o 選項表示輸出文件不存在就建立,若是存在的話就直接覆蓋重寫。若是咱們不想覆蓋重寫,而是想追加在文件後面,咱們可使用 -a 選項。spa

$ /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複製代碼

這個結果信息就至關詳細了,咱們能夠獲取到足夠多咱們所須要的信息。

2020 精選 阿里/騰訊等一線大廠 面試、簡歷、進階、電子書 公衆號「良許Linux」後臺回覆「資料」免費獲取

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複製代碼

看完的都是真愛,點個贊再走唄?您的「三連」就是良許持續創做的最大動力!

  1. 關注原創公衆號「良許Linux」,第一時間獲取最新Linux乾貨!
  2. 公衆號後臺回覆【資料】【面試】【簡歷】獲取精選一線大廠面試、自我提高、簡歷等資料。
  3. 關注個人博客:lxlinux.net
相關文章
相關標籤/搜索