6

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.

gagneet
  • 35,729
  • 29
  • 78
  • 113
  • Possible duplicate of [How can I get the CPU usage and memory usage of a single process on Linux (Ubuntu)?](http://stackoverflow.com/questions/1221555/how-can-i-get-the-cpu-usage-and-memory-usage-of-a-single-process-on-linux-ubunt) – Ciro Santilli OurBigBook.com Nov 13 '16 at 15:25
  • 2
    @CiroSantilli烏坎事件2016六四事件法轮功: that question was asked later than this one, and would be a duplicate, not this one :-) – gagneet Nov 25 '16 at 00:11
  • http://meta.stackexchange.com/questions/147643/should-i-vote-to-close-a-duplicate-question-even-though-its-much-newer-and-ha ;-) – Ciro Santilli OurBigBook.com Nov 25 '16 at 00:18

2 Answers2

2

If you are looking for capturing CPU and Mem utilization dynamically for entire linux box, then following command can help you too:

CPU

vmstat -n 15 10| awk '{now=strftime("%Y-%m-%d %T "); print now $0}'> CPUDataDump.csv &

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 example

awk '{now=strftime("%Y-%m-%d %T "); print now $0}' this will dump the timestamp of each iteration

in the end, the dump file with & for continuation

Memory

free -m -s 10 10 | awk '{now=strftime("%Y-%m-%d %T "); print now $0}'> DataDumpMemoryfile.csv &

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 iteration

in the end, the dump & for continuation

mucka
  • 1,286
  • 3
  • 20
  • 36
1

I'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:

time dd if=/dev/zero bs=1K of=/dev/null count=1024000
1024000+0 records in
1024000+0 records out
1048576000 bytes (1.0 GB) copied, 0.738194 seconds, 1.4 GB/s
0.218u 0.519s 0:00.73 98.6%     0+0k 0+0io 0pf+0w <== that's time result
Drakosha
  • 11,925
  • 4
  • 39
  • 52
  • could you provide an example? i tried the same within shell, but it expects the process to get over and i have multiple instances of the call running in the same script ... written one line after the other. – gagneet Apr 30 '09 at 13:35
  • I added an example, i think you'll need to collect the output and sum it manually. Another suggestion to make all the runs in one script and do 'time' for it. – Drakosha Apr 30 '09 at 15:16