How to test Linux command run time?

Liang Xu has written a Shell script in his work. This script can obtain time from four NTP servers in turn, and then set the most reliable time to system time.

Because we have a high demand 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 long it will take from the start to the correct setting.

In fact, in the work, there are many situations that need to test how long a script or program runs, especially for systems with high time requirements.

For the test of time, we can use a command: time. Let's take a detailed look at how to use the time command to measure the time of a script / command.

1. Basic usage of time command

The basic usage of time command is time + command, for example:

$ time ping
PING ( 56(84) bytes of data.
64 bytes from ( icmp_seq=1 ttl=56 time=2.83 ms
64 bytes from ( icmp_seq=2 ttl=56 time=2.77 ms
--- 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 spent from the time when we executed the ping command to the time when we finally terminated it with ctrl+c; user and sys represent the time when the ping command ran 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, we can use the - o option and specify the file path to write to.

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

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

-The o option indicates that the output file is created if it does not exist, and overwrites the override if it exists. If we don't want to overwrite the override but want to append it to the file, we can use the - a option.

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

3. Display more detailed time information

The time command displays less information without options. If we want more detailed information, we can use the - v option.

$ /usr/bin/time -v ping
PING ( 56(84) bytes of data.
64 bytes from ( icmp_seq=1 ttl=56 time=2.75 ms
64 bytes from ( icmp_seq=2 ttl=56 time=2.76 ms
64 bytes from ( icmp_seq=3 ttl=56 time=2.85 ms
64 bytes from ( icmp_seq=4 ttl=56 time=2.77 ms
--- 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"
        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

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

4. Custom output format

By default, the time command only outputs real, usr and sys. If we want to personalize it, we need to define its output format. The time command is also supported. The time command supports many formats, as follows:

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 as follows:

$ /usr/bin/time -f "Elapsed Time = %E, Inputs %I, Outputs %O" ping
PING ( 56(84) bytes of data.
64 bytes from ( icmp_seq=1 ttl=54 time=1.82 ms
64 bytes from ( icmp_seq=2 ttl=54 time=1.86 ms
--- 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 result to wrap, you can add the line in the corresponding place, for example:

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

The output is similar to this:

Elapsed Time = 0:03.92
Inputs 0
Outputs 0

All I see is true love. Can I go after I like it? Your "three company" is Liang Xu's biggest driving force for continuous creation!

  1. Focus on the original official account "Linux", and get the latest Linux dry cargo for the first time!
  2. The official account is back in the background.
  3. Follow my blog:

Tags: Linux socket shell less

Posted on Sun, 05 Apr 2020 21:35:52 -0700 by maGGot_H