PHP Profiling based on XHGui

1.1.2 2024-04-16 05:42 UTC


PHP Profiler

A PHP profiling library to submit profilings to XHGui.

Supported profilers:

This profiling library will auto-detect any supported profiler and use that. The specific profiler can be chosen by profiler config key.


  • Compatibility with PHP >= 5.3.0
  • No dependencies aside from the relevant extensions
  • Customizable and configurable so you can build your own logic on top of it


In order to profile your application, you need to:


The supported way to install this package is via composer:

composer require perftools/php-profiler

Create profiler

Creating profiler would be something like this:


// Add this block inside some bootstrapper or other "early central point in execution"
try {
     * The constructor will throw an exception if the environment
     * isn't fit for profiling (extensions missing, other problems)
    $profiler = new \Xhgui\Profiler\Profiler($config);

    // The profiler itself checks whether it should be enabled
    // for request (executes lambda function from config)
} catch (Exception $e){
    // throw away or log error about profiling instantiation failure

If you need to disable profiler doing flush, session_write_close and fastcgi_finish_request at the end of profiling, pass false to register shutdown handler:


Using config file

You can create config/config.php and load config from there:

  1. copy config/config.default.php to config/config.php
  2. use Config::create() to new Profiler
// Config::create() will load config/config.default.php
// and then merge with config/config.php (if it exists).
$config = \Xhgui\Profiler\Config::create();
$profiler = new \Xhgui\Profiler\Profiler($config);

Advanced Usage

You might want to control capture and sending yourself, perhaps modify data before sending.

/** @var \Xhgui\Profiler\Profiler $profiler */
// start profiling
$profiler->enable($flags, $options);

// run program

// stop profiler
$profiler_data = $profiler->disable();

// send $profiler_data to saver


To be able to profile autoloader, this project provides autoload.php that loads classes needed to start up the profiler.

Load it before loading composer autoloader:

require_once '/path/to/your/project/vendor/perftools/php-profiler/autoload.php';

$profiler = new \Xhgui\Profiler\Profiler($config);

require_once '/path/to/your/project/vendor/autoload.php';

Loading composer autoloader is still needed when saving results to MongoDB or PDO directly.


Reference config of what can be configured:

It includes all configuration options and inline documentation about the options.


To deliver captured data to XHGui, you will need one of the savers to submit to the datastore XHGui uses.

Stack saver

Allows saving to multiple handlers.

The example config configures to use Upload Saver, and if that fails, save to File Saver:

    'save.handler' => \Xhgui\Profiler\Profiler::SAVER_STACK,
    'save.handler.stack' => array(
        'savers' => array(
        // if saveAll=false, break the chain on successful save
        'saveAll' => false,
    // subhandler specific configs
    'save.handler.file' => array(
        'filename' => '/tmp/',
    'save.handler.upload' => array(
        'url' => '',
        'timeout' => 3,
        'token' => 'token',

Upload saver

This is the recommended saver as it's the easiest to set up.

Example config:

    'save.handler' => \Xhgui\Profiler\Profiler::SAVER_UPLOAD,

    // Saving profile data by upload is only recommended with HTTPS
    // endpoints that have IP whitelists applied.
    'save.handler.upload' => array(
        'url' => '',
        // The timeout option is in seconds and defaults to 3 if unspecified.
        'timeout' => 3,
        // the token must match 'upload.token' config in XHGui
        'token' => 'token',

File saver

If your site cannot directly connect to your XHGui instance, you can choose to save your data to a temporary file for a later import to XHGui.

Example config:

    'save.handler' => \Xhgui\Profiler\Profiler::SAVER_FILE,
    'save.handler.file' => array(
        // Appends jsonlines formatted data to this path
        'filename' => '/tmp/',

To import a saved files, see Import jsonl files section.

MongoDB Saver

NOTE: Saving directly to MongoDB is discouraged, use Upload/File/Stack saver instead.

For saving directly to MongoDB you would need ext-mongo for PHP 5 and ext-mongodb with alcaeus/mongo-php-adapter package for PHP 7 along with perftools/xhgui-collector package:

for PHP 5:

pecl install mongo
composer require perftools/xhgui-collector

for PHP 7:

pecl install mongodb
composer require perftools/xhgui-collector alcaeus/mongo-php-adapter

Example config:

    'save.handler' => \Xhgui\Profiler\Profiler::SAVER_MONGODB,
    'save.handler.mongodb' => array(
        'dsn' => 'mongodb://',
        'database' => 'xhprof',
        // Allows you to pass additional options like replicaSet to MongoClient.
        // 'username', 'password' and 'db' (where the user is added)
        'options' => array(),
        // Allows you to pass driver options like ca_file to MongoClient
        'driverOptions' => array(),

PDO Saver

NOTE: Saving directly to PDO is discouraged, use Upload/File/Stack saver instead.

PDO Saver should be able to save to any PDO driver connection.

You will need to install additionally perftools/xhgui-collector package:

composer require perftools/xhgui-collector

Example config:

    'save.handler' => \Xhgui\Profiler\Profiler::SAVER_PDO,
    'save.handler.pdo' => array(
        'dsn' => 'sqlite:/tmp/xhgui.sqlite3',
        'user' => null,
        'pass' => null,
        'table' => 'results'

Custom Saver

You can create your own profile saver by implementing SaverInterface and calling setSaver().

use Xhgui\Profiler\Profiler;
use Xhgui\Profiler\Saver\SaverInterface;

class StdOutSaver implements SaverInterface
    public function isSupported()
        return true;

    public function save(array $data)
        fwrite(STDOUT, json_encode($data));

/** @var Profiler $profiler */
$profiler->setSaver(new StdOutSaver());

Import jsonl files

You can use ./bin/import.php script to submit files saved by File Saver to XHGui server.

  1. Setup config file
  2. Configure to use Upload Saver
  3. Execute the ./bin/import.php script

The script can take multiple jsonl formatted files, or if none given read stdin stream.

$ ./bin/import.php tests/tmp/php-profiler-xhgui-test-1596093567.787220-c857.json
Imported 1 lines

Configure Profiling Rate

You may want to change how frequently you profile the application. The profiler.enable configuration option allows you to provide a callback function that specifies the requests that are profiled.

The following example configures to profile 1 in 100 requests, excluding requests with the /blog URL path:

    'profiler.enable' => function() {
        $url = $_SERVER['REQUEST_URI'];
        if (strpos($url, '/blog') === 0) {
            return false;

        return mt_rand(1, 100) === 42;

In contrast, the following example instructs to profile every request:

    'profiler.enable' => function() {
        return true;

Profile using XHProf helper

If you want to start profiling using a browser based tool like XHProf helper, You can use this method:

    'profiler.enable' => function() {
        return !empty($_COOKIE['_profiler']);
        // or
        return !empty($_COOKIE['XHProf_Profile']);

Configure 'Simple' URLs Creation

This library generates 'simple' URLs for each profile collected. These URLs are used to generate the aggregate data used on the URL view. Since different applications have different requirements for how URLs map to logical blocks of code, the profile.simple_url configuration option allows you to provide the logic used to generate the simple URL.

By default, all numeric values in the query string are removed.

    'profile.simple_url' => function($url) {
        return preg_replace('/=\d+/', '', $url);

Configure ignored functions

You can use the profiler.options configuration value to set additional options for the profiler extension. This is useful when you want to exclude specific functions from your profiler data:

    'profiler.options' => array(
        'ignored_functions' => array(

In addition, if you do not want to profile all PHP built-in functions, Add ProfilingFlags::NO_BUILTINS, to 'profiler.flags'.

Installing profilers

For this library to capture profiling data, you would need any of the profiler extension. Depending on your environment (PHP version), you may need to install different extension.

Supported profilers:

Tideways XHProf (5.+)

Tideways XHProf v5.x requires PHP >= 7.0.

To install tideways_xhprof extension, see their installation documentation.

Alternatively on brew (macOS) you can use packages from kabel/pecl tap:

brew install kabel/pecl/php@7.4-tideways-xhprof
brew install kabel/pecl/php@8.0-tideways-xhprof
brew install kabel/pecl/php@8.1-tideways-xhprof

For outdated php versions few recipes exist in glensc/tap tap:

brew install glensc/tap/php@7.1-tideways-xhprof


XHProf supports all PHP versions.

  • xhprof 0.9.x requires PHP >= 5.3, < PHP 7.0
  • xhprof 2.x requires PHP >= 7.0

for PHP 5.x:

pecl install xhprof-0.9.4

for PHP >=7.0:

pecl install xhprof

Alternatively on brew (macOS) you can use packages from kabel/pecl tap:

brew install kabel/pecl/php@7.4-xhprof
brew install kabel/pecl/php@8.0-xhprof
brew install kabel/pecl/php@8.1-xhprof

Tideways (4.x)

Tideways 4.x extension requires with PHP >= 7.0.

To install tideways extension, see their installation documentation.

curl -sSfL | tar zx
cd php-xhprof-extension-4.1.6/
make install
echo extension=/usr/local/lib/php/pecl/20160303/ | tee /usr/local/etc/php/7.1/conf.d/ext-tideways.ini


UProfiler requires PHP >= 5.3, < PHP 7.0

To install uprofiler extension, see their installation documentation.