当前位置:首页 > PHP > 正文内容

php使用register_tick_function来定位执行慢的代码

高老师5年前 (2021-01-28)PHP1461

同事在优化页面加载慢的问题中将数据分离为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慢日志即可快速定位。

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/194.html

分享给朋友:

“php使用register_tick_function来定位执行慢的代码” 的相关文章

PHP函数赋值给一个变量的另类用法

PHP函数赋值给一个变量的另类用法

<?php   function  go($str) { echo'I\'m '.$str;  }   $goto='go';   $goto('gaojiufeng'...

PHP生成缩略图

PHP生成缩略图

//参数1  文件名    参数2  缩放比例  function   _thumb($_filename,$_percent){    ob_clean();...

 php xml字符串转数组,phpxml转数组,php 将xml转换成数组

php xml字符串转数组,phpxml转数组,php 将xml转换成数组

001源码:/*  * $xml_str是xml字符串  */ function  xmltoarray($xml_str) { //禁止XML实体扩展攻击 libxml_disable_entity_loader(true); //拒绝包含...

php数组合并 array_merge和+号的区别

php数组合并 array_merge和+号的区别

array_merge是最常用的数组合并方法,+号同样也可以,但是却有很大不同。array_merge遇到相同字符串key,后面数组的key会覆盖前面数组的key,+号正好相反。$a = [ 'one' => 'A on...

xmlrpc  php,php通过xml-rpc进行通信

xmlrpc php,php通过xml-rpc进行通信

xmlrpc协议是通过http请求xml数据进行通信。webservice中和它相同的是soap。soap调用的确很简单,但是创建wsdl太繁琐,效率低下。xmlrpc很好的解决这个问题。(1).创建xmlrpc服务端(求和函数api)function getSum($method,$ar...

php定义常量数组

php定义常量数组

<?php //php7+ define('CONFIG', [     'MYSQL' => '127.0.0.1',     ...