DEV Community

菜皮日记
菜皮日记

Posted on

使用基于 tideways 的 php-monitor 搭建 PHP 性能监控平台

tideways、xhprof、xhgui 介绍

tideways 是一家提供 PHP 应用性能监控相关服务的公司,最主要的产品是 tideways PHP 性能监控扩展,tideways 扩展能够监控请求耗时,SQL 执行耗时,CPU 和内存占用。监控数据存储支持MySQL 和 MongoDB。

提到 PHP 性能监控,不得不提性能监控工具 xhprof。xhprof 是 facebook 开发并开源的 PHP 性能监控扩展,配合 xhgui 做数据的可视化。但由于 facebook 后期将自家 PHP 替换成 HHVM,xhprof 也不再维护了。之后 tideways 演化出了两个分支,一个是原本的 tideways 扩展,一个是继承自xhprof 的 tideways_xhprof 扩展,tideways_xhprof 内的功能与 xhprof 一致。在演化成两个分支后,原有的 tideways 逐渐演化成了付费的 SaaS 服务,而 tideways_xhprof 兼容了 xhprof 的一些使用习惯,并会一直维护。

产生分支前的最后一个版本:https://github.com/tideways/php-xhprof-extension/tree/v4.1.7

产生分支后的第一个版本:https://github.com/tideways/php-xhprof-extension/tree/v5.0-beta1

php-monitor 介绍

在 xhprof 不维护之后,其对应的数据可视化 xhgui 也就随之不再更新。php-monitor 的作者最开始是为了做一版汉化的 xhgui:xhgui-branch。之后作者又结合 tideways 扩展做了 php-monitor,一个 PHP 性能监控平台,同时支持监控数据收集和数据可视化展示。

配置过程

tideways 扩展

先安装 tideways 扩展。如果使用 PHP 5.6,请下载 tideways v4.1.5。如果使用 PHP7+ ,请下载v4.1.7(更高的版本无法显示SQL)。

wget --no-check-certificate https://github.com/tideways/php-xhprof-extension/archive/v4.1.7.tar.gz && tar zxvf v4.1.7.tar.gz && cd php-xhprof-extension-4.1.7 && phpize && ./configure && make && sudo make install
Enter fullscreen mode Exit fullscreen mode

项目中引入 php-monitor

首先要明确,php-monitor 包含两部分,数据收集和数据可视化。数据收集需要将 php-monitor 整合进项目中,数据可视化部分则单独部署。这两块的数据部分互通即可。

之后尝试在 swoole 项目中引入 php-monitor,首先发现 php-monitor 没有发布到 packagist,所以只能本地安装。

监控数据存储

数据存储使用 MongoDB,方便快捷。

项目是在本地 docker-compose 下起来的,在 docker-compose.yml 中创建一个 mongo 容器并向外暴露 mongo 端口27017。同时 php-monitor 的数据可视化部分也可以在 mongo 中读取监控数据。

docker-compose.yml 声明 mongo

my-mongodb:
container_name: my-mongodb
image: mongo:4.0.3
restart: always
volumes:
  - ./mongodata/db:/data/db
  - ./mongodata/log:/var/log/mongodb
ports:
  - 27017:27017
networks:
  - my-networks
environment:
  MONGO_INITDB_ROOT_USERNAME:
  MONGO_INITDB_ROOT_PASSWORD:
Enter fullscreen mode Exit fullscreen mode

在 php-monitor/src/config/config.php 中配置使用 mongo,在 swoole 处理请求的函数中开启监控,并在返回后记录数据

public function onRequest(SwooleRequest $request, SwooleResponse $response)
{
    require_once '/working/vendor/laynefyc/php-monitor/src/autoPrepend.php';
    // 处理
    \pm\common\PMonitor::shutdown($uri, '127.0.0.1','GET');
}
Enter fullscreen mode Exit fullscreen mode

在 mongo 中可以看到监控数据:

!https://deb-gmi.oss-cn-beijing.aliyuncs.com/img/20210326121446.png

其中 profile 字段是具体的调用栈情况,a ==> b 表示从 a 函数到 b 函数的过程

  • ct:调用的次数
  • wt:函数方法执行的时间耗时。相当于,在调用前记录一个时间,函数方法调用完毕后,计算时间差。
  • cpu:函数方法执行消耗的 cpu 时间。和 wt 的差别在于,当进程让出 cpu 使用权后,将不再计算 cpu 时间。通过调用系统调用 getrusage 获取进程的占用 cpu 数据。
  • mu:函数方法所使用的内存。相当于,在调用前记录一个内存占用,函数方法调用完毕后,计算内存差。调用的是 zend_memory_usage 获取内存占用情况。
  • pmu:函数方法所使用的内存峰值。调用的是 zend_memory_peak_usage 获取内存情况。

数据可视化

使用同样的 mongo 配置,在 php-monitor/public 中启动服务 php -S 127.0.0.1:8066

浏览器访问:

按url查询耗时:

查看具体一次请求的 SQL 耗时详情:

函数的耗时和内存占用:

函数调用次数和总耗时:

性能指标监控的原理

使用 tideways 扩展提供的 tideways_enable 开启监控,tideways_disable 结束监控。可以监控到这两个函数之间的所有执行过程。这一点跟 xhprof一致,也是 xhprof_enable 开启,xhprof_disable 结束。

xhprof扩展:

// 开启监控
xhprof_enable();

// 业务逻辑

// 结束监控
$xhprofData = xhprof_disable();
Enter fullscreen mode Exit fullscreen mode

tideways扩展:

// 开启监控并支持CPU和内存监控
tideways_enable(TIDEWAYS_FLAGS_CPU | TIDEWAYS_FLAGS_MEMORY);
// 开启sql监控
tideways_span_create('sql');

// 业务逻辑

// 结束监控
$monitorData = tideways_disable();
// sql监控
$sqlData = tideways_get_spans();
Enter fullscreen mode Exit fullscreen mode

tideways_xhprof扩展:

// 开启监控并支持CPU和内存监控
tideways_xhprof_enable(TIDEWAYS_XHPROF_FLAGS_MEMORY | TIDEWAYS_XHPROF_FLAGS_CPU);

// 业务逻辑

// 结束监控
$monitorData = tideways_xhprof_disable();
Enter fullscreen mode Exit fullscreen mode

参考引用:

xhgui:https://github.com/laynefyc/xhgui-branch

php-monitor:https://github.com/laynefyc/php-monitor/blob/master/README-zh_CN.md

作者博文《Tideways和xhgui打造PHP非侵入式监控平台》:https://blog.it2048.cn/article-tideways-xhgui/

tideways github:https://github.com/tideways/php-xhprof-extension/

tideways官网对tideways扩展的接收及数据格式说明:https://support.tideways.com/documentation/reference/tideways-xhprof/tideways-xhprof-extension.html

《Swoole整合PHP性能分析平台: Tideways+Xhgui》:https://blog.csdn.net/uisoul/article/details/103936873

《composer如何安装本地依赖包》:https://learnku.com/laravel/t/1999/composer-local-path-loading-third-party-extension-pack

《深入了解xhprof性能分析工具》:https://www.bo56.com/深入了解xhprof性能分析工具/

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay