I have created a benchmark class that allows the user to insert for example
$timer->checkpoint('1');
to check out some code for time,memory consumption and such.... and at the end of the code if she/he wants to test it she/he has to insert
$result=$timer->result();
this gives out some data to public function result() like e.g. memory usage (using memory_get_peak_usage) and time consumption (microtime()).
This all works just fine for me.
But how can I use the combination of existing built-in php functions to get a value that can be considered a CPU consumption?
It has been quite easy to calculate how much time has been spent on a certain piece of code using the built-in functions, but I've been having trouble thinking of a way how to get the CPU consumption for a certain piece of code.
Hate to answer my own question but I did a lot of research, and here I go... Basically what I did was this...
$dat=getrusage();
foreach ($dat as $key => $val) {
$usertime= $dat['ru_utime.tv_usec'];
$systemtime= $dat['ru_stime.tv_usec'];
$finalresultcpu= ($systemtime + $usertime);
}
$this->_cpuTrackers[$name]=$finalresultcpu;
return $this;
}
As you can see I used a getrusage php built-in function. Which gives out an array with a whole bunch of info, of which most was pretty useless to me, except ru_utime.tv_usec(user time) and ru_stime.tv_usec(system time). To see how much CPU power the script has consumed, we need to look at the 'user time' and 'system time' values and as you can see in the code, add it to get the time which is in fact a cpu usage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With