Profile slow PHP pages in production

北城以北 提交于 2020-01-01 05:30:17

问题


Is there a way of profiling only slow PHP pages on a production server?

At the moment we're logging slow pages to a text file, but without more information it's hard to say why they're slow (not consistently slow).

I've used the Xdebug profiler before, but I really don't want to enable this on our production servers as we are likely to get 100's of requests per second. I've also used Zend Platform, but I don't really want to install that again.


回答1:


You might alter your Apache/HTTP Server logs to record the time spent serving each request. Follow this guide for example. Then you can gather data for how long each request takes, identify the slow pages/requests and use XDebug or WebGrind to further analyse the causes.

Easy, and no great drain on your production server.




回答2:


You could write timer statements are parts of the slow pages to narrow it down. Then once some data is built up, rinse and repeat.

define('START_TIME', microtime(true));
function timer() {
    static $last;
    $time_since_start = microtime(true) - START_TIME;
    $time_since_last = microtime(true) - $last;
    // Do something with $time vars
    $last = microtime(true);
}

Also check out this: http://particletree.com/features/php-quick-profiler/

It might suit your needs




回答3:


I'd be leery of of a whole new library for a production server. When I'm debugging I like to use the *auto_prepend_file* and *auto_append_file* directives in php.ini. You could easily do as suggested above with this method and get a very exact time for each page load.

If you are worried about only slow load pages measured in seconds, here is a quick and dirty solution that subtracts the server request time from approximate finish time in an auto-appended file. You can then store the result in a db or flat file.

eg in php.in

auto_append_file = [location]/my_timer.php

my_timer.php

define('TRIGGER_TIME_LOG','3'); // Minimum number of timer seconds to log page load 

$time = time() - $_SERVER['REQUEST_TIME']; // Page load time

if($time >= TRIGGER_TIME_LOG)
{
  /*
   * DO LOGGING TO DB OR FLAT FILE HERE
   */
}



回答4:


I suggest you to take a look at the webgrind project. You can activate profiling per-query, which would maybe allow you to get profiling data from your production server without a huge performance impact.

I hope this will help you




回答5:


I know this is not the best solution, but...

You can create a helper class to log every process you have, along with start and end time. I know you do it already for whole process, but for each function start and end you can add a "Profiler::logtime(FUNC)";



来源:https://stackoverflow.com/questions/4039613/profile-slow-php-pages-in-production

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!