Tuesday, February 5, 2013

Timing Command Line Applications on Linux



The command line time utility available on linux as /usr/bin/time can be used to measure the elapsed time of test applications launched from the command line, and also measure other information such as cpu time and other metrics, outputting the result as a csv file that can then be easily charted.  Using the "format" option of the time utility allows the output to be formatted as csv with a single line per app run, rather than the default output which contains multiple lines of output.

Script


echo TestCase , Seconds , RealTime , CpuTimeUser , CpuTimeKernel
/usr/bin/time --format="MyTestCase1 , %e , %E , %U , %S" ./mytestcase1.sh mytestcase1parameter

/usr/bin/time --format="MyTestCase2 , %e , %E , %U , %S" java -jar mytestcase2.jar


Output


TestCase , Seconds , RealTime , CpuTimeUser , CpuTimeKernel
MytestCase1 , 679.33 , 11:19.33 , 227.13 , 105.85
MytestCase2 , 460.99 , 7:40.99 , 96.32 , 44.64

Format String


The format string can be configured to output the following options:

       Time
       %E     Elapsed real time (in [hours:]minutes:seconds).
       %e     (Not in tcsh.) Elapsed real time (in seconds).
       %S     Total number of CPU-seconds that the process spent in kernel mode.
       %U     Total number of CPU-seconds that the process spent in user mode.
       %P     Percentage of the CPU that this job got, computed as (%U + %S) / %E.

       Memory
       %M     Maximum resident set size of the process during its lifetime, in Kbytes.
       %t     (Not in tcsh.) Average resident set size of the process, in Kbytes.
       %K     Average total (data+stack+text) memory use of the process, in Kbytes.
       %D     Average size of the processâs unshared data area, in Kbytes.
       %p     (Not in tcsh.) Average size of the processâs unshared stack space, in Kbytes.
       %X     Average size of the processâs shared text space, in Kbytes.
       %Z     (Not in tcsh.) Systemâs page size, in bytes.  This is a per-system constant, but varies between systems.
       %F     Number of major page faults that occurred while the process was running.  These are faults where the page has to be  read  in  from
              disk.
       %R     Number of minor, or recoverable, page faults.  These are faults for pages that are not valid but which have not yet been claimed by
              other virtual pages.  Thus the data in the page is still valid but the system tables must be updated.
       %W     Number of times the process was swapped out of main memory.
       %c     Number of times the process was context-switched involuntarily (because the time slice expired).
       %w     Number of waits: times that the program was context-switched voluntarily, for instance while waiting for an I/O operation  to  com-
              plete.

       I/O
       %I     Number of file system inputs by the process.
       %O     Number of file system outputs by the process.
       %r     Number of socket messages received by the process.
       %s     Number of socket messages sent by the process.
       %k     Number of signals delivered to the process.
       %C     (Not in tcsh.) Name and command-line arguments of the command being timed.
       %x     (Not in tcsh.) Exit status of the command.