使用Tideways和Toolkit对PHP代码进行性能分析

21 篇文章 1 订阅

码字不易,转载请附原链,搬砖繁忙回复不及时见谅,技术交流请加QQ群:909211071

安装Tideways

git clone https://github.com/tideways/php-xhprof-extension.git
cd php-profiler-extension
phpize
./configure
make && make install


php.ini加入 extension=tideways.so

service php-fpm restart

安装Toolkit

go get github.com/tideways/toolkit

命令在go的bin目录下:
localhost:bin why$ pwd
/Users/why/Desktop/go/bin
localhost:bin why$ ll
total 10208
drwxr-xr-x   3 why  wheel       96 12  3 12:29 ./
drwxr-xr-x  14 why  wheel      448 11 27 23:27 ../
-rwxr-xr-x   1 why  staff  5226048 12  3 12:29 toolkit*



测试:
localhost:bin why$ ./toolkit
The Tideways Toolkit (tk) is a collection of commandline tools to interact with
PHP and perform various debugging, profiling and introspection jobs by
interacting with PHP or with debugging extensions for PHP.

Are you looking for a production profiler for your team with Web UI, SQL and
HTTP profiling, monitoring, exception tracking and more?

Start a Tideways Profiler 30 days trial @ https://tideways.io

Usage:
  tk [command]

Available Commands:
  analyze-callgrind             Parse the output of callgrind outputs into a sorted tabular output.
  analyze-xhprof                Parse the output of JSON serialized XHProf outputs into a sorted tabular output.
  compare-callgrind             Compare two callgrind outputs and display them in a sorted table.
  compare-xhprof                Compare two JSON serialized XHProf outputs and display them in a sorted table.
  generate-xhprof-diff-graphviz Parse the output of two JSON serialized XHProf outputs, and generate a dot script out of their diff.
  generate-xhprof-graphviz      Parse the output of JSON serialized XHProf outputs into a dot script for graphviz.
  help                          Help about any command

Flags:
  -h, --help   help for tk

Use "tk [command] --help" for more information about a command.
subcommand is required

PHP代码

<?php

namespace App\Http\Middlewares;

use Closure;

class TidewaysMiddleware
{
    public function handle($request, Closure $next)
    {
        $debug = env('APP_DEBUG') && extension_loaded('tideways_xhprof');

        if (! $debug) {
            return $next($request);
        }

         tideways_xhprof_enable(TIDEWAYS_XHPROF_FLAGS_CPU | TIDEWAYS_XHPROF_FLAGS_MEMORY);
}

        
        $begin = microtime(true);
        $result = $next($request);
        $end = microtime(true);

        if ($end - $begin > env('APP_MAX_RESPONSE_TIME', 1)) {
            $dir = storage_path('logs');
            $file = 'tideways.' . route_name() . '.log';
            $content = json_encode(tideways_xhprof_disable());

            file_put_contents($dir . '/' . $file, $content);
        }

        return $result;
    }
}

下边这个兼容不同版本的tideways

<?php

namespace App\Http\Middlewares;

use Closure;

class TidewaysMiddleware
{
    public function handle($request, Closure $next)
    {
        $debug = env('APP_DEBUG') && extension_loaded('tideways');

        if (! $debug) {
            return $next($request);
        }

        tideways_enable(TIDEWAYS_FLAGS_CPU | TIDEWAYS_FLAGS_MEMORY);
        
        $begin = microtime(true);
        $result = $next($request);
        $end = microtime(true);

        if ($end - $begin > env('APP_MAX_RESPONSE_TIME', 1)) {
            $dir = storage_path('logs');
            $file = 'tideways.' . route_name() . '.log';
            $content = json_encode(tideways_disable());

            file_put_contents($dir . '/' . $file, $content);
        }

        return $result;
    }
}

分析结果

[weihaoyu@iZ23u681ae1Z logs]$ toolkit analyze-xhprof ./tideways.question.store.log
Showing XHProf data by Exclusive Wall-Time
+-------------------------------+-------+-----------+-------------------------------+
|           FUNCTION            | COUNT | WALL-TIME | EXCL  WALL-TIME (>= 14 46 MS) |
+-------------------------------+-------+-----------+-------------------------------+
| PDOStatement::execute         |    28 | 144.64 ms | 144.64 ms                     |
| PDO::prepare                  |    28 | 136.89 ms | 136.89 ms                     |
| Composer\Autoload\includeFile |   119 | 56.43 ms  | 53.23 ms                      |
| fgets                         |    17 | 33.78 ms  | 33.78 ms                      |
| PDO::__construct              |     1 | 14.86 ms  | 14.86 ms                      |
+-------------------------------+-------+-----------+-------------------------------+
Looking for a Web UI and SQL Profiling Support? Try our SaaS: https://tideways.io

发现该接口的大部分时间都用在PDO绑定和执行上,所以考虑优化SQL

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AirGo.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值