Page 1 of 1
Getting CPU usage information in PHP
Posted: Wed Aug 09, 2006 1:46 pm
by idono
I am looking for a way to get process specfic CPU usage information. I took a look at win32ps.dll, and the function win32_ps_stat_proc... but all that has is CPU Time, I need the actual CPU usage at the time that the query takes place.
Any ideas? Any help would be appreciated.
Posted: Wed Aug 09, 2006 2:05 pm
by Weirdan
Posted: Wed Aug 09, 2006 2:15 pm
by idono
With those formulas in mind, I must take two different snapshots of the CPU time.. How can I assure that I can get PHP to wait for an exact time in order to ensure an accurate reading?
Posted: Wed Aug 09, 2006 3:36 pm
by Weirdan
well, you can't force PHP to wait for exact time period, but you can get the period of time elapsed between two invocations of
microtime() function.
Is resolution down to microsecond enough for your task?
Posted: Wed Aug 09, 2006 4:05 pm
by idono
Yes, it is. So you are saying, that I should query the CPU Time and use the microtime() function subsequently in the code, and then do the math based on the time returned from each microtime() function
Posted: Wed Aug 09, 2006 4:08 pm
by Weirdan
...unless you manage to find the api call to return the info you want in WinAPI docs =)
Posted: Wed Aug 09, 2006 4:10 pm
by bokehman
Micro time is slow the first time it is run so run it once to initialise it before using it for your timing function.
Posted: Wed Aug 09, 2006 5:00 pm
by nickvd
bokehman wrote:Micro time is slow the first time it is run so run it once to initialise it before using it for your timing function.
Assuming you are right (which i have no reason to believe otherwise), why would that be? Does the function itself have to init something the first time it's run?
Posted: Wed Aug 09, 2006 5:19 pm
by bokehman
nickvd wrote:why would that be?
I have no idea about PHP's behind the scenes mechanisms but I guess functions aren't initialised unless they are going to be used. The following code shows what I'm saying to be fact:
Code: Select all
<?php
function elapsed_time_in_microseconds($start, $end){
$start = explode(' ', $start);
$start = $start[0] + $start[1];
$end = explode(' ', $end);
$end = $end[0] + $end[1];
return (round(($end - $start) * 1000000));
}
$first = microtime();
$second = microtime();
$third = microtime();
$forth = microtime();
$fifth = microtime();
print(elapsed_time_in_microseconds($first, $second) . ' microseconds<br>'); // 36 microseconds
print(elapsed_time_in_microseconds($second, $third) . ' microseconds<br>'); // 9 microseconds
print(elapsed_time_in_microseconds($third, $forth) . ' microseconds<br>'); // 10 microseconds
print(elapsed_time_in_microseconds($forth, $fifth) . ' microseconds<br>'); // 9 microseconds
?>
This also shows that
microtime() itself takes a finite amount of time to run and this time would need to be subtracted from whatever you are timing.
Also you need to understand you are only timing runtime. Compile time of course is not included.
Posted: Wed Aug 09, 2006 8:14 pm
by Ambush Commander
Erm,
usleep() or
sleep()? usleep preferably, unless you're not running PHP 5.