top of page
  • Writer's picturePanshul Khurana

Profiling Drupal application using Xdebug and Webgrind

Updated: Jul 20, 2022

Sunday, May 17, 2020

Profiling an application is an integral part of software development. It is highly recommended to check the performance of the code before deploying it to Stage or production servers, to make sure that application's performance has the best possible performance rating. Xdebug is a PHP extension that can be used for the following :

  • Step by step Debugging

  • Adding breakpoints to the code and getting backtrace of a function.

  • Providing additional stack traces for Notices, Warnings, Errors and Exceptions

Most importantly ,

  • Profiling a PHP application using CacheGrind tool.

Let's see how we can use xdebug to profile a Drupal application. To start, let's get our local machine ready for using Xdebug.

Installation

For installation you can either continue reading the post or checkout the youtube tutorial here in order to see it in action : https://www.youtube.com/watch?v=pmgwZ6RXdrMThe steps shared here can be followed for Linux and MacOs. For installation on windows, follow the official documentation here: https://xdebug.org/docs/install


Xdebug Setup

  1. Install Xdebug using the following command pecl install xdebug If you don't have pecl installed on your machine, follow commands - sudo apt-get install php-pear sudo apt-get install php-xml php7.2-xml.

  2. Once, Xdebug is installed successfully, add the following line to your PHP.ini file - zend_extension="/usr/local/php/modules/xdebug.so" NOTE : Make sure to check the path first before it is included.

  3. Restart the apache server to load the changes : sudo /etc/init.d/apache2 restart

  4. To verify the changes, create a file by the name info.php at your webroot directory and add the following code - <?php phpinfo(); ?> Now execute the php file using terminal or access the info.php file on your browser and notice the following changes - That's it!! We now have xdebug enabled on our local machine, we will make few a more changes in order to check the Drupal Application's performance.

  5. Next step is to add setup the environment for profiling, to do this add the following line of code in the php.ini file - xdebug.profiler_enable_trigger = 1 This will help us to profile the Code by adding the following query parameter to the profiling application local url - ?XDEBUG_PROFILE=on NOTE : In order for this to work make sure to set the following in your php.ini file, else the profiler won't be able to generate the CacheGrind file. xdebug.profiler_enable = 0

  6. If you want to change the directory of the generated output file (cachegrind) ad the following line your php.ini file xdebug.profiler_output_dir="/var/www/html/profiler/xdebug_profiler" and also if you want to change the name of the file generated, use the following code and add it in your php.ini file - xdebug.profiler_output_name="cachegrind.out.%u.%H_%R" That's it! Don't forget to restart your apache server for changes to take affect. Now let's setup webgrind tool. WebGrind Setup To setup webgrind, head towards the github URL : https://github.com/jokkedk/webgrind

  7. Clone or download the zip file.

  8. Unzip the file and place it in your webroot directory

  9. Visit thee URL ex. http://127.0.0.1/webgrind.

You should be able to see the following interface- Before we profile our Drupal application let's see how we can profile a simple PHP application. For this,

  • Create a simple PHP script at your webroot directory and add some testing code like -


<?php 

function testEverything() {   
  for ($i = 0; $i<8000; $i++) {
 }   
}
 
function add() {   
  $a = 1000000;   
  $b = 10000000;   
  $b = $a + $b; 
}    
    
function multiply() {   
   $a = 8000;   
   $b = 9000;   
   $c = $a*$b;    
}  
  
multiply(); 
add(); 
testEverything(); 
echo 'Testing all functions......'; 

?>


Now let's execute the script using the query parameter as - http://127.0.0.1/profiler.php?XDEBUG_PROFILE=on Notice the following results in webgrind - Similarly, append the query parameter to the Drupal URL as follows : http://localhost/drupalSession?XDEBUG_PROFILE=on NOTE: The generated cachegrind file size can be as large as the application's size. For example, a simple script might generate a smaller sized cachegrind file whereas Drupal's cachegrind file size can be a bit larger than that. And the time to display the result on webgrind will depend upon the size of the file. Drupal Application Detailed performance report : https://youtu.be/IMRIJxAW4UcThis video covers the following :

  • Detailed Drupal Application performance report break down.

  • Explanations about Inclusive Costs, Self Costs and invocation count.

  • Checking the code written in the method that is invocated multiple times and has a greater number of self cost involved.

Conclusion This is how you can use Xdebug and Webgrind for profiling any PHP based application. As compared to other tools, the process of setting up Xdebug and Cachegrind tools is fairly simple and can be used while development to keep a check on the performance of the application.

28 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

Comments


bottom of page