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.
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
);
what about auto_prepend_file and auto_append_file, just wrote a post about it http://blog.xrado.si/post/php-slow-log
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.
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.
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/
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