What do I set in the controller to send total render time to the view?
In production mode, I obviously want the information hidden, however I am perfectly fine in (and have become accustomed to) having the render time in an HTML comment tag within the footer. It's only a nicety, and typically unimportant, but sometimes it can be useful and informative for very trivial estimations and comparisons.
I realize this is different for CakePHP v1.2 and v1.3, but either or both answers would be great. I was unable to find the right information for either of them on my own.
Asked by BrendonKoz, on 24/2/10
1 Answer
If you're running PHP 5.1 or higher, the start of the request is available (in timestamp format) as: $_SERVER['REQUEST TIME'];
Then you can use CakePHP's getMicroTime() function and just subtract the start time from that:
class MyController extends AppController {
function my_action() {
$loadTime = getMicroTime() - $_SERVER['REQUEST_TIME'];
$this->set(compact('loadTime'));
}
}
Answered by jnayon 24/2/10
I've marked it as correct since it will work, and it should work for any framework or code. I didn't know about the new server variable (as of PHP5) REQUEST_TIME. In reading about it, and trying to determine what the difference between PHP's own microtime() and CakePHP's getMicroTime(), I found that as of PHP 5.1 microtime has an optional parameter so I can skip the overload of using CakePHP and just use microtime(true) instead. Thanks!
So yeah...
$this->set('loadtime', microtime(true) - $_SERVER['REQUEST_TIME']);
This of course would only work with PHP >= 5.1.0; thankfully all of my hosts are running >= 5.2.x.
BrendonKoz - on 25/2/10
Rating
0
Viewed
303 times
Last Activity
on 25/2/10