PHP5 getrusage() returning incorrect information?
- by Andrew
I'm trying to determine CPU usage of my PHP scripts. I just found this article which details how to find system and user CPU usage time (Section 4).
However, when I tried out the examples, I received completely different results.
The first example:
sleep(3);
$data = getrusage();
echo "User time: ".
($data['ru_utime.tv_sec'] +
$data['ru_utime.tv_usec'] / 1000000);
echo "System time: ".
($data['ru_stime.tv_sec'] +
$data['ru_stime.tv_usec'] / 1000000);
Results in:
User time: 29.53
System time: 2.71
Example 2:
for($i=0;$i<10000000;$i++) {
}
// Same echo statements
Results:
User time: 16.69
System time: 2.1
Example 3:
$start = microtime(true);
while(microtime(true) - $start < 3) {
}
// Same echo statements
Results:
User time: 34.94
System time: 3.14
Obviously, none of the information is correct except maybe the system time in the third example. So what am I doing wrong? I'd really like to be able to use this information, but it needs to be reliable.
I'm using Ubuntu Server 8.04 LTS (32-bit) and this is the output of php -v:
PHP 5.2.4-2ubuntu5.10 with Suhosin-Patch 0.9.6.2 (cli) (built: Jan 6 2010 22:01:14)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies