简体   繁体   English

如何调试 php-fpm 性能?

[英]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.网页加载速度非常慢,甚至开始发送页面数据大约需要 6 秒,然后在 0.2 秒内发送并在 0.19 秒内生成。

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我怀疑是php或者浏览器引起的,所以问题一定是nginx和php5-fpm处理的服务器

A server admin said that indeed the problem was caused by a misconfigured fpm or nginx服务器管理员说问题确实是由配置错误的 fpm 或 nginx 引起的

How can I debug the cause of the slowdown?如何调试速度下降的原因?

Setup: php5.3, mysql5, linux, nginx, php5-fpm设置:php5.3、mysql5、linux、nginx、php5-fpm

This question is probably too broad for StackOverflow, as the question could span several pages and topics.这个问题对于 StackOverflow 来说可能太宽泛了,因为这个问题可能跨越多个页面和主题。

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.但是,如果问题只是我如何调试 PHP-FPM 的性能,那么答案会容易得多 - 使用 Strace 和下面的脚本。

#!/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.该脚本会将strace附加到所有正在运行的 php-fpm 实例。 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.任何到达 php-fpm 的对 web 服务器的请求都将记录它们的所有系统调用,这将允许您检查哪些系统调用花费的时间最多,这将使您能够找出首先需要优化的内容。

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.另一方面,如果您可以从 Strace 输出中看到 PHP-FPM 正在快速处理每个请求,它也将允许您消除该问题,并允许您调查 nginx,以及 nginx 如何与 PHP 对话- FPM,这也可能是问题所在。

@Danack saved my life. @Danack救了我的命。 But I had to change the command to get the MASTER_PID:但是我必须更改命令才能获取 MASTER_PID:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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