Get CPU usage


#1

Hello everybody,

I would like to get the % of CPU usage of a current Ruby script.
For example, I would like to start my script like and allow it to get
and print this such as:

./application.rb
=> 12:10 => CPU usage is 3.2%
=> 12:15 => CPU usage is 3.1%
=> 12:20 => CPU usage is 3.1%
=> 12:25 => CPU usage is 3.5%

Have you got an idea? Many thanks.


#2

On 14.12.2008 14:01, Panda B. wrote:

=> 12:25 => CPU usage is 3.5%

Have you got an idea? Many thanks.

$ ruby -e ‘10.times { 100000.times { 1 + 2 }; p Process.times }’
#
#
#
#
#
#
#
#
#
#

You could correlate that to real time that has passed.

$ ruby -e ‘t=Time.now; 10.times { sleep 2;
pt=Process.times;p((pt.utime+pt.stime)/(Time.now-t)) }’
0.015
0.00749812546863284
0.00499916680553241
0.00374953130858643
0.002999700029997
0.00249979168402633
0.00214270409256482
0.00187488281982376
0.00166657407921782
0.00149992500374981

robert@fussel ~
$ ruby -e ‘t=Time.now; 10.times {10_000.times {1+2};
pt=Process.times;p((pt.utime+pt.stime)/(Time.now-t)) }’
19.25
8.55555555555556
5.13333333333333
4.89473684210526
3.875
3.20689655172414
3.20588235294118
2.86842105263158
2.5952380952381
2.31914893617021

Obviously there is a factor missing…

robert@fussel ~

robert