badoo/ liveprof
适用于运行在实时网站上的性能监控系统
Requires
- php: >=5.4
- ext-curl: *
- ext-json: *
- ext-zlib: *
- doctrine/dbal: ~2.0
- psr/log: ~1.0
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 ->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_PATH
:在 \Badoo\LiveProfiler\LiveProfiler::MODE_FILES 模式下保存配置文件的路径
LIVE_PROFILER_API_URL
:在 \Badoo\LiveProfiler\LiveProfiler::MODE_API 模式下发送配置文件的 API url,并在 liveprof.org 上查看示例
工作流程
实时分析器允许以自定义频率(例如每1000个请求中的1个)运行分析,按应用程序名称(默认为'Default')和自定义标签(默认为 URL 路径或脚本名称)分组。
正确计算 请求除数 非常重要,以便有足够的数据进行汇总。您应该将每日请求数量除以每天大约有一个配置文件,例如,如果您每天有 1M 个对 /users 页面的请求,则除数应为 1000000/(60*24) = 694,因此 700 的除数就足够了。
此外,您还必须计算所有请求分析的 总除数。这对于控制整个系统健康非常重要。它可以像特别请求的除数计算一样计算,但在这种情况下,您应使用每日总请求数。例如,如果您每天有 10M 个请求 - 总除数=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
如果您的服务器有 PHP 版本 7.0 或更高,则最好使用 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 开源许可证的许可。