magdv / liveprof
适用于实际网站的运行性能监控系统
Requires
- php: >=8.0
- ext-curl: *
- ext-json: *
- ext-zlib: *
- doctrine/dbal: ~3.0
- psr/log: ^2
Requires (Dev)
- phpunit/phpunit: ~4.0|~5.0
README
Live Profiler是Badoo使用的基于XHProf或其分支(Uprofiler或Tideways)的系统级性能监控系统。Live Profiler通过在XHProf下运行页面请求样本,持续从生产层收集函数级分析数据。
Live profiler UI根据时间、页面类型等维度汇总与单个请求对应的分析数据,有助于回答各种问题,例如:特定页面的函数级分析是什么?函数“foo”在所有页面或特定页面上的开销是多少?过去一天/一周/一个月中哪些函数退步最大?页面/函数的执行时间历史趋势是什么?等等。
这里有一个PhpStorm插件,可以直接在IDE中查看方法性能。
liveprof.org显示所有功能,可用于测试目的。
系统要求
- PHP版本5.4或更高版本 / hhvm版本3.25.0或更高版本
- 其中一个XHProf、Uprofiler或Tideways来分析和收集数据。您可以使用返回以下格式的其他分析器
$data = [ [ 'parent_method==>child_method' => [ 'param' => 'value' ] ] ];
- 数据库扩展将分析结果保存到数据库。
安装
- 您可以通过Composer安装Live Profiler
php composer.phar require badoo/liveprof
- 根据模式准备结果存储
[保存数据到数据库]如果您使用DB模式,则需要准备一个数据库服务器。您可以使用此处描述的任何驱动程序或实现自定义的驱动程序。您需要运行一个脚本来配置数据库。此脚本创建“details”表
LIVE_PROFILER_CONNECTION_URL=mysql://db_user:db_password@db_mysql:3306/Profiler?charset=utf8 php vendor/badoo/liveprof/bin/install.php
[保存数据到文件]也可以将分析结果保存到文件中。为此,准备一个具有写入权限的目录。
[将数据发送到演示网站]您需要访问liveprof.org,登录并复制API密钥。
- 在项目入口点(通常是public/index.php)工作代码之前初始化分析器。
使用方法
这里有一个使用默认参数使用分析器的示例
<?php include 'vendor/autoload.php'; \Badoo\LiveProfiler\LiveProfiler::getInstance()->start(); // Code is here
这里有一个在没有扩展和数据库的情况下测试Live Profiler的示例。您可以使用与XHProf兼容的内置分析器以及liveprof.org作为UI
<?php include 'vendor/autoload.php'; \Badoo\LiveProfiler\LiveProfiler::getInstance() ->setMode(\Badoo\LiveProfiler\LiveProfiler::MODE_API) ->setApiKey('70366397-97d6-41be-a83c-e9e649c824e1') // a key for guest ->useSimpleProfiler() // Use build-in profiler instead of XHProf or its forks ->setApp('Demo') // Some unique app name ->start(); // Code is here // start a timer before each inportant method \Badoo\LiveProfiler\SimpleProfiler::getInstance()->startTimer(__METHOD__); // any string can be used as a timer tag // stop the timer before the end of the method \Badoo\LiveProfiler\SimpleProfiler::getInstance()->endTimer(__METHOD__); // any string can be used as a timer tag
这里有您可以用来更改选项的完整方法列表
<?php // Start profiling \Badoo\LiveProfiler\LiveProfiler::getInstance() ->setMode(\Badoo\LiveProfiler\LiveProfiler::MODE_DB) // optional, MODE_DB - save profiles to db, MODE_FILES - save profiles to files, MODE_API - send profiles to http://liveprof.org/ ->setConnectionString('mysql://db_user:db_password@db_mysql:3306/Profiler?charset=utf8') // optional, you can also set the connection url in the environment variable LIVE_PROFILER_CONNECTION_URL ->setPath('/app/data/') // optional, path to save profiles, you can also set the file path in the environment variable LIVE_PROFILER_PATH ->setApiKey('api_key') // optional, api key to send profiles and see demo, you can get it on http://liveprof.org/ ->setApp('Site1') // optional, current app name to use one profiler in several apps, "Default" by default ->setLabel('users') // optional, the request name, by default the url path or script name in cli ->setDivider(700) // optional, profiling starts for 1 of 700 requests with the same app and label, 1000 by default ->setTotalDivider(7000) // optional, profiling starts for 1 of 7000 requests with forces label "All", 10000 by default ->setLogger($Logger) // optional, a custom logger implemented \Psr\Log\LoggerInterface ->setConnection($Connection) // optional, a custom instance of \Doctrine\DBAL\Connection if you can't use the connection url ->setDbTableName("liveproof") // optional, a custom db name, you can also set set table name int the env var LIVE_PROFILER_DB_TABLE_NAME ->setDataPacker($DatePacker) // optional, a class implemented \Badoo\LiveProfiler\DataPackerInterface to convert array into string ->setStartCallback($profiler_start_callback) // optional, set it if you use custom profiler ->setEndCallback($profiler_profiler_callback) // optional, set it if you use custom profiler ->useXhprof() // optional, force use xhprof as profiler ->useTidyWays() // optional, force use TidyWays as profiler ->useUprofiler() // optional, force use uprofiler as profiler ->useSimpleProfiler() // optional, force use internal profiler ->useXhprofSample() // optional, force use xhprof in sampling mode ->start();
如果您想在运行期间更改标签(例如,在您在路由器或控制器中获得一些信息后)可以调用
<?php $number = random_int(0, 100); $current_label = \Badoo\LiveProfiler\LiveProfiler::getInstance()->getLabel(); \Badoo\LiveProfiler\LiveProfiler::getInstance()->setLabel($current_label . $number);
如果您不想保存分析结果,可以随时重置它
<?php \Badoo\LiveProfiler\LiveProfiler::getInstance()->reset();
在脚本结束时,它将在关闭时调用\Badoo\LiveProfiler\LiveProfiler::getInstance()->end();
,但您可以在工作代码后显式调用它。
环境变量
LIVE_PROFILER_CONNECTION_URL
:数据库连接url
LIVE_PROFILER_DB_TABLE_NAME
:存储日志的数据库表名
LIVE_PROFILER_PATH
:在 \Badoo\LiveProfiler\LiveProfiler::MODE_FILES 模式下保存配置文件的路径
LIVE_PROFILER_API_URL
:在 \Badoo\LiveProfiler\LiveProfiler::MODE_API 模式下发送配置文件的 API URL,并查看 liveprof.org 上的演示
工作流程
实时分析器允许以自定义频率(例如每1000次请求中的1次)运行分析,按应用名称(默认为'Default')和自定义标签(默认为 URL 路径或脚本名称)分组。
正确计算 请求分隔符 非常重要,以便为聚合提供足够的数据。您应该将每日请求数量除以大约每分钟一个配置文件。例如,如果您每天的页面 /users 有100万次请求,则分隔符应为 1000000/(60*24) = 694,因此分隔符 = 700 就足够了。
您还需要计算所有请求分析的 总分隔符。这有助于控制整个系统的健康。它可以通过与特定请求分隔符相同的方式进行计算,但在此情况下,您应使用每日总请求数量。例如,如果您每天有1000万次请求,则总分隔符 = 10000000/(60*24) = 6940,因此总分隔符 = 7000 就足够了。
分析器会自动检测您拥有的分析器扩展(xhprof、uprofiler 或 tidyways)。如果您使用其他分析器,则需要设置分析器回调。
您可以使用具有 xhprof 扩展的 docker 容器运行测试脚本,并在 Dockerfile 中查看服务器配置示例
docker build -t badoo/liveprof .
docker run badoo/liveprof
或者,您可以使用采样构建带有 xhprof 的 docker 容器
docker build -f DockerfileSamples -t badoo/liveprof .
docker run badoo/liveprof
或者,您可以使用 tideways 扩展构建 docker 容器
docker build -f DockerfileTidyWays -t badoo/liveprof .
docker run badoo/liveprof
或者 uprofiler 扩展
docker build -f DockerfileUprofiler -t badoo/liveprof .
docker run badoo/liveprof
或者带有内置 xhprof 扩展的最新 hhvm
docker build -f DockerfileHHVM -t badoo/liveprof .
docker run badoo/liveprof
或者如果您想使用内置 xhprof 扩展的 API
docker build -f DockerfileUseApi -t badoo/liveprof .
docker run badoo/liveprof
如果您的服务器上安装了 7.0 或更高版本的 PHP,则最好使用 Tideways 作为分析器。
安装 tideways 扩展的步骤
git clone https://github.com/tideways/php-profiler-extension.git cd php-profiler-extension phpize ./configure make make install echo "extension=tideways_xhprof.so" >> /usr/local/etc/php/conf.d/20-tideways_xhprof.ini echo "xhprof.output_dir='/tmp/xhprof'" >> /usr/local/etc/php/conf.d/20-tideways_xhprof.ini
安装 uprofiler 的步骤
git clone https://github.com/FriendsOfPHP/uprofiler.git cd uprofiler/extension/ phpize ./configure make make install echo "extension=uprofiler.so" >> /usr/local/etc/php/conf.d/20-uprofiler.ini echo "uprofiler.output_dir='/tmp/uprofiler'" >> /usr/local/etc/php/conf.d/20-uprofiler.ini
测试
使用开发需求安装 Live Profiler
php composer.phar require --dev badoo/liveprof
在项目目录中,运行
vendor/bin/phpunit
许可证
本项目的许可证为 MIT 开源许可证。