最近在公司項目裏發現,一個頁面加載非常緩慢,打開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 <[email protected]>
// +----------------------------------------------------------------------
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;
}
}
結果
當時我看到結果,真的忍不住說句臥槽 這尼瑪也太坑了
以前是太懶了,項目遇到坑從來不記錄下來,都爛在心裏,到最後爛到自己都忘記了掉了,最近要開始努力更新博客