解决php的session异步请求堵塞问题(采用xhprof性能分析)



最近在公司项目里发现,一个页面加载非常缓慢,打开network分析了一下,发现都是卡在Waiting(TTFB)上。我怀疑是服务器什么地方导致请求堵塞了。

打开服务器开启了php-fpm的slow慢日志,我需要定位堵塞是堵塞在IO、网络还是php-fpm挂载数量上。

开启php-fpm慢日志查询   

        打开php-fpm配置文件,找到下面的配置项

; The log file for slow requests
; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
slowlog = /data/logs/php-fpm.slow.log

; 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 = 2

说明:request_slowlog_timeout设置为0就是关闭慢日志输出

然后重启php-fpm,继续观察慢日志,发现了问题所在

[11-Jul-2017 09:06:24]  [pool www] pid 6992
script_filename = /data/www/coobar/yun6.coobar.net/public/index.php
[0x00007f85e6e12930] session_start() /data/www/coobar/yun6.coobar.net/thinkphp/library/think/Session.php:117
[0x00007f85e6e128b0] boot() /data/www/coobar/yun6.coobar.net/thinkphp/library/think/Session.php:310
[0x00007f85e6e127e0] has() /data/www/coobar/yun6.coobar.net/application/core/observer/SessionObserver.php:56
[0x00007f85e6e12720] update() /data/www/coobar/yun6.coobar.net/application/core/Base.php:72
[0x00007f85e6e12670] notify() /data/www/coobar/yun6.coobar.net/application/core/Base.php:23
[0x00007f85e6e125f0] __construct() /data/www/coobar/yun6.coobar.net/thinkphp/library/think/App.php:249
[0x00007f85e6e12580] newInstanceArgs() /data/www/coobar/yun6.coobar.net/thinkphp/library/think/App.php:249
[0x00007f85e6e124b0] invokeClass() /data/www/coobar/yun6.coobar.net/thinkphp/library/think/Loader.php:410
[0x00007f85e6e123b0] controller() /data/www/coobar/yun6.coobar.net/thinkphp/library/think/App.php:373
[0x00007f85e6e12240] module() /data/www/coobar/yun6.coobar.net/thinkphp/library/think/App.php:138
[0x00007f85e6e12110] run() /data/www/coobar/yun6.coobar.net/thinkphp/start.php:18
[0x00007f85e6e120a0] [INCLUDE_OR_EVAL]() /data/www/coobar/yun6.coobar.net/public/index.php:17

发现等待都发生了session这里,我开始怀疑是不是session进行堵塞了,我们session采用的是memecade,并且在session使用后立马调用session_write_close(),还是出现了session堵塞问题,我采用xhprof进行性能分析,使用原生的php进行一步一步分析

采用直接读取session不调用session_write_close

<?php
/**
 * Created by PhpStorm.
 * User: hls
 * Date: 2017/7/21
 * Time: 下午3:09
 */
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
session_start();
$data = $_SESSION['data'];
$xhprof_data = xhprof_disable();
$XHPROF_ROOT = "/data/www/hls/yun6.hls.com/public";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";

$xhprof_runs = new \XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_testing");
echo $run_id;

exit;

前端代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script type="text/javascript" src="http://yun6.coobar.net:82/web/components/jquery/dist/jquery.min.js"></script>

<script>
    for (var i = 0;i<20;i++){
        console.log('第' + i + '次请求');
        $.get('http://yun6.hls.com:82/ajax.php?a=' + i,function ($data) {
            console.log($data);
        });
    }
</script>
</body>
</html>

查看结果


发现部分接口还是在堵塞加载

我在session_start后直接关闭session(只针对只读session的情况下)

<?php
/**
 * Created by PhpStorm.
 * User: hls
 * Date: 2017/7/21
 * Time: 下午3:09
 */
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
session_start();
session_write_close();
$data = $_SESSION['data'];
$xhprof_data = xhprof_disable();
$XHPROF_ROOT = "/data/www/hls/yun6.hls.com/public";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";

$xhprof_runs = new \XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_testing");
echo $run_id;
exit;

结果


发现还有部分接口堵塞,我查了官方文档和一些文章,解决sesison的问题就是其实用完调用session_write_close及时释放session,但是还是有接口堵塞。我查看了xhprof的性能分析


发现在性能都在堵塞了在session_start上,我怀疑是不是memcached连接数满了(因为我们session是放在memecached内),我接着尝试着直接直连memcahed拿session信息


<?php
/**
 * Created by PhpStorm.
 * User: hls
 * Date: 2017/7/21
 * Time: 下午3:09
 */
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
$memcahce = new Memcached();
$memcahce->addServer('127.0.0.1',11211);
$data = $memcahce->get('memc.sess.key.kdtttfj9b4h7ld6d15dj3qh437');
$xhprof_data = xhprof_disable();
$XHPROF_ROOT = "/data/www/hls/yun6.hls.com/public";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";

$xhprof_runs = new \XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_testing");
echo $run_id;
exit;

结果


发现接口都请求正常没有堵塞,我开始怀疑是不是php内置session的存储机制存在排他锁,为了验证这个想法我找了某框架下的session的memcached的自定义存储类

<?php
/**
 * Created by PhpStorm.
 * User: hls
 * Date: 2017/7/21
 * Time: 下午3:09
 */
require  './Memcached.php';
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
session_set_save_handler(new Memcached());
session_start();
session_write_close();
$data = $_SESSION['data'];
$xhprof_data = xhprof_disable();
$XHPROF_ROOT = "/data/www/hls/yun6.hls.com/public";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";

$xhprof_runs = new \XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_testing");
echo $run_id;
exit;

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace think\session\driver;

use SessionHandler;
use think\Exception;

class Memcached extends SessionHandler
{
    protected $handler = null;
    protected $config  = [
        'host'         => '127.0.0.1', // memcache主机
        'port'         => 11211, // memcache端口
        'expire'       => 3600, // session有效期
        'timeout'      => 0, // 连接超时时间(单位:毫秒)
        'session_name' => '', // memcache key前缀
        'username'     => '', //账号
        'password'     => '', //密码
    ];

    public function __construct($config = [])
    {
        $this->config = array_merge($this->config, $config);
        $sessionName = ini_get('memcached.sess_prefix');
        $this->config['session_name'] = empty($sessionName) ? '' : $sessionName;
    }

    /**
     * 打开Session
     * @access public
     * @param string    $savePath
     * @param mixed     $sessName
     */
    public function open($savePath, $sessName)
    {
        // 检测php环境
        if (!extension_loaded('memcached')) {
            throw new Exception('not support:memcached');
        }
        $this->handler = new \Memcached;
        // 设置连接超时时间(单位:毫秒)
        if ($this->config['timeout'] > 0) {
            $this->handler->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $this->config['timeout']);
        }
        // 支持集群
        $hosts = explode(',', $this->config['host']);
        $ports = explode(',', $this->config['port']);
        if (empty($ports[0])) {
            $ports[0] = 11211;
        }
        // 建立连接
        $servers = [];
        foreach ((array) $hosts as $i => $host) {
            $servers[] = [$host, (isset($ports[$i]) ? $ports[$i] : $ports[0]), 1];
        }
        $this->handler->addServers($servers);
        if ('' != $this->config['username']) {
            $this->handler->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
            $this->handler->setSaslAuthData($this->config['username'], $this->config['password']);
        }
        return true;
    }

    /**
     * 关闭Session
     * @access public
     */
    public function close()
    {
        $this->gc(ini_get('session.gc_maxlifetime'));
        $this->handler->quit();
        $this->handler = null;
        return true;
    }

    /**
     * 读取Session
     * @access public
     * @param string $sessID
     */
    public function read($sessID)
    {
        return $this->handler->get($this->config['session_name'] . $sessID);
    }

    /**
     * 写入Session
     * @access public
     * @param string $sessID
     * @param String $sessData
     */
    public function write($sessID, $sessData)
    {
        return $this->handler->set($this->config['session_name'] . $sessID, $sessData, $this->config['expire']);
    }

    /**
     * 删除Session
     * @access public
     * @param string $sessID
     */
    public function destroy($sessID)
    {
        return $this->handler->delete($this->config['session_name'] . $sessID);
    }

    /**
     * Session 垃圾回收
     * @access public
     * @param string $sessMaxLifeTime
     */
    public function gc($sessMaxLifeTime)
    {
        return true;
    }
}

结果


当时我看到结果,真的忍不住说句卧槽 这尼玛也太坑了

以前是太懒了,项目遇到坑从来不记录下来,都烂在心里,到最后烂到自己都忘记了掉了,最近要开始努力更新博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值