marfatech/liveprof

此包已被废弃且不再维护。未建议替代包。

适用于运行在实时网站上的性能监控系统

v1.4.3 2023-04-07 12:57 UTC

This package is auto-updated.

Last update: 2024-03-07 14:47:39 UTC


README

logo

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

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

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

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

Build Status codecov Scrutinizer Code Quality GitHub license

系统要求

  • PHP 版本 5.4 或更高版本 / hhvm 版本 3.25.0 或更高版本
  • 以下之一用于分析和收集数据:XHProfUprofilerTideways。您可以使用其他符合以下格式的分析工具
    $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';

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

以下是如何在没有扩展和数据库的情况下测试 Live Profiler 的示例。您可以使用与 XHProf 和 liveprof.org 兼容的内置分析器作为 UI

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

\MarfaTech\LiveProfiler\LiveProfiler::getInstance()
     ->setMode(\MarfaTech\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
\MarfaTech\LiveProfiler\SimpleProfiler::getInstance()->startTimer(__METHOD__); // any string can be used as a timer tag
// stop the timer before the end of the method
\MarfaTech\LiveProfiler\SimpleProfiler::getInstance()->endTimer(__METHOD__); // any string can be used as a timer tag

以下是您可以使用的方法的完整列表,以更改选项

<?php

// Start profiling
\MarfaTech\LiveProfiler\LiveProfiler::getInstance()
    ->setMode(\MarfaTech\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 \MarfaTech\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 = \MarfaTech\LiveProfiler\LiveProfiler::getInstance()->getLabel();
\MarfaTech\LiveProfiler\LiveProfiler::getInstance()->setLabel($current_label . $number);

如果您不想保存分析结果,您可以在任何时候重置它

<?php

\MarfaTech\LiveProfiler\LiveProfiler::getInstance()->reset();

脚本结束后,它将在关闭时显式调用 \MarfaTech\LiveProfiler\LiveProfiler::getInstance()->end();,但您可以在工作代码后显式调用它。

环境变量

LIVE_PROFILER_CONNECTION_URL:数据库连接的 URL

LIVE_PROFILER_PATH:在 \MarfaTech\LiveProfiler\LiveProfiler::MODE_FILES 模式下保存配置文件的路径

LIVE_PROFILER_API_URL:在 \MarfaTech\LiveProfiler\LiveProfiler::MODE_API 模式下发送配置文件的 API URL,并可在 liveprof.org 上查看示例

工作流程

实时分析器允许以自定义频率(例如每 1000 个请求中的 1 个)运行分析,并按应用名称(默认为 'Default')和自定义标签(默认为 URL 路径或脚本名称)分组。

正确计算 请求除数 很重要,以便为聚合提供足够的数据。您应将每日请求量除以每分钟大约一个配置文件。例如,如果您每天的页面 /users 有 1M 个请求,除数应为 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 开源许可证。