Pushing the Envelope with JMeter

Web Apps: Find how far you can go - and - what happens when you go too far! For once the phrase is being used properly!
This common business phrase comes from aeronautics. The concept being that the known set of extremes of flight at which an aeroplane is flyable is called its envelope. A test pilot must take this known envelope and push the aeroplane beyond it. Only by doing so can designers understand how far the aeroplane can really be pushed and pilots can be sure that they are flying safely during normal flight. The picture above is of the astonishing X15 experimental rocket plane. The job of its pilot was to take this machine to and beyond the known envelope every time it was flown. So it must be with web applications. Of all applications, there are the ones which will get the most abuse. Knowing yours works is only half the story. The interesting stuff, and that which separates the great from the good, is to know when it breaks. To kick off, let us look at the test setup. On one machine I have TAG.net running a Deployview instance. This is connected over my LAN to the test machine. The test machine has Internet Exporer 7 (beta) and windows XP. What I want to do is make a realistic test of the system using JMeter. This is like showing that the aeroplane will actually fly. Once I have a realistic simulation of a browser session with the server, I can then load it up a bit; yes that will be Pushing The Envelope! Unless you are truly super human, making an accurate assessment of exactly what ‘chatter’ goes on between a browser and the server by just looking at the web pages is impossible. I have tried it many times and have always been surprise at how much I miss out. So, to be realistic we need a way of recording what goes on. Because I am using TAG.net I could use the chatter recorder built into TAG.net to record all the requests and responses. This is very handy for debugging, but it is rather awkward in this case as I would have to copy over all the data into JMeter. By far the easiest (and most general) solution is to use JMeter’s built in proxy server. A HTTP Proxy server uses a special (publicly defined) protocol to allow communication with the browser. The browser asks the proxy server for a page and the proxy server goes and gets the page and passes it back to the browser. This means that the proxy server gets to ‘see’ all the communication from the browser to the server and back. JMeter’s proxy server can record this conversation in a format JMeter automatically understands.

You can think of JMeter as a telephone tap! It hides in the connection from the browser to the server.

To set up a JMeter HTTP Proxy Server we can start off with a new JMeter project. I simply fired up JMeter and started to work on the empty project it started with. I wanted the recording from the HTTP Proy Server to go into my workbench rather than clutter up the Test Pan. To achieve this, right click on the WorkBench icon in the left hand panel and move through Add, Non Test Elements, HTTP Proxy Server.

Now that the proxy exists it is necessary to configure it. I set mine up to Capture HTTP Headers and to Set Keep Alive. Keep alive means that for each session JMeter will try and reuse the socket it creates to talk to the server. In my case, the socket will be closed by TAG after a configured time, but most of the time one socket will be used for an entire session. This closely resembles the behaviour of most browsers. Though, in truth, browsers often use more than one socket to download on session. We can look into that in a later post.

Other settings for the proxy which I used were to make it record the conversation ender the server element “Target Controller: Work Bench->HTTP Proxy Server”. Also you want to ensure that it is set not to group samplers. It is very important to make note of the listening port, in this case it was 8080.

Next I set up IE7; IE6 is almost identical. For JMeter to listen into the chatter between the server and the browser, it is necessary to make the browser connect to JMeter rather than directly to the server. This is done from the Tools, Internet Options menu.

On the Internet Options panel, the Connections tab leads to the LAN settings and from there to the proxy settings.

In my case I had JMeter running on the same machine as the browser. This meant that the browser could see the JMeter proxy server on the local ‘loopback’ interface. This is called LocalHost on most Windows machines. To get the browser to talk to the proxy server I checked the ‘Use a proxy server’ check box. Then I put in ‘LocalHost’ as the server and 8080 as the port. The port is the one that was set in the JMeter HTTP Proxy Server setup earlier.

Now we are in a position to start recording sessions! But that is not what I actually did. I first added a Thread Group to my Test Plan. We will use it later, but as all the pictures have it in at this point, I discuss how to add it right now.

The Thread Group control threads of parallel execution of the Test Plan. You can think of each thread as a separate user. In the above picture, I right clicked on my Test Plan and went to Add/Thread Group.

Finally - to get the process rolling you must start the Proxy Server:

Then you use your browser to browser the site as you would normally. In my case I logged onto DeployView, went to user management, added a new user and then updated their details. This process was important for working each part of a web application test plan. The logging on used cookies, the form submissions used CGI encoding and the updating of the added user meant having to extract the user identifier from the result of one request and post it as part of the CGI variables for another.

As you browse then Proxy will record the all the page requests that are occurring. By expanding the tree beneath the HTTP Proxy Server tab in the left pane, you can see the individual requests. Once you have made a recording – remember to turn off the proxy server!

Each request from the browser gets recorded as an HTTP Request sampler. If you click on the sampler you can see the settings that JMeter has recorded:

I have found that JMeter sometimes makes mistakes. I have raised with the community and hopefully it can be resolved. In the mean time, it is worth looking through the results of the HTTP Proxy Server and checking for obvious errors (like that in the picture above where 618961df was appended the target URL). Also there may be some requests that get made in which you genuinely have no interest. In this example, I found that some debug code was making calls to the JSErrorLogger.php web service. This was not part of a production system, so I did not want theses requests in the Test Plan. Also, there might be some requests at the front and end of the browsing you did that you do not want in the test plan. No problem, this is one of the reasons we have a Work Bench! Click and drag (one at a time or use shift click) the requests you DO want up to the Test Plan. When you drop them on the Test Plan it will ask if you want them to be added before, after or as a child. You want them added as a child. At this point your test plan is nearly ready to run for the first time. I would strongly suggest saving it; crashes happen.

Just before running the script I added in a TreeView listener. This oddly named listener keeps a record of every request and response. It is not much use in load testing as it soon overloads, but it is just the ticket when testing a script. At this point I also added in an Aggregate Graph listener which is key to load testing.

There is silence in the room. The crowd awaits to big moment. Slowly he moves the mouse over the ‘Run’ menu button. The inevitable click and then again on ‘Start’. Time stands still, hundreds of minds in suspension. As one they think ‘Will it work?’


Get real, this is a computer; so, what went wrong? DeployView uses cookies to maintain the user session once the user is logged on. So we need to add in a Cookie Manager. This will store the any Cookies associated with a thread. Remember that in JMeter threads are like users, so if you have 4 threads then you will have the same as 4 separate browsers connected to the server, and hence 4 separate cookies.

In my test I wanted each run through the test plan to be like a new login so I clicked ‘Clear cookies each iteration?’

This time it worked – no red! Do you remember that we are working through an example off creating a user and then updating that user? Well we have gotten most of the way. However, this test plan as it stands does not update the user it creates, it keeps updating the user that was created when the session was recorded by the HTTP Proxy Server. Extracting a variable from a response is the wait to finish off our goal here. To achieve this I had to know a little bit about the web application I was testing. I knew that the new user’s id would be reported in the response from the first call to DVAddUser.php (we like to keep names nice and obvious!) To extract data like this we need to add a Regular Expression Extractor, I will show how I did this in a minute. First I had to find the appropriate page. To do that I selected my handy View Results Tree and by searched for the page request I in the left panel of JMeter. Once I had what I thought was the correct request I could than line that up with the View Results Tree and see if the data passing back and forth was what I expected it to be.

After a lot of squinting at the results in the right hand pane, I finally found the result I was looking for:

It had the format id="iud" value="". I have deliberately not written tin the value there. I am not interested in it! It is not the actual value that is important, but how to find it. JMeter finds values in responses using regular expressions. Now that I knew which request/response I was looking for and what in the response I was looking, I could add a Regular Expression Extractor.

I set up the extractor to look in the response body. I then gave it a reasonable name “AddUser” and set its value to the match to the contents of the value clause of the regex. The reason that I knew this regex would always find the value I wanted is that the Id of the HTML element is unique. So, as long as the page was not corrupt, I could use the definition of the Id as “uid” as an anchor to fund the appropriate value. The match inside the brackets then is forwarded to the "template" setting in the extractor. Just to be sure I set the “Default” value to something that would be obvious if the extraction went wrong; in DeployView there should never be a user with an Id of -1.

Placing the extracted value in the update request was the next step.

Putting an extracted value into a request is quite straightforward. In JMeter speak, the extraction process creates a ‘variable’. In my case the variable was called AddUser. To place a variable into some other part of the JMeter process you can references it as ${Name}. In my case I found the HTTP Request for DVUpdateUser and in the “Send Parameters With Request” section I replaces the uid parameter’s value with ${AddUser}. To make the data that was updating the the database nice and obvious, I added in the Id to the first named as well (as shown in the above image).

Then I just ran it one more time and checked the application to see if the add and update happened – and they did.

Now we have achieved our first test flight! In my next post on the subject, I will show how to ‘Push the Envelope’.

But – be careful: