JMeter - how to log the full request for a failed response?

The View Results Tree component shows a tree of all sample responses, allowing you to view both the request and response for any sample.

When load testing (Always in NON GUI mode), fill in "Filename" field and select to only save Responses in Error:

View Results Tree in error

As you can see above we clicked on Configure to select all fields except CSV ones.

You can also save the entire response to a file using Save Responses to a file:

Save Responses to a file


This is how I log the full request (request URL + request body) for failed requests.

  1. Add a Listener inside the Thread Group
try{
  var message = "";
  var currentUrl = sampler.getUrl();
  message +=  ". URL = " +currentUrl;
  var requestBody = sampler.getArguments().getArgument(0).getValue();
  message += " --data " + sampler.getArguments();

  if(!sampleResult.isSuccessful()){
      log.error(message);
  }

}catch(err){
  //do nothing. this could be a debug sampler. no need to log the error
}

For every Sampler inside the Thread Group, the Listener will execute this code after the Sampler.


I found this thread searching for a solution to log the response only when a sampler fails, so the accepted solution is not good for me. I have occasional sample failures at a very high load involving hundreds of thousands of samples, so a tree listener is completely impractical for me (it will reach several gigabytes in size), so here is what I came up with (which should be good for the OP's scenario as well):

Add a [JSR223 Assertion][1] (should come after all the other assertions) and put the below code in it:

if (Boolean.valueOf(vars.get("DEBUG"))) {
  for (a: SampleResult.getAssertionResults()) {
    if (a.isError() || a.isFailure()) {
      log.error(Thread.currentThread().getName()+": "+SampleLabel+": Assertion failed for response: " + new String((byte[]) ResponseData));
    }
  }
}

This will cause the entire response getting logged to the jmeter log file which is fine in my case, as I know that the responses are really small, but for large responses, more intelligent processing could be done.


There is a 'Save responses to a file' listener, which can save to file only when error occurs.

Tags:

Jmeter