Tag Archives: profiling

Xhprof for profiling PHP applications

1 Mar

We wrote extensions to Xhprof so we could do aggregate/production grade profiling. To enable this capability we use Redis as the data store for profiling data.

Installing Xhprof for profiling PHP applications

This step is optional and only required if you are planning on doing PHP profiling using Xhprof.

Xhprof is a lightweight profiling library developed at Facebook. It can be used to profile applications in production as well.

  1. Prereq
    1. Requires php-devel package
  2. To install Xhprof
    1. mkdir -p /opt/xhprof
    2. cd /opt/xhprof
    3. pecl d xhprof-0.9.2
    4. tar xzf xhprof-0.9.2.tgz
    5. cd xhprof-0.9.2/extension/
    6. phpize
    7. ./configure –with-php-config=/usr/bin/php-config
    8. make
    9. make test
    10. make install
  3. Add xhprof as extension to /etc/php.d/xhprof.ini


    ; directory used by default implementation of the iXHProfRuns
    ; interface (namely, the XHProfRuns_Default class) for storing
    ; XHProf runs.
  4. Xhprof has a dependency on Graphviz to generate the call graphs
    1. cd /etc/yum.repos.d/
    2. wget http://www.graphviz.org/graphviz-rhel.repo
    3. yum install graphviz.x86_64
  5. To install the Xhprof UI
    1. cd /var/www/html
    2. ln -s /opt/xhprof/xhprof-0.9.2/xhprof_html .
  6. Install a Redis Server
    1. If you don’t already have a Redis Server setup follow instructions at http://stansantiago.blogspot.com/search/label/redis to create a Redis Server and PHP Redis Client.

Redis Based Xhprof Extension

  1. The files referred to in this section are available as attachments to this wiki page. Please download
    and install at the specified locations.
  2. To support production grade profiling the following wrapper scripts have been written:
    1. XhprofWrapper.php: specified before and after the part of the code to be profiled
    2. XhprofAgg.php: This is a standalone PHP script that aggregates the profiling of a multi-user test
    3. XhprofTest.php: A standalone PHP script that can be used to test connectivity to Redis
    4. xhprof_runs.php: I’ve created a new class that has logic to read/save profiling data to a Redis instance.

Installing the Custom Extension

  1. Copy XhprofTest.php and XhprofAgg.php to /opt/xhprof
  2. Copy XhprofWrapper.php to a directory accessible by your PHP codebase.
  3. Backup the existing xhprof_runs.php
    1. cd /opt/xhprof/xhprof-0.9.2/xhprof_lib/utils
    2. cp xhprof_runs.php xhprof_runs.php.orig
    3. Append the contents of the attached xhprof_runs.php to the /opt/xhprof/xhprof-0.9.2/xhprof_lib/utils/xhprof_runs.php file.

Enabling Profiling

  1. To enable profiling we will add the XhprofWrapper code so it executes before the location you want to start profiling.
  2. Insert the XhprofWrapper preRequest() and postRequest() methods around the code that needs to be profiled:require_once ‘XhprofWrapper.php’;

    …* Modify the 3 main PHP entry pages (index.php, callgraph.php, typeahead.php) in the ” /opt/xhprof/xhprof-0.9.2/xhprof_html/” directory to use the new class instead of the default class XHProfRuns_Default. Change this line in the 3 files:
    $xhprof_runs_impl = new XHProfRuns_Default();* Ensure xhprof_html is accessible via Rewrite rules.Developer profiling: How to test a single request
  1. Print out/echo the runId returned by XhprofWrapper::postRequest()
    1. Replace <runId> in the URL below with this runId.
  2. Access http://<hostname>/xhprof_html/?run=<runId>&source=devprofile

Aggregate profiling: How to load test and generate an aggregate profile report

  1. Log into Redis Server machine:
    1. cd /opt/redis/latest/src
    2. ./redis-cli -h <hostname> -p <port number>
    3. “DEL runIds” will delete any existing runIds
  2. Run the load test
  3. Edit /opt/xhprof/XhprofAgg.php and set a Unique Aggregate run id; ALL, ALL2, ALL3 etc
  4. /usr/bin/php -f XhprofAgg.php to create Aggregate report
  5. Access http://<hostname>/xhprof_html/?run=ALL&source=aggregate to access the aggregate report.
  6. Repeat steps 2-6 for difference test scenarios if required



  • For more information see Xhprof.