利用Php-fpm慢日志slowlog检测php脚本性能的调研

阅读数:2231 发布时间:2016-10-04 10:59:54

作者:w4why 标签: w4why php fpm slowlog 性能

在日常开发中,如果遇到502之类的情况那我们该如何去排错,首先我们想到的是检测脚本的性能,也就是脚本的运行时间,在fpm里提供了我们一个很好的工具用来检测运行过慢的脚本,它就是慢日志(slowlog).

php-fpm.conf的配置文件中有一个参数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 '0s' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_slowlog_timeout = 0

如图:

img

在这里关键的地方在于: request_slowlog_timeout 这一句话, 它表示如果哪个脚本执行时间大于指定秒数,会记录这个脚本到慢日志文件中.

慢日志文件位置默认在php的安装目录下的log文件夹中,可以通过修改slowlog = var/log/php/php-fpm.log.slow参数来指定。

这样我们完成了慢日志的配置.
接下来不要忘记重启fpm:

service php5.5.-fpm restart

我们就可以到相应日志查看相关信息了,慢日志会记录下进程号,脚本名称,具体哪个文件哪行代码的哪个函数执行时间过长。

[21-Nov-2013 14:30:38] [pool www] pid 11877
script_filename = /usr/local/lnmp/nginx/html/www.xyt.com/www/xyt.php
[0xb70fb88c] file_get_contents() /usr/local/lnmp/nginx/html/www.xyt.cn/www/xyt.php:2
[21-Nov-2013 14:15:23] ERROR: [pool www] 'slowlog' must be specified for use with 'request_slowlog_timeout'

最后是需要补充的是不同php版本slowlog配置的不同:
PHP 5.3.3 之前版本设置如下:

The timeout (in seconds) for serving a single request after which the worker process will be terminated
Should be used when 'max_execution_time' ini option does not stop script execution for some reason
'0s' means 'off'
<value name="request_terminate_timeout">10s</value>
The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
'0s' means 'off'
<value name="request_slowlog_timeout">1s</value>
The log file for slow requests
<value name="slowlog">logs/slow.log</value>

PHP 5.3.3 之后版本设置如下:

; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
request_slowlog_timeout = 1s
; The log file for slow requests
; Default Value: /usr/local/php/log/php-fpm.log.slow
slowlog = /usr/local/php/log/php-fpm.log.slow
; The timeout for serving a single request after which the worker process will
; be killed. This option should be used when the 'max_execution_time' ini option
; does not stop script execution for some reason. A value of '0' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
request_terminate_timeout = 10s

have a nice day

相关文章推荐: