Benchmarking with the Xdebug Extension

Welcome to the sixth installment of a series that covers debugging in PHP with the Xdebug extension. Comprised of seven approachable tutorials, this series gets you started using the set of functions provided by this helpful library, so you can begin debugging your own PHP applications with an excellent level of control.

Introduction

In case you haven’t heard about it yet, the Xdebug extension is a full-featured library that allows you to perform all sorts of debugging tasks in PHP. These range from checking the values assigned to variables and determining which program lines are executed by the PHP engine, to performing more complex operations, such as benchmarking entire applications and verifying in which order functions are called by a script.

Of course, it’s nearly impossible to learn how to achieve all of these tasks in only one single step, so if you’re interested in mastering the most relevant methods that come bundled with the Xdebug PHP extension, then you might want to have a close look at this group of educational articles.

Now that you’re aware of the topics that are covered in this series of articles, it’s time to recall what we discussed in the previous part. In that particular chapter, I used a couple of examples to help explain how to use the  “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions to keep track of the sequence of lines executed by a sample PHP script.

These functions permit you to verify, with a decent level of accuracy, the order in what the different blocks that compose a certain PHP application are called by the interpreter. Logically, performing this controlling process helps to debug errors that are commonly raised when working with conditional statements and recursive functions, which under normal conditions are pretty hard to grab.

You’ll be pleased to hear that the Xdebug extension comes equipped with additional functions that allow us to perform other debugging tasks, such as benchmarking PHP applications and checking in what order certain functions were called by the PHP engine.

Therefore, in this sixth chapter of the series I’ll be discussing in depth how to take advantage of the benchmarking abilities offered by the Xdebug library, which rely heavily on its “xdebug_time_index()” function.

Are you ready to learn how to use this brand new debugging function? Then click on the link that appears below and start reading now!

{mospagebreak title=Review: tracking program flow with the Xdebug extension}

If you didn’t have the chance to learn how to use the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions for keeping track of the sequence of lines executed by a PHP application, then you’re in luck; here’s an example created in the preceding article of this series, which demonstrates how to utilize these functions along with a simple PHP class.

Take a look at the corresponding code sample, please:


class User{

private $firstName;

private $lastName;

private $email;

public function __construct($firstName,$lastName,$email){

if(!$firstName){

throw new Exception(‘Invalid parameter First Name.’);

}

if(!$lastName){

throw new Exception(‘Invalid parameter Last Name.’);

}

if(!$email){

throw new Exception(‘Invalid parameter Email.’);

}

$this->firstName=$firstName;

$this->lastName=$lastName;

$this->email=$email;

}

// get first name

public function getFirstName(){

return $this->firstName;

}

// get last name

public function getLastName(){

return $this->lastName;

}

// get email

public function getEmail(){

return $this->email;

}

}


xdebug_start_code_coverage();

$user=new User(‘John’,'Doe’,'john@domain.com’);

if(rand(1,10)<5){

echo ‘First Name :’.$user->getFirstName().’<br />’;

echo ‘Last Name :’.$user->getLastName().’<br />’;

}

else{

echo ‘Email :’.$user->getEmail().’<br />’;

}

var_dump(xdebug_get_code_coverage());


/* displays the following when ‘rand()’ function returns a value < 5


First Name :John

Last Name :D oe


array

‘/path/to/example/program_flow_debug.php’ =>

array

8 => int 1

11 => int 1

14 => int 1

17 => int 1

18 => int 1

19 => int 1

20 => int 1

23 => int 1

36 => int 1

37 => int 1

38 => int 1

39 => int 1

40 => int 1

44 => int 1


// displays the following when the ‘rand()’ function returns a value > 5


Email :john@domain.com


array

/path/to/example/program_flow_debug.php’ =>

array

8 => int 1

11 => int 1

14 => int 1

17 => int 1

18 => int 1

19 => int 1

20 => int 1

31 => int 1

36 => int 1

37 => int 1

42 => int 1

44 => int 1


*/


As illustrated by the above hands-on example, the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions can be very useful when it comes to finding out the sequence of lines executed by a PHP script.

In this specific case, this pair of complementary functions is utilized in conjunction with a basic class and a simple conditional “if” statement, with the purpose of showing how the two blocks of code that compose the prior script are called by the PHP engine. Not too difficult to grasp, right?

Well, at this point I should assume that you’re pretty familiar with using the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions to control the flow of a specified PHP program. Therefore, it’s time to continue exploring other functions of the library.

As I stated in the beginning, this extension also offers a function called “xdebug_time_index().” As its name suggests, it can be used for timing PHP scripts with minor hassles. Therefore, in the section to come I’ll be explaining how to use this function for benchmarking purposes.

This topic will be discussed in the next few lines, so click on the link that appears below and keep reading.

{mospagebreak title=Benchmarking PHP applications with the X-debug extension}

As you may guess, any decent PHP debugging library must have the capability of benchmarking programs with relative ease, and certainly Xdebug isn’t an exception. The library in question comes equipped with a handy function called “xdebug_time_index(),” which can be used for timing PHP scripts with extreme simplicity.

To demonstrate this function, I’m going to start coding a basic example, which will use it within a typical “for” loop. Here’s the corresponding code sample; have a look at it, please:


echo ‘Starting time : ‘.xdebug_time_index().’<br />’;

for($i=0;$i<=3;$i++){

// do nothing

sleep(1);

}

echo ‘Total script execution time : ‘.xdebug_time_index();


/* displays the following


Starting time : 0.001168966293335

Total script execution time : 3.9993019104004

*/


If you analyze the above code sample, you’ll realize how simple it is to benchmark a specified PHP script using the aforementioned “xdebug_time_index()” function. As shown above, the function has been called twice, that is before the “for” statement starts its iteration, and at the end of the script, to display its total execution time.

Of course, if you’re anything like me, then you may want to see how the flexibility offered by the “xdebug_time_index()” function can be exploited to create an object-oriented timing application with extreme ease.

With that idea in mind, the last section of this article will be focused on explaining how to build a basic timer class, which will use internally the previous “xdebug_time_index()” function for benchmarking PHP scripts.

This brand new timer class will be developed in the following segment, so jump ahead and read the new few lines. I’ll be there, waiting for you.

{mospagebreak title=Extending the use of the xdebug_time_index() function}

In the section that you just read, I explained how to use the “xdebug_time_index()” function, to create a simple procedural timing script. However, it’s also feasible to take advantage of the intrinsic capabilities of this function to build a timer class.

Want to see how this class can be built in a few easy steps? Examine the following code sample, which defines a primitive “Timer” class that obviously uses the “xdebug_time_index()” function as its workhorse. Here it is:

class Timer{

public function __construct(){}

public function getTime(){

return xdebug_time_index();

}

}


Indeed, the above “Timer” class doesn’t bear any further discussion. It only implements a single method called “getTime(),” which behaves as a wrapper for the “xdebug_time_index()” function. That’s all.

It would, however, be pretty interesting to see how this class can be used for timing a simple application. Thus, below I coded a final hands-on example that utilizes the previous timing class for calculating the total execution time of a basic script. Here it is:

$timer=new Timer();

echo ‘Starting time :’.$timer->getTime().’<br />’;

for($i=0;$i<=5;$i++){

sleep(1);

}

echo ‘Total script execution time :’.$timer->getTime();

/* displays the following


Starting time :0.0012168884277344

Total script execution time :5.9927868843079


*/


Here you have it. Now that you’ve seen for yourself how simple it is to build a timer class using the “xdebug_time_index()” function, hopefully this will fire up your own creativity, and you’ll start building your own benchmarking scripts.

Final thoughts

In this sixth episode of the series, I walked you through using the “xdebug_time_index()” function that comes with the X-debug extension, to perform basic benchmarking on several PHP scripts. Undeniably, the utilization of this function is pretty intuitive, so in theory you shouldn’t have major problems using it for timing your own PHP programs.

However, this series still has a final installment, in which you’ll learn how to employ another handy function of the X-debug library. It’s called “xdebug_get_function_stack(),” and it will be utilized for exploring the stack of function calls generated by the PHP engine during the execution of a script.

Now that you’ve been warned about the subject of the final article of this series, you won’t want to miss it!

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan