Reputation: 34006
I read this but it doesn't fit my solution.
I need to find out memory and CPU time bottlenecks in my CakePHP 2 application.
With microtime
and memory_get_usage
in controller actions I found out some clues. I fixed some of with this. But it is so hard to diagnose every controller action one by one.
I need to log CPU and memory loads for each of my actions. I'm planning to put 2 global variables in my controller. And calculate them inside beforeFilter and afterFilter and log them for checking afterwards. Is this proper way or can you recommend another solution?
class AppController extends Controller {
var $requestStartTime = 0;
var $requestDifTime = 0;
var $memoryBefore = 0;
var $memoryAfter = 0;
function beforeFilter() {
$requestStartTime = microtime(true);
$memoryBefore =memory_get_usage(true);
}
function afterFilter() {
$requestDifTime = microtime(true) - $requestStartTime;
$memoryAfter = memory_get_usage(true);
$myFile = TMP.'logs'.DS.'mylog.txt';
$fh = fopen($myFile, 'a');
$string = "start time:" . $requestStartTime .
" dif time: " . $requestDifTime
" memory usage: " . $memoryBefore . " and " . $memoryAfter
."\n";
fwrite($fh,$string);
fclose($fh);
}
}
Upvotes: 3
Views: 2296
Reputation: 1460
The best tool I have found when working with PHP, and any PHP framework including CakePHP is "Xdebug". Xdebug is a PHP extension that can be enabled to provide profiling output files that can be analysed with tools like "Webgrind" (or CallGrind, and others).
Webgrind will take the xdebug trace files and provide you with a visual tree of time spent and resource allocation. This enables you to selectively drill down into the method and function calls made during the execution of a system, and find where time is being lost, and where resources are being allocated.
In addition, Xdebug enables you to start debugging your application. You can allocate breakpoints, and pause execution, modify values and step through your code providing you with a more flexible debugging approach to development.
This has been a valuable tool while building things with CakePHP, and also while building on the core for CakePHP itself.
Upvotes: 4