Páginas

SyntaxHighlighter

Mostrando postagens com marcador stress test. Mostrar todas as postagens
Mostrando postagens com marcador stress test. Mostrar todas as postagens

quinta-feira, 12 de abril de 2012

JMeter - Using BSF Assertion to fail samples based on JSON response


I've tested some web applications whose unexpected errors or exceptions were handled by the application code and encoded in the response body as JSON.

A hypothetical response is shown below:

{
    data: null,
    message: "TimeoutException: Transaction rolled back after 3000 seconds.",
    success: false
}

The problem with this approach is that most of the times the HTTP response code is 200 (OK) which  jmeter interprets as a successful sample.
I'm not a big fan of this solution I'd rather have it returning 4XX or 5XX response codes, in which case jmeter would fail the sample.

In order to workaround this, I normally use a BSF Assertion containing javascript code similar to:

try {
   eval('var response = ' + prev.getResponseDataAsString());
   if (!response.success) {
      prev.setSuccessful(false);
      prev.setResponseMessage(response.message);
   }
} catch(e) {
      prev.setSuccessful(false);
      prev.setResponseMessage("Invalid response. Expected a valid JSON.");
}

First I try to evaluate the response string into a valid javascript object. If that succeeds I check the success flag and handle it properly, otherwise, in the catch block, I force the sample to fail due to a bad response format.
Note that in both failure scenarios I invoke the setSuccessful and the setResponseMessage methods to signal a sample failure and have better detailed error messages respectively.

And that's it!
Happy load testing!

JMeter - Changing sample label/name at runtime

When creating jmeter's test plans, sometimes its useful to change the samples names in order to have more accurate and meaningful reports. To achieve this I normally use one of these two approaches:

Using variables (placeholders) in the sampler name

If the variables you need to form the new sample name are already available just fill the the name's text field using the syntax ${variable name}.
The only problem with this approach is that the name shown in test plan tree (left panel) might end up being not so intuitive.


Dynamically setting it in BSF Post Processors

Just use the variable prev (which gives access to the previous SampleResult) and use the method setSampleLabel to set the new label.
In the example below I'm using the value of another variable (named url) previously put in the vars object:

quinta-feira, 31 de março de 2011

JMeter - Processing and handling JSON responses using BSF post processors

In one of my Jmeter posts I talked about how to post JSON data using a HttpSampler.
Now I want to show some cool stuff we can do using Jmeter's javascript/BSF support for handling JSON responses.

Suppose we have the following JSON response:

{ success: true, data:[ 1,2,3,4,5 ]  }

This string can be easily converted to a javascript object by using the eval function like this:

eval( 'var myObj = ' + prev.getResponseDataAsString() )

After the evaluation we can use the variable "myObj" as we wish and do things like accessing the success property:

log.info(myObj.success)

Or iterate over the list in the data property:

for (  var i = 0;  i < myObj.data.length; i++ ) {
     log.info(myObj.data[i])
}

We can also put the object in the implicit vars object so it can be used in another sampler, for example:

vars.putObject('myObj', myObj)

terça-feira, 22 de março de 2011

JMeter - POSTing / Sending JSON data

Recently I was load testing a RESTful mobile API where all the transmitted data were encoded as JSON. It took me some time to figure out how to simulate that in JMeter using a regular HTTPRequest sampler so I decided to share it with others.

NOTE: I successfuly used this technique for webservices exposed by the Jersey library as well as ASP.NET asmx webservices

It's quite simple. Assuming you need to send POST request to the /login URI passing two parameters: email and password encoded as JSON, like this:

{ email: 'myemail@email.com', password: 'mypassword' }

Just follow two basic steps:

Step 1 - Setting the "ContentType" http header attribute to "application/json"
  • Add an HTTP Header Manager to your test plan;
  • Add a new attribute: set name to "Content-Type" and value to "application/json"



Step 2 - Setting the JSON object as an unnamed http request parameter
  • Add a HttpRequest sampler
  • Add a new parameter, leave the name blank and set the value to the JSON string:  { 'email': 'myemail@email.com', 'password': 'mypassword' }

And last but not least: you can use variables and properties as you wish in the JSON string. Suppose you have a CSV Data Set Config which defines to external variables: email e pwd. You could use them in your sampler like this:

{ email: '${email}', password: '${pwd}' }