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

安装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

发布了200 篇原创文章 · 获赞 26 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/why444216978/article/details/103365063
今日推荐