feat(stack): 所有 Docker 模式集成 XHProf 性能分析

在 docker-dev/docker-dev-sync/docker-serve/full 四个模式中集成 XHProf
性能分析能力,默认不启用(--profile xhprof 按需开启),零侵入不改
业务代码和 run.sh。

核心组件:
- source/docker/xhprof-bootstrap.php: 采集器初始化(auto_prepend_file)
- source/docker/xhgui-config.php: XHGui 认证配置(Basic Auth)+ PDO 存储
- 各 Dockerfile: 安装 PECL xhprof 扩展 + php-profiler 到 /opt/xhprof/
- 各 docker-compose: 新增 xhgui 服务(profiles: xhprof),端口 8142

存储方案:PDO MySQL(复用现有 MySQL),避免 MongoDB PHP 驱动兼容性问题。
docker-dev/docker-dev-sync 开箱即用;docker-serve/full 需配置 XHGUI_PDO_DSN。

认证:HTTP Basic Auth,默认 admin/xhgui123,环境变量可覆盖。
采集控制:XHPROF_ENABLE 环境变量 + cookie _profiler 强制触发 + 1% 采样。
Dockerfile.base 同步新增 xhprof 扩展(框架作者需重建推送基础镜像)。
This commit is contained in:
augushong
2026-08-09 09:07:10 +08:00
parent af0988118a
commit e0e2580646
12 changed files with 319 additions and 4 deletions

View File

@@ -27,6 +27,7 @@ RUN install-php-extensions imagick
RUN install-php-extensions zip
RUN install-php-extensions pcntl
RUN install-php-extensions xdebug
RUN install-php-extensions xhprof
# Clean default Nginx config
RUN rm /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
@@ -62,6 +63,17 @@ RUN rm -rf /var/www/html/source/docker && ln -sf /var/www/html/source/stack/dock
# Generate autoloader (without --no-dev optimizations)
RUN composer dump-autoload
# XHProf: 安装 php-profiler 到独立目录(避免 rsync 同步覆盖)
RUN mkdir -p /opt/xhprof \
&& cd /opt/xhprof \
&& composer require perftools/php-profiler
# XHProf: 复制采集器 bootstrap全局配置不受 rsync 同步影响)
COPY source/docker/xhprof-bootstrap.php /opt/xhprof/xhprof-bootstrap.php
# XHProf: 配置 auto_prepend_filezzz 前缀确保最后加载)
RUN echo 'auto_prepend_file = /opt/xhprof/xhprof-bootstrap.php' > /usr/local/etc/php/conf.d/zzz-xhprof.ini
# Internal install composer and dependencies, comment out if not needed
# RUN install-php-extensions @composer

View File

@@ -17,6 +17,9 @@ services:
environment:
# Sync polling interval in seconds (default: 3)
- SYNC_INTERVAL=${SYNC_INTERVAL:-3}
- XHPROF_ENABLE=${XHPROF_ENABLE:-false}
- XHGUI_UPLOAD_URL=http://xhgui:80/run/import
- XHGUI_UPLOAD_TOKEN=${XHGUI_UPLOAD_TOKEN:-ulthon-admin-xhprof}
extra_hosts:
- "host.docker.internal:host-gateway"
depends_on:
@@ -57,3 +60,21 @@ services:
depends_on:
mysql:
condition: service_healthy
xhgui:
image: xhgui/xhgui:0.24.x
restart: unless-stopped
profiles: ["xhprof"]
ports:
- "8142:80"
volumes:
# 挂载全局认证配置Basic Auth
- ../../../source/docker/xhgui-config.php:/var/www/xhgui/config/config.php
environment:
- XHGUI_UPLOAD_TOKEN=${XHGUI_UPLOAD_TOKEN:-ulthon-admin-xhprof}
# Basic Auth 认证(默认 admin/xhgui123通过 .env 或环境变量覆盖)
- XHGUI_AUTH_USER=${XHGUI_AUTH_USER:-admin}
- XHGUI_AUTH_PASS=${XHGUI_AUTH_PASS:-xhgui123}
depends_on:
mysql:
condition: service_healthy