Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP equivalent to MySQL's slow query log?

I am working on optimizing my site, and I have had the MySQL slow queries log on for a few days now, but after going through >260M queries, it only logged 6 slow queries, and those were special ones executed by me on phpMyAdmin. I am wondering if there is something to log slow PHP page execution time so that I can find certain pages that are hogging resources, rather than specific queries.

like image 604
James Simpson Avatar asked Jun 04 '09 07:06

James Simpson


5 Answers

First, there is xdebug, which has a profiler, but I wouldn't use that on a production machine, since it injects code and brings the speed to a crawl. Very good for testing environments, though.

If you want to measure speeds on a productive environment, I would just to the measuring manually. microtime() is the function for these things in PHP. Assuming you have a header.php and a footer.php which get called by all php scripts:

# In your header.php (or tpl)
$GLOBALS['_execution_start'] = microtime(true);

# In your footer.php (or tpl)
file_put_contents(
    '/tmp/my_profiling_results.txt',
    microtime(true) - $GLOBALS['_execution_start'] . ':' . print_r($_SERVER, true) . "\n",
    FILE_APPEND
);
like image 126
soulmerge Avatar answered Nov 15 '22 08:11

soulmerge


what about auto_prepend_file and auto_append_file, just wrote a post about it http://blog.xrado.si/post/php-slow-log

like image 26
xrado Avatar answered Nov 15 '22 08:11

xrado


You could wrap your scripts in a simple timer, like this:

/*in your header or at the top of the page*/
$time_start = microtime(true); 

/* your script goes here */

/*in your footer, or at the bottom of the page*/
$time_end = microtime(true);
$time = $time_end - $time_start;   
echo "It took $time seconds\n";

Note that will add two function executions and a tiny bit of math as overhead.

like image 40
karim79 Avatar answered Nov 15 '22 10:11

karim79


Could you not register a shutdown function that calls an end to the timer? http://us3.php.net/register_shutdown_function That way you only need to start the timer wherever you think there might be a problem.

like image 1
SeanJA Avatar answered Nov 15 '22 09:11

SeanJA


If you are using FastCGI for executing your PHP scripts you can use the FastCGI Process Manager (FPM, php-fpm) which also supports a so called “slowlog”.

You can enable it in the php configuration of your php-fpm (for debian this is in /etc/php5/fpm/pool.d/www.conf) with the config options: request_slowlog_timeout and slowlog.

request_slowlog_timeout

The timeout for serving a single request after which a PHP backtrace will be dumped to the 'slowlog' file. A value of '0' means 'Off'. Available units: s(econds)(default), m(inutes), h(ours), or d(ays). Default value: 0.

slowlog

The log file for slow requests. Default value: #INSTALL_PREFIX#/log/php-fpm.log.slow.

from http://php.net/manual/en/install.fpm.configuration.php

Also see: http://php.net/manual/en/install.fpm.php and http://rtcamp.com/tutorials/php/fpm-slow-log/

like image 1
white_gecko Avatar answered Nov 15 '22 08:11

white_gecko