简体   繁体   中英

How to debug php-fpm performance?

Webpages are loading very slow, it takes them around 6 seconds to even start sending the page data, which is then sent in a matter of 0.2 seconds and is generated in 0.19 seconds.

I doubt that it is caused by php or the browser, so the problem must be with the server which is handled by nginx and php5-fpm

A server admin said that indeed the problem was caused by a misconfigured fpm or nginx

How can I debug the cause of the slowdown?

Setup: php5.3, mysql5, linux, nginx, php5-fpm

This question is probably too broad for StackOverflow, as the question could span several pages and topics.

However if the question was just how do I do debug the performance of PHP-FPM then the answer would be much easier - use Strace and the script below.

#!/bin/bash

mkdir trc
rm -rf trc/*.trc

additional_strace_args="$1"

MASTER_PID=$(ps auwx | grep php-fpm | grep -v grep | grep 'master process'  | cut -d ' ' -f 7)

summarise=""
#shows total of calls - comment in to get 
#summarise="-c"

nohup strace -r $summarise -p $MASTER_PID -ff -o ./trc/master.follow.trc >"trc/master.$MASTER_PID.trc" 2>&1 &

while read -r pid;
do
    if [[ $pid != $MASTER_PID ]]; then
        #shows total of calls
        nohup strace -r $summarise -p "$pid" $additional_strace_args >"trc/$pid.trc" 2>&1 &
    fi
done < <(pgrep php-fpm)

read -p "Strace running - press [Enter] to stop"

pkill strace

That script will attach strace to all of the running php-fpm instances. Any requests to the web server that reach php-fpm will have all of their system calls logged, which will allow you to inspect which ones are taking the most time, which will allow you to figure out what needs optimising first.

On the other hand, if you can see from the Strace output that PHP-FPM is processing each request fast, it will also allow you to eliminate that as the problem, and allow you to investigate nginx, and how nginx is talking to PHP-FPM, which could also be the problem.

@Danack saved my life. But I had to change the command to get the MASTER_PID:

MASTER_PID=$(ps auwx | grep php-fpm | grep -v grep | grep 'master process' | sed -e 's/ \+/ /g' | cut -d ' ' -f 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM