PHP debugging with JMeter and Zend Debugger
For those who don’t know what JMeter is, you’re missing A LOT! in short, JMeter is a desktop application designed to load test functional behavior and measure performance. (http://jmeter.apache.org/)
If you’re working on an API or consuming content from any HTTP service you could easily create a JMeter plan in order to test it.
Problem
Now, I think JMeter is awesome, no doubt about it, however what if I want to test something while developing a new API Endpoint? well I used to be accustom to the nasty var_dump(); exit; combination or if the API was already completed I used to do error_log() everywhere and figure out the results.
Now, I’ve been using Zend Studio for quite a while and installed Zend Server on my local machine to test and develop and I love the easy integration that Zend Server has with Zend Studio for debugging and I though to myself, why not just change the client that triggers the debugging console so instead of the browser use JMeter, at the end both uses HTTP so it should work, well guess what, IT DOES, and It’s awesome. No more print_r, error_log, var_dump in the API, lets do the Step by Step debugging with JMeter!
Solution
If you can do it using a browser you can do it using JMeter. So lets start.
Note: Even though I’m writting about Zend Debugger and Zend Studio, I would like to point out that the same can be acomplished using XDebug (I’ll write later on about it). If you don’t like Zend Server/Zend Studio, don’t worry, XDebug is an awesome project, it’s open, free and there are plugins for the most popular browsers and IDEs, 100% recommended!
Download the JMeter Plan
JMeter Plan: JMeterZend.jmx
1. Create a new project in Zend Studio.
I created a new virtual host on my machine just for the sake of the example. In my case I’ll use fakesite.pabloviquez.com.
Once that was completed, I added a new page (index.php) with some simple code for the sake of the example.
index.php
<?php
/**
* This site is a fake site, however I want to make request to
* it and do step by step debugging with JMeter and
* Zend Debugger
*
* @see http://www.pabloviquez.com
* @author Pablo Viquez <pviquez@pabloviquez.com>
*/
$startVariable = 1;
$anotherVariable = 'x';
for ($i = 0; $i <= 10; $i++) {
echo "For loop, i: {$i}\n<br />\n";
}
2. Configure Zend Studio
Open Zend Studio preferences, and open the Installed Debuggers section: PHP > Debug > Installed Debuggers, now write the port used for the Zend Debugger somewhere, we’re going to need it later on.
3. Create a new JMeter Plan
Open JMeter and create a new plan, should look like this:
Now lets add the following sections using the same structure:
- Thread Group
- HTTP Request
- HTTP Cookie Manager
- User Defined Variables
- View Results Tree
4. Configure JMeter
Now, lets add the required configurations to JMeter in order to make a simple GET request to the index.php page we created in the step 1.
4.1 User Defined Variables
Create a new variable with the name HOST, the value must be the host name you’re going to use without the protocol.
4.2 HTTP Request
Click on the HTTP Request, and put the HOST variable we created in the previous step inside the Server Name or IP field.
For the sake of testing, add a parameter in the “Send Parameters With the Request:” section. Add whatever you want, this is because I want to be able to see them in the debugger later on.
4.3 HTTP Cookie Manager
This is where the magic happens! Zend Debugger requires a set of cookies in the request in order to launch the debugger. Basically, PHP reads these cookies and the Zend Debugger module recognize them, if they are good, the debugger makes a broadcast to the debug port configured launching the debugging session.
The set of key/values are:
| Parameter |
Method |
Description |
| start_debug | GET/COOKIE | Starts the debug session. Set to zero to deactivate the debugger. |
| debug_host | GET/COOKIE | The client IP to which debugging results will be returned. This host should be allowed via php.ini setting zend_debugger.allow_hosts. The default is localhost. |
| debug_port | GET/COOKIE | The port to which the debugger will connect. This should match the port number configured in Zend Studio (Window | Preferences, PHP | Debug | Installed Debuggers). |
| send_sess_end | GET/COOKIE | Enables data to be sent at the end of a profiling session. |
| debug_stop | GET/COOKIE | If set, the debugger will pause at the first line of PHP code. |
| original_url | GET/COOKIE | The URL from which the debugging process will start. This is an important parameter, since Path Mapping resolution is based on it. |
| use_ssl | GET/COOKIE | If set, the communication with the client is encrypted using SSL. |
| start_profile | GET/COOKIE | Starts a profiling session. This cannot be used simultaneously with a start_debug request. The send_sess_end flag must be enabled for profiling results to be returned. |
| debug_start_session | GET/COOKIE | Enables debug session capabilities according to the parameter set in debug_cont_session. |
| debug_start_url | GET | If the debug_start_session parameter is set, browsing to the URL specified here will activate a debug session. Until this URL is reached, the debugger is not triggered even though a debug session is still active. After the URL is reached, the session will continue according to the settings specified for the debug_cont_session parameter. |
| debug_cont_session | GET | If set to 0, a debug session will be launched once when debug_start_url is reached. If set to 1, the debugging session will be launched once when the next link in the debug_start_url is clicked, a form is posted, or an AJAX request is executed. If set to 2, a debug session is started whenever the URL set in debug_start_url is browsed to. |
| no_remote | GET/COOKIE | If set to 1, the file content will only be taken from files located on the server. |
| use_remote | GET/COOKIE | If set to 1, file content will be taken from the project in Zend Studio. |
| debug_file_bp | GET/COOKIE | Sets a breakpoint in the specified file. |
| debug_line_bp | GET/COOKIE | Sets a breakpoint in the specified line in the file configured in debug_file_bp. |
| _bm | GET/COOKIE | Sets a bitmask with the configured options. |
| debug_jit | COOKIE | Debugger will delete cookie values after the debug session ends. |
| debug_fastfile | GET/COOKIE | Speeds up file exchange. Supported in Zend Studio since version 6.1.0 |
| debug_coverage | GET/COOKIE | Enables code coverage when profiling. |
| debug_session_id | GET/COOKIE | Debug session ID. Zend Studio since version 6.0.0 can work with several simultaneous debug sessions. This parameter allows to specify which debug session to attach to. |
(Values taken from: http://kb.zend.com/index.php?View=entry&EntryID=434)
Using those values, now add them to the HTTP Cookie manager:
5. Run the test plan!
Click Run > Run
The IDE should start! with the step by step debugging
Once completed you can check the results like you normally will do.












Follow me on Twitter
RSS