fonclub/liveprof

适用于在线网站的性能监控系统

dev-dev 2023-02-10 08:40 UTC

This package is auto-updated.

Last update: 2024-09-10 12:03:16 UTC


README

logo

Live Profiler 是一个基于 XHProf 或其分支(如 Uprofiler 或 Tideways)的系统级性能监控系统,在 Badoo 上使用,它是构建在 XHProf 或其分支之上的。Live Profiler 通过在 XHProf 下运行页面请求的样本,持续从生产层收集函数级别的分析数据。

Live Profiler UI 然后根据时间、页面类型等维度汇总与单个请求对应的分析数据,可以帮助回答各种问题,例如:特定页面的函数级分析是什么?函数“foo”在所有页面或特定页面上的开销有多大?在过去的一天/周/月中,哪些函数退步最大?页面/函数的执行时间历史趋势是什么?等等。

这里有 一个用于 PhpStorm 的插件,可以直接在 IDE 中查看方法性能。

liveprof.org 展示了所有功能,并可用于测试目的。

Build Status codecov Scrutinizer Code Quality GitHub license

系统要求

  • PHP 版本 5.4 或更高 / hhvm 版本 3.25.0 或更高
  • 以下之一用于分析并收集数据:XHProf、Uprofiler 或 Tideways。您可以使用其他以以下格式返回数据的分析器
    $data = [
        [
            'parent_method==>child_method' => [
                'param' => 'value' 
            ]
        ]  
    ];
  • 数据库扩展将分析结果保存到数据库中。

安装

  1. 您可以通过 Composer 安装 Live Profiler
php composer.phar require badoo/liveprof
  1. 根据模式准备结果存储

[保存数据到数据库] 如果您使用 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 密钥。

  1. 在项目入口点(通常是 public/index.php)中初始化分析器,然后开始工作代码。

使用方法

这里有使用默认参数使用分析器的一个示例

<?php
include 'vendor/autoload.php';

\Badoo\LiveProfiler\LiveProfiler::getInstance()->start();
// Code is here

这里有测试 Live Profiler 而不安装任何扩展和数据库的示例。您可以使用与 XHProf 兼容并作为 UI 的内置分析器 liveprof.org

<?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路径或脚本名称)分组。

正确计算 请求除数 是很重要的,以便有足够的数据进行聚合。你应该将每日请求量除以得到每分钟大约有一个配置。例如,如果你每天有100万个请求,对于页面/users,除数应该是1000000/(60*24) = 694,所以除数=700就足够了。

同时,你还需要计算所有请求分析的总 除数。这对于控制整个系统健康非常重要。它可以像特别请求的除数计算一样计算,但在这种情况下,你应该使用每日所有请求的数量。例如,如果你每天有1000万个请求,则total_divider=10000000/(60*24) = 6940,所以total_divider = 7000就足够了。

分析器会自动检测你有哪些分析器扩展(xhprof、uprofiler或tideways)。如果你使用其他分析器,你必须设置分析器回调。

你可以在带有 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 开源许可证。