Last week I measured the performance impact of sending and receiving messages over a Bluetooth link to see how well it would scale up if needed. The metrics I chose were the percentages of CPU and RAM used by the running process. To collect this data, two common tools came to mind:
top. Both of them were capable of providing the CPU and RAM usage of running processes, but they differed in a few key aspects. At first
ps seemed to be the best tool for the job since it outputs a snapshot of the current statistics of running processes. Meanwhile
top is mainly used for an interactive display of current processes. However, the main deciding factor that pushed me to use
top instead of
ps is the way each of them estimated CPU usage. According to their respective man pages
ps calculates the CPU usage based on the time spent running during the entire lifetime of the program whereas
top bases its calculation on the time spent running since the last refresh. The difference is minor for most purposes, but since I wanted to sample the CPU usage when sending messages at different rates, the CPU usage should only be based on the time since the sampling began to when it ended. If it were based on the entire time running, the results would be polluted with the periods of time before messages were sent when the connection was still being set up. In addition, the process would have to be restarted to send messages at different rates or else
ps would report a mixed value for its usage.
To sum up what I found,
ps is still useful if you want to know how much CPU time the process has cumulatively used. However, if you want to know how much CPU time the process used during some interval of time,
top is preferred.