<p>I'm running some <a href="http://jakarta.apache.org/jmeter/" rel="noreferrer">JMeter</a> tests against a Java process to determine how responsive a web application is under load (500+ users). JMeter will give the response time for each web request, and I've written a script to ping the Tomcat Manager every X seconds which will get me the current size of the JVM heap.</p>

<p>I'd like to collect stats on the server of the % of CPU being used by Tomcat. I tried to do it in a shell script using <code>ps</code> like this:</p>

<pre><code>PS_RESULTS=`ps -o pcpu,pmem,nlwp -p $PID`
</code></pre>

<p>...running the command every X seconds and appending the results to a text file. (for anyone wondering, <code>pmem</code> = % mem usage and <code>nlwp</code> is number of threads)</p>

<p>However I've found that this gives a different definition of "% of CPU Utilization" than I'd like - according to the manpages for ps, <code>pcpu</code> is defined as:</p>

<blockquote>
  <p>cpu utilization of the process in "##.#" format. It is the CPU time used divided by the time the process has been running (cputime/realtime ratio), expressed as a percentage. </p>
</blockquote>

<p>In other words, <code>pcpu</code> gives me the % CPU utilization for the process for the <em>lifetime</em> of the process.</p>

<p>Since I want to take a sample every X seconds, I'd like to be collecting the CPU utilization of the process at the current time only - similar to what <code>top</code> would give me
(CPU utilization of the process since the last update). </p>

<p>How can I collect this from within a shell script? </p>

[{'answer_id': 50334, 'author': 'Ben Collins', 'author_id': 3279, 'author_profile': 'https://Stackoverflow.com/users/3279', 'pm_score': 5, 'selected': True, 'text': '<p>Use <code>top -b</code> (and other switches if you want different outputs).  It will just dump to stdout instead of jumping into a curses window.</p>\n'}, {'answer_id': 50362, 'author': 'Adam Wright', 'author_id': 1200, 'author_profile': 'https://Stackoverflow.com/users/1200', 'pm_score': 2, 'selected': False, 'text': '<p>Off the top of my head, I\'d use the /proc filesystem view of the system state - Look at <em>man 5 proc</em> to see the format of the entry for /proc/PID/stat, which contains total CPU usage information, and use /proc/stat to get global system information. To obtain "current time" usage, you probably really mean "CPU used in the last N seconds"; take two samples a short distance apart to see the current rate of CPU consumption. You can then munge these values into something useful. Really though, this is probably more a Perl/Ruby/Python job than a pure shell script.</p>\n\n<p>You might be able to get the rough data you\'re after with /proc/PID/status, which gives a Sleep average for the process. Pretty coarse data though.</p>\n'}, {'answer_id': 50697, 'author': 'Tim', 'author_id': 5284, 'author_profile': 'https://Stackoverflow.com/users/5284', 'pm_score': 3, 'selected': False, 'text': '<p>The most useful tool I\'ve found for monitoring a server while performing a test such as JMeter on it is <a href="http://dag.wieers.com/home-made/dstat/." rel="noreferrer">dstat</a>. It not only gives you a range of stats from the server, it outputs to csv for easy import into a spreadsheet and lets you extend the tool with modules written in Python.</p>\n'}, {'answer_id': 14520547, 'author': 'mighq', 'author_id': 1242724, 'author_profile': 'https://Stackoverflow.com/users/1242724', 'pm_score': 0, 'selected': False, 'text': '<p>also use 1 as iteration count, so you will get current snapshot without waiting to get another one in $delay time.</p>\n\n<pre><code>top -b -n 1\n</code></pre>\n'}, {'answer_id': 15420777, 'author': 'Bilzard', 'author_id': 2170865, 'author_profile': 'https://Stackoverflow.com/users/2170865', 'pm_score': 2, 'selected': False, 'text': "<p>User load: <code>top -b -n 2 |grep Cpu |tail -n 1 |awk '{print $2}' |sed 's/.[^.]*$//'</code>\nSystem load: <code>top -b -n 2 |grep Cpu |tail -n 1 |awk '{print $3}' |sed 's/.[^.]*$//'</code>\nIdle load: <code>top -b -n 1 |grep Cpu |tail -n 1 |awk '{print $5}' |sed 's/.[^.]*$//'</code></p>\n\n<p>Every outcome is a round decimal.</p>\n"}, {'answer_id': 65937910, 'author': 'isalgueiro', 'author_id': 1913856, 'author_profile': 'https://Stackoverflow.com/users/1913856', 'pm_score': 0, 'selected': False, 'text': '<p>This will not give you a per-process metric, but the <a href="https://amanusk.github.io/s-tui/" rel="nofollow noreferrer">Stress Terminal UI</a> is super useful to know how badly you\'re punishing your boxes. Add <code>-c</code> flag to make it dump the data to a CSV file.</p>\n<p><a href="https://i.stack.imgur.com/lXNuM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lXNuM.png" alt="enter image description here" /></a></p>\n'}]
['https://Stackoverflow.com/questions/50312', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4249/']