It's better to use something like time
command.
Bash also has a time
built-in, what I'm suggesting is using time
binary located at: /usr/bin/time
.
For one runtime:
command time -f "\nElapsed: %E \nUser: %U \nSystem: %S \nMemory: %M\n" \
./MyCommand 1> /dev/null
Which outputs:
Elapsed: 0:00.01
User: 0.00
System: 0.00
Memory: 2412
command
: forces bash to use /usr/bin/time
instead of time
built-in.
You can use time
with a loop to get a "avrage", "min", "max" of a specific resource say memory:
This code will run ./COMMAND
commands 1000 times, then prints out the "min, max, avg" of its total (data+stack+text) memory usage.
#!/bin/bash
tmpfile=`mktemp`
for i in {1..1000}; do command time -ao $tmpfile -f "%K" ./COMMAND 1>/dev/null; done;
awk 'NR == 1 {min = $0} $0 > max {max = $0} {total += $0} END {print total/NR, min, max}' $tmpfile
rm $tmpfile
Here is the output:
2436.89 2524 2324
You can change %K
with:
%E
: Elapsed real time
%I
: Number of file inputs
%P
: Percentage of the CPU that this job got
%k
: Number of signals delivered to the process
%U
: CPU usage in user mode
%S
: CPU usage in kernel mode
See man time
Thanks to muru for writing a more clear awk
statement.