Home arrow PHP arrow Page 2 - Benchmarking with the Xdebug Extension

Review: tracking program flow with the Xdebug extension - PHP

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.

TABLE OF CONTENTS:
  1. Benchmarking with the Xdebug Extension
  2. Review: tracking program flow with the Xdebug extension
  3. Benchmarking PHP applications with the X-debug extension
  4. Extending the use of the xdebug_time_index() function
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
March 09, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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 :Doe


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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: