Getting CPU usage information in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
idono
Forum Newbie
Posts: 3
Joined: Wed Aug 09, 2006 1:40 pm

Getting CPU usage information in PHP

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

idono
Forum Newbie
Posts: 3
Joined: Wed Aug 09, 2006 1:40 pm

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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?
idono
Forum Newbie
Posts: 3
Joined: Wed Aug 09, 2006 1:40 pm

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

...unless you manage to find the api call to return the info you want in WinAPI docs =)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post 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?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Erm, usleep() or sleep()? usleep preferably, unless you're not running PHP 5.
Post Reply