Simplest way of measuring cpu-memory of a single process
The problem with the core tools - top (or htop), pidstat, iostats, mpstat, vmstat, ps, etc is that they are generally useful for looking at existing running processes. But what if you want a simple perf stat <command>
like interface?
1. Pidstat's "-e" flag
pidstat -ur -t --human 1 -e stress-ng --cpu 1
- Prints out an entry every time interval. Here 1.
- Prints out an average at the very end.
- Can't redirect stdout.
- CPU is not working at all!
2. time (yes, time)
Surprisingly time can print out a lot more than just CPU time. Look at the man page!
/usr/bin/time -f "%P, %M Kbytes" stress-ng --cpu 1
- Prints out a single number at the very end. Here, Avg CPU utilization and Max memory consumption.
- This single number approach might not be too coarse: you have no idea of variations during program runtime.
3. psrecord
Install: pip install psrecord
psrecord --include-children --plot output.png --log output.log "stress-ng --cpu 1"
- Samples CPU util and Memory and outputs to log file and creates a png with both values in a single plot!
- Can use the
--interval
flag to control how often to take samples. The default is as fast as it can, which seems to be a few milliseconds! - Switch to using intervals of tens or hundreds of milliseconds for best results - graphs that are acutally readable.
- The log file can be used for other manglings.