Here is the stack of my web app. Simply nginx + php-fpm + php5.3 + mysql + memcache. Recently we deployed some refactored code. Which involves SQL refactoring and caching adjustments. We found that after the deployment, the server load had an sharp growth. The memory usage climbed even sharper. And from the top command many php-fpm processes were using 2 times of memories than before. So yes there's something wrong in the deployed code. The problem only occured in production env, in test & dev envs it's all fine, so it's correlated to traffic.
My question is generally how can I find out which requests(scripts) or which parts of my code are consuming too much memory? What's the easiest way to find out?
Thanks a lot.
Create an include file to log the memory usage when the program exits, then map it using auto-prepend. If this were apache, then it'd be safe to write this to stderr and it would appear in the error_log - not sure if this works with nginx:
<?php
function logit()
{
$line = $_SERVER['REQUEST_URI']
. ' ' . memory_get_peak_usage(true);
// if stderr works...
$stderr = fopen('php://stderr', 'w');
fputs(stderr, date('r') . ' ' . $line);
fclose($stderr);
// alternatively
openlog("php_memory", LOG_PID | LOG_PERROR, LOG_LOCAL0);
syslog(LOG_INFO, $line);
closelog();
}
register_shutdown_function('logit');
We are using XHProf for memory profiling. It's not perfect, but you get a pretty good sense of whats happening. If you are running it more then once (which is what you need to do to find the spiking reqeust) I also recommend to use the following GUI: XHPROF GUI
regards
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