linux time command

At work, I wrote a Shell script , the script can get the time from turn four NTP server, then the most reliable time to the system time. Because we have high requirements for time, we need to get the right time in a short time. So we need to test the running time of this script to see how much time it takes from starting to run to setting the time correctly.

Linux command runtime test Linux command runtime test

In fact, in work, there are many situations where you need to test how long a script or program runs, especially for systems with high time requirements. For the time test, we can use a command : time. Let's take a closer look at how to use the time command to measure the time of a script/command.

1. Basic usage of time command

The most basic usage of the time command is the time + command, such as:

$ 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 

In the result, real represents the time taken from the execution of the ping command to the final termination by pressing ctrl+c; user and sys represent the running time of the ping command in user space and kernel space, respectively.

2. Write time information to file

If we want to write the time information directly to the file instead of displaying it on the screen, then we can use the -o option and specify the file path for writing.

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

After executing this command, the output result of the ping command will still be in the terminal, and the result of the time command will be written into the time-output.txt file we specified.

The -o option means that the output file will be created if it does not exist, and if it exists, it will be overwritten. If we don't want to overwrite, but want to append to the back of the file, we can use the -a option.

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

3. Display more detailed time information

If the time command has no options, the amount of information displayed is relatively small. If we want to get more detailed information, then we can use the -v option.

$ /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 

The result information is quite detailed, and we can get enough information we need.

4. Customize the output format

By default, the time command only outputs the three contents of real, usr, and sys. If we want to be more personalized, even if we define its output format, the time command is also supported. There are many formats supported by the time command, as shown below:

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 

If we want to output the following format:

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

We can customize it like this:

$ /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

If you want the output to have a line break, you can add \n in the corresponding place, such as:

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

The output will be similar to this:

Elapsed Time = 0:03.92 
Inputs 0 
Outputs 0 

The original text comes from: https://os.51cto.com/art/202004/613848.htm

This article address: https://www.linuxprobe.com/linux-command-runtime-test.html Editor: Feng Ruitao, Auditor: Pang Zengbao

Linux command list: https://www.linuxcool.com/

The above is the Linux-related knowledge shared by Liangxu Tutorial Network for all friends.

Guess you like

Origin blog.csdn.net/manongxianfeng/article/details/113090853