了解 yii的生命周期,框架响应时长

响应结果:

Yii请求类实例化开始时间戳:109070854,结束时间戳:109070854,共计用时:0
核心组件、设置时区、设置框架路径、实例化开始时间戳:109070854,结束时间戳:109070855,共计用时:1
定义错误处理程序开始时间戳:109070855,结束时间戳:109070855,共计用时:0
初始化组件和拓展开始时间戳:109070855,结束时间戳:109070856,共计用时:1
实例化控制器开始时间戳:109070856,结束时间戳:109070928,共计用时:72
action运行开始时间戳:109070928,结束时间戳:109071014,共计用时:86
接口请求始时间戳:109070856,结束时间戳:109071014,共计用时:158
{"success":true,"result":{"code":200,"result":{"have_more":true,"list":[{"id":"KETQTNOV","article_id":"00KETQQ2Z5JXZZU3ED6KYYPK","author_id":"00K7PSUV4PEZJLXD9XI1TAJQ","summary":"TCP/IP入门经典 chapter6 传输层\n问题驱动TCP 协议是不限制一个特定的连接(两端 socke","is_auto_summary":2,"created_at":1599556807,"author":{"id":"00K7PSUV4PEZJLXD9XI1TAJQ","avatar":"0e09f4a6e1915e881363bf03e9459434.png","nickname":"南方小菜"}}],"code":2027}}}
接口请求始时间戳:结束时间戳:109071014

1.修改 项目根目录下 \vendor\yiisoft\yii2\base\Application.php __construct方法

public function __construct($config = [])
    {
        Yii::$app = $this;

        $instanceStart = self::getMillisecond();

        static::setInstance($this);

        $instanceEnd = self::getMillisecond();
        $instanceTotal = $componentEnd - $componentStart;
        echo "Yii请求类实例化开始时间戳:{$instanceStart},结束时间戳:{$instanceEnd},共计用时:{$instanceTotal}\n";


        $this->state = self::STATE_BEGIN;

        $preInitStart = self::getMillisecond();

        $this->preInit($config);

        $preInitEnd = self::getMillisecond();
        $preInitTotal = $preInitEnd - $preInitStart;
        echo "核心组件、设置时区、设置框架路径、实例化开始时间戳:{$preInitStart},结束时间戳:{$preInitEnd},共计用时:{$preInitTotal}\n";

        $registerErrorStart = self::getMillisecond();

        $this->registerErrorHandler($config);

        $registerErrorEnd = self::getMillisecond();
        $registerErrorTotal = $registerErrorEnd - $registerErrorStart;
        echo "定义错误处理程序开始时间戳:{$registerErrorStart},结束时间戳:{$registerErrorEnd},共计用时:{$registerErrorTotal}\n";

        $componentStart = self::getMillisecond();

        Component::__construct($config);

        $componentEnd = self::getMillisecond();
        $componenTotal = $componentEnd - $componentStart;
        echo "初始化组件和拓展开始时间戳:{$componentStart},结束时间戳:{$componentEnd},共计用时:{$componenTotal}\n";
    }

2.修改 项目根目录下 vendor\yiisoft\yii2\base\Module.php

public function runAction($route, $params = [])
    {
        $controllerStart = Application::getMillisecond();
        $parts = $this->createController($route);
        if (is_array($parts)) {
            /* @var $controller Controller */
            list($controller, $actionID) = $parts;
            $oldController = Yii::$app->controller;
            Yii::$app->controller = $controller;
            $controllerEnd = Application::getMillisecond();
            $controllerTotal = $controllerEnd - $controllerStart;
            echo "实例化控制器开始时间戳:{$controllerStart},结束时间戳:{$controllerEnd},共计用时:{$controllerTotal}\n";

            $actionStart = Application::getMillisecond();
            $result = $controller->runAction($actionID, $params);
            $actionEnd = Application::getMillisecond();
            $actionTotal = $actionEnd - $actionStart;
            echo "action运行开始时间戳:{$actionStart},结束时间戳:{$actionEnd},共计用时:{$actionTotal}\n";
            if ($oldController !== null) {
                Yii::$app->controller = $oldController;
            }

            return $result;
        }

        $id = $this->getUniqueId();
        throw new InvalidRouteException('Unable to resolve the request "' . ($id === '' ? $route : $id . '/' . $route) . '".');
    }

3.修改 项目根目录下 \vendor\yiisoft\yii2\base\Application.php run()方法

public function run()
    {
        try {
            $this->state = self::STATE_BEFORE_REQUEST;
            $this->trigger(self::EVENT_BEFORE_REQUEST);

            $requestStart = self::getMillisecond();

            $this->state = self::STATE_HANDLING_REQUEST;
            $response = $this->handleRequest($this->getRequest());

            $requestEnd = self::getMillisecond();
            $requestTotal = $requestEnd - $requestStart;
            echo "接口请求始时间戳:{$requestStart},结束时间戳:{$requestEnd},共计用时:{$requestTotal}\n";

            $this->state = self::STATE_AFTER_REQUEST;
            $this->trigger(self::EVENT_AFTER_REQUEST);

            $this->state = self::STATE_SENDING_RESPONSE;
            $response->send();
            $responseEnd = self::getMillisecond();
            echo "接口请求始时间戳:结束时间戳:{$responseEnd}\n";

            $this->state = self::STATE_END;

            return $response->exitStatus;
        } catch (ExitException $e) {
            $this->end($e->statusCode, isset($response) ? $response : null);
            return $e->statusCode;
        }
    }

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小猪@笨笨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值