A Docker image that includes the XHGui web interface, which can be used in conjunction with Xhprof to view PHP profiling data.

Setup

Install dependencies

composer require --dev perftools/php-profiler
Copied!

Bootstrap code

bootstrap.php
Copied!
$profiler = new \Xhgui\Profiler\Profiler([
    'save.handler' => \Xhgui\Profiler\Profiler::SAVER_FILE,
    'save.handler.file' => [
        'filename' => '/mnt/files/xhprof/xhgui.data.jsonl',
    ],
]);

$profiler->start();

(*) Update configuration

If your application makes use of services that run on localhost you should adjust your configuration to use an alternative DNS name:

- localhost:3306
+ host.docker.internal:3306
Copied!

Create Docker setup

docker-compose-xhgui.yml
Copied!
version: "3.8"

services:
  nginx-xhprof:
    image: wodby/nginx:1.19
    environment:
      NGINX_VHOST_PRESET: php
      NGINX_BACKEND_HOST: xhprof
      NGINX_SERVER_ROOT: /var/www/html/public
    ports:
      - 8000:80
    volumes:
      - app:/var/www/html

  xhprof:
    image: wodby/php:8.1-dev
    environment:
      PHP_DEBUG: true
      PHP_XHPROF: true
      PHP_XHPROF_SAMPLING_INTERVAL: 100000
      PHP_XHPROF_SAMPLING_DEPTH: INT_MAX
      PHP_XHPROF_COLLECT_ADDITIONAL_INFO: 0
      PHP_XHPROF_OUTPUT_DIR: /mnt/files/xhprof
    volumes:
      - app:/var/www/html
      - xhprof:/mnt/files/xhprof
    depends_on:
      - nginx-xhprof

  xhgui:
    image: ghcr.io/nedix/xhgui-docker
    volumes:
      - xhprof:/data/xhprof
    ports:
      - 8001:80
    depends_on:
      - xhprof

volumes:
  app:
    driver: local
    driver_opts:
      device: ${PWD}
      o: bind
      type: none
  xhprof:
    driver: local

Usage

Step 1.

Start the containers:

docker compose -f docker-compose.yml -f docker-compose-xhgui.yml up -d
Copied!

Step 2.

Navigate to your page on localhost:8000 to capture the profiling data.

Step 3.

Execute the following command to load the data into XHGui:

docker compose -f docker-compose-xhgui.yml exec xhgui php /var/www/xhgui/external/import.php -f /data/xhprof/xhgui.data.jsonl
Copied!

Step 4.

Find XHGui on localhost:8001.