top of page
  • Writer's picturePanshul Khurana

Profiling Drupal 8 application using Xhprof

Updated: Jul 20, 2022

Application profiling is an integral part of web development. It is really important to measure and know the performance of the application based upon various metrics such as Time consumed to load the resources, CPU utilisation, Memory consumption etc.

knowing these factors about your application will help to come up with a better product in the market. Your customer is relying on you, to increase his/her business and achieve the numbers that the customer has been aiming for.

Profiling is a way to identify and work on performance improvement areas by knowing the current performance status of your application in terms of CPU Utilisation, Function Call counts and traces, memory utilisation and time consumed to load resources.

"Xhprof", originally developed at facebook and launched in the open source market in March 2009 is a lightweight hierarchical profiler for PHP applications that delivers performance reports in terms of Inclusive/Exclusive Wall times, CPU utilisation and memory consumption. It provides :

  • Reports in terms of function level counts and inclusive and exclusive metrics such as Wall time, CPU time & Memory usage.

  • Function Profiles Can be broken down by Callers and Callees.

  • Simple HTML based User interface to show reports.

  • Also provides a Call graph view of the report to understand the code execution in terms of a graph.

You can use xhprof for:

  • Understanding structure of the code being executed in the form of callers and callees.

  • Provides hierarchical nature and Flat profile reports of the reports that can be used to determine the backtrace of the code.

  • Finding out the difference in the reports by providing the ability to compare.

  • Can be used on production (with some things to keep in mind such as number of requests to be profiled on production environment).

Before moving towards the details about xhprof and to see it's working with Drupal application let's take a look at the installation steps :

Installation Steps and Integration with Drupal :

To be able to integrate and profile a drupal application, two things are required :

  1. PHP Xhprof extension on your local machine.

  2. Drupal Xhprof Module installed on your Drupal application

I am using the UBUNTU 18.04 platform as an example here. But for complete installation steps for other platforms follow the community documentation.

First step is to run and update :

sudo apt-get update

Next, I used pecl to install the xhprof extension:

pear channel-update
pecl install xhprof

This should download and install the php xhprof module. Now, next step is to add following extension to your php.ini file to enable the same :


If you are unsure of the location of php.ini, file simply run:

php -i | grep php.ini

Once this is completed, you can verify the installation by running :

php -m | grep xhprof 


php -i | grep xhprof

This should display the xhprof php extension installed on your local machine.

Do not forget to restart the apache server by running:

sudo /etc/init.d/apache2 restart

Now, download and install the Drupal xhprof module using composer or manually download and place it in the contrib module folder. Once downloaded and enabled, navigate to the following configuration URL provided by the module : /admin/config/development/xhprof and you should be able to see the option:

Once enabled, you should be able to see the other configuration options to profile your application using installed xhprof php extension.

With the drupal module you can choose to :

  • Exclude the page for profiling by providing the URL of the page to exclude.

  • Exclude the PHP built in functions or indirect functions like call_user_func or call_user_func_array

  • Display CPU and Memory reports in the profile output.

  • Choose the profiling request intervals, this can be useful for production sites where you may choose to profile your application after a specific request interval has passed.

After enabling, visit the page that you want to profile and at the bottom of the page you should be able to see a link xhprof output which will navigate you to xhprof reports.

Some Basic Terminologies to understand xhprof reports better...

  • Call Graph : Displays executed code in a visual diagram where each node corresponds to a function or method and their relations show Code Flow.

  • Exclusive Time : How long does it take to execute a function without time spent on other functions/methods.

  • Inclusive Time : Total time spent executing a function/method including any external call.

To know more about Xhprof head towards:

On the reports page, you should be able to see the reports in the following format:

Now this is called a Flat profile

It Provides function-level summary information such as number of calls, inclusive/exclusive wall time, memory usage, and CPU time.

To see the details of each function, click on the function in the reports and you will be navigated to following reports :

This is called the "Hierarchical Profile"

For each function, it provides a breakdown of calls and times per parent (caller) & child (callee)

You can also profile your code while working on a specific feature on your local machine using the xhprof built-in functions:

xhprof_enable() : Enables the xhprof profiling.

xhprof_disable(): Stop profiling the application

Take a look at the example snippet here:

On running this displays the output in terms of :


    [foo==>bar] => Array
            [ct] => 2        # number of calls to bar() from foo()
            [wt] => 37       # time in bar() when called from foo()
            [cpu] => 0       # cpu time in bar() when called from foo()
            [mu] => 2208     # change in PHP memory usage in bar() when called from foo()
            [pmu] => 0       # change in PHP peak memory usage in bar() when called from foo()

    [foo==>strlen] => Array
            [ct] => 2
            [wt] => 3
            [cpu] => 0
            [mu] => 624
            [pmu] => 0


This is how it can be used at code level to profile while working on a specific feature.

This is how xhprof can be used to profile an application. Hope this helps in integration with Drupal. In case, you are using any other platform that is based upon PHP, using this blog you should be able to install php extension on your local machine.

References :

Thanks !!

4 views0 comments

Recent Posts

See All

How to configure Layout Builder with Drupal site?

Drupal's most interesting feature is the new Layout Builder which was added in Drupal 8.5. I'm sure you must have read about it and some of you might have already got your hands dirty with this amazin

My Experiments with Drupal 9 and Gutenberg Editor

As a developer, I understand the importance of providing the best possible features to ease out an editor's job. And recently when I was migrating my blog site I faced the challenge that I work every


bottom of page