同事在优化页面加载慢的问题中将数据分离为2个接口来分别异步加载,但是实际的情况是接口1一直在查询,接口2也一直在查询,接口2一定要等接口1查询完成才能返回结果。一直以为fast-cgi进程数问题,反复设置无效,通过getmypid就能看到每次相应的进程id不同。正好我有空就想到通过register_tick_function来判断到底是卡在哪一行,一旦注册register_tick_function,php执行一行代码都会调用注册的处理函数,通过计算上一次代码执行时间和本次执行时间即可定位位置。
<?php
/**
* Class Tracker
*/
class Tracker
{
/**
* 执行代码时间(单位毫秒)
* @var int
*/
protected $eval_code_time = 0;
/**
* 执行代码过慢时间(单位毫秒)
* @var int
*/
protected $slow_code_time = 200;
/**
* 获取当前毫秒时间戳
* millisecondTime
* @return float|int
*/
protected function millisecondTime()
{
return microtime(true) * 1000;
}
/**
* setSlowCodeTime
* @param $time
* @return Tracker
* @throws Exception
*/
public function setSlowCodeTime($time)
{
if ($time <= 0) {
throw new \Exception('执行代码过慢时间必须大于0');
}
$this->slow_code_time = $time;
return $this;
}
/**
* startCollect
* @throws Exception
*/
public function startCollect()
{
declare (ticks = 1);
if (!register_tick_function([$this, 'handle'], true)) {
throw new \Exception('注册tracker处理函数失败');
}
}
/**
* handle
*/
public function handle()
{
$millisecondTime = $this->millisecondTime();
if (!$this->eval_code_time) {
$this->eval_code_time = $millisecondTime;
}
if (($millisecondTime - $this->eval_code_time) > $this->slow_code_time) {
$e = new Exception();
var_dump($e->getTrace());die();
} else {
$this->eval_code_time = $millisecondTime;
}
}
}
//查询执行超过1秒的代码位置
$object = new Tracker();
$object->setSlowCodeTime(1)->startCollect();
sleep(1);最终定位到的问题是同事本机开发环境session是保存在文件中的,存在session锁的问题,上面的简单粘贴下代码。
可能存在的问题,如果代码执行直接卡死进程关闭是无法定位到的,由于是windows,否则可以开启fpm慢日志即可快速定位。
上家公司开发医院挂号系统,系统采用GBK编码。ajax发送的中文用户名让PHP保存为cookie出现乱码的解决方案。1.Javascript变量var user=document.getElementById('user').innerText; user=escape(u...
function objtoarr($obj){ $ret = array(); foreach($obj as $key =>$value){ if(gettype($value) == 'arr...
应用场景:PHP模拟购买,商品数量大于0才能购买常见代码:<?php //连接数据库 $con=mysqli_connect("localhost","ihuohuo","927464cy","ihuohuo");...
面试中PHP面试官会问调用一个不存在的方法,如何知道是哪个文件哪行调用的?假设方法是getWorkLoad()回答1:开启PHP错误输出,PHP会输出Fatal error: Call to undefined function getWorkLoad() in D:\wwwroot\thinkpa...
(1).config.php 配置文件<?php /** * RabbitMQ_Config */ $config = [ 'host' => ...
参数中包含gb2312的字符串,返回结果是false或者null(不同PHP版本具有差异性)代码:<?php $dbms = 'mysql'; $host = '192.168.8.8'; $dbName =&n...