HaveComputerWillCode.Com

Welcome!
Life is a Non-Deterministic Finite State Automata
Automation ? (*pGeekiness)++ : Code /eneration;

January 11, 2011

Processing JSON Responses with Jmeter and the BSF Post Processor

Filed under: Performance,Testing — Tags: , , , — admin @ 9:20 am

The JMX file can be downloaded here.

When testing web services these days with Jmeter, you will almost certainly come across the JSON Notation [JavaScript Object Notation]. It is nothing fancy – it is just a very compact way of serializing and deserializing objects in a way best suited to JavaScript. Think of it as XML without the Lard.

When processing responses with Jmeter, the usual approach is to use the ‘Regular Expression Post Processor’ to pull out matches and stash them into variables. That generally works fine with raw HTML but not with complex JSON structures. This post will show you how to make a request to the Google Search Service using Jmeter that returns a response in the JSON Format. We will then use a BSF Post Processor to turn that JSON into a real Javascript object and extract the URLs using code. Each URL will be placed into its own Jmeter variable.

To get started with JSON in Jmeter, add a HTTP Sampler in Jmeter and then a Debug Sampler. At the very end, add a Tree View Listener so we can see everything that happened. The HTTP Sampler that does the search should look like this:

That sampler is making a call to the Google Web API for ‘havebootswilltrek’ (my other blog) and returning the matching results. If I run it, I will end up with this output from the Search:

The structure is obvious if you have worked with JSON before: responseData.results[]…. etc. Without going into JSON in too much detail – you can read about the returned Google structure here – what I want to do is to turn every matching URL into a Jmeter variable of the form haveBoots[Number].

Let’s get too it!

To the above Google Request, add a BSF Post Processor and set it up as follows:

In the ‘Script’ part, we will add the following code:

// Turn the JSON into an object called 'SearchResponse'
eval('var SearchResponse = ' + prev.getResponseDataAsString());

// Create a variable called haveBoots_# containing the number of matching URLs
// For Each Result
//     Create a variable called haveBoots and assign it the URL
vars.put("haveBoots_#", SearchResponse.responseData.results.length);

for (var counter = 1; counter <= SearchResponse.responseData.results.length; counter++)	
{		
    var x = SearchResponse.responseData.results[counter - 1];

   // haveBoots = theUrl
   vars.put("haveBoots" + counter, x.unescapedUrl);
}

That will extract the ResponseData – which in this case is JSON – from the ‘Previous’ Sampler (ie: the parent ‘Search’ Sampler) and then turn the JSON into a JavaScript object using eval(). It’s then a simple case of iterating the .results. and creating a variable for each match.

When you rerun the script, the output from the Debug Sampler will look like this:

To extend this, you could create variables using the BSF Post Processor that look like the Regular Expression Post Processor extractor.

L8r!

1 Comment

  1. Thanks a lot dude. My backside was in hotwater, everyone was looking upt to the contractor to deliver. you’ve just helped a stup further … Thanks

    Comment by Dreamer — January 17, 2011 @ 4:05 pm

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress