I am running a shell script to execute a c++ application, which measures the performance of an api. i can capture the latency (time taken to return a value for a given set of parameters) of the api, but i also wish to capture the cpu and memory usage alongside at intervals of say 5-10 seconds.
is there a way to do this without effecting the performance of the system too much and that too within the same script? i have found many examples where one can do outside (independently) of the script we are running; but not one where we can do within the same script.
If you are looking for capturing CPU and Mem utilization dynamically for entire linux box, then following command can help you too:
CPU
vmstat
is used for collection of CPU counters-n
for delay value, in this case it's 15, that means after every 15 sec, stats will be collected.then
10
is the number of intervals, there would be 10 iterations in this exampleawk '{now=strftime("%Y-%m-%d %T "); print now $0}'
this will dump the timestamp of each iterationin the end, the dump file with
&
for continuationMemory
free
is for mem stats collection-m
this is for units of mem (you can use-b
for bytes,-k
for kilobytes,-g
for gigabytes)then
10
is the number of intervals (there would be 10 iterations in this example)awk'{now=strftime("%Y-%m-%d %T "); print now $0}'
this will dump the timestamp of each iterationin the end, the dump
&
for continuationI'd suggest to use 'time' command and also 'vmstat' command. The first will give CPU usage of executable execution and second - periodic (i.e. once per second) dump of CPU/memory/IO of the system.
Example: