tungatarov-kl / liveprof
适用于运行在实时网站上的性能监控系统
Requires
- php: >=8.0
- ext-curl: *
- ext-json: *
- ext-zlib: *
- doctrine/dbal: ~2.0|~3.0
- psr/log: ~1.0|~2.0|~3.0
Requires (Dev)
- phpunit/phpunit: ~4.0|~5.0
This package is auto-updated.
Last update: 2024-09-05 12:50:19 UTC
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'; \TungatarovKl\LiveProfiler\LiveProfiler::getInstance()->start(); // Code is here
这是一个在没有扩展和数据库的情况下测试 Live Profiler 的示例。您可以使用与 XHProf 兼容的内置分析器以及 liveprof.org 作为 UI
<?php include 'vendor/autoload.php'; \TungatarovKl\LiveProfiler\LiveProfiler::getInstance() ->setMode(\TungatarovKl\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 \TungatarovKl\LiveProfiler\SimpleProfiler::getInstance()->startTimer(__METHOD__); // any string can be used as a timer tag // stop the timer before the end of the method \TungatarovKl\LiveProfiler\SimpleProfiler::getInstance()->endTimer(__METHOD__); // any string can be used as a timer tag
这是一个您可以用来更改选项的方法完整列表
<?php // Start profiling \TungatarovKl\LiveProfiler\LiveProfiler::getInstance() ->setMode(\TungatarovKl\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 \TungatarovKl\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 = \TungatarovKl\LiveProfiler\LiveProfiler::getInstance()->getLabel(); \TungatarovKl\LiveProfiler\LiveProfiler::getInstance()->setLabel($current_label . $number);
如果您不想保存分析结果,您可以在任何时候重置它
<?php \TungatarovKl\LiveProfiler\LiveProfiler::getInstance()->reset();
脚本执行完毕后,将在关闭时调用 \TungatarovKl\LiveProfiler\LiveProfiler::getInstance()->end();
,但您也可以在运行代码后显式调用它。
环境变量
LIVE_PROFILER_CONNECTION_URL
:数据库连接的 url
LIVE_PROFILER_PATH
:在 \TungatarovKl\LiveProfiler\LiveProfiler::MODE_FILES 模式下保存配置文件的路径
LIVE_PROFILER_API_URL
:在 \TungatarovKl\LiveProfiler\LiveProfiler::MODE_API 模式下发送配置文件的 api url,并查看 liveprof.org 上的示例
工作流程
Live profiler 允许以自定义频率(例如 1000 个请求中的 1 个)进行性能分析,按应用名称(默认为 'Default')和自定义标签(默认为 url 路径或脚本名称)分组。
正确计算 请求除数 是很重要的,以确保有足够的数据进行聚合。您应该将每日请求量除以,以每分钟大约生成一个配置文件。例如,如果您每天有 100 万个对 /users 页面的请求,除数应为 1000000/(60*24) = 694,因此除数 700 已足够。
您还需要计算所有请求性能分析的总 除数。这对于控制整个系统健康是很重要的。它可以像特别请求的除数计算一样计算,但在此情况下,您应使用每日请求的总数。例如,如果您每天有 1000 万个请求,total_divider=10000000/(60*24) = 6940,因此 total_divider = 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 开源许可证。