Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to test the command run time in Linux

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to test the command run time in Linux", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to test command run time in Linux".

1. Basic usage of time command

The most basic use 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 indicates the time it takes from the time we execute the ping command to the time we finally press ctrl+c to terminate; user and sys represent the time that the ping command runs in user space and kernel space, respectively.

two。 Write time information to a 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.

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

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

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

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

3. Show more detailed time information

The time command displays less information without an option, and if we want more detailed information, 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 Elapsed 2.851 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): 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 requiring 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. Custom output format

By default, the time command outputs only three pieces of real,usr,sys, and if we want to personalize it, we can define its output format, and the time command also supports it. There are many formats supported by the time command, 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 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.859 packet loss 1.879 ms Elapsed Time = 0icmp_seq=2 ttl=54 time=1.86 ms 03.92, Inputs 0, Outputs 0

If you want the output to have line breaks, you can add\ nin the corresponding place, such as:

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

The result of this output is similar to this:

Elapsed Time = 0Outputs 03.92 Inputs 0 and above are all the contents of the article "how to run time testing commands in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 297

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report