Enable Gzip Compression in JMeter

Enable Gzip Compression in JMeter

Jmeter   2 Amazing Comments

You’re probably asking yourself: How can I enable Compression support in JMeter?

JMeter can use up to 10x more bandwidth when compression is disabled.

Learn how to configure JMeter to support Request and Response Compression like GZip.

HTTP Compression

As explained on HTTP Compression, the client advertises itself as capable of handling various compression formats:

GET /encrypted-area HTTP/1.1
Host: www.example.com
Accept-Encoding: gzip, deflate

The server should then respond with the Content-Encoding: gzip header:

HTTP/1.1 200 OK
Server: Apache/1.3.3.7 (Unix)  (Red-Hat/Linux)
Accept-Ranges: bytes
Content-Length: 438
Connection: close
Content-Type: text/html; charset=UTF-8
Content-Encoding: gzip

Let’s see how we can enable HTTP compression in JMeter with minimal fuss.

Accept Gzipped Responses

Add an HTTP Header Manager to the Thread Group in your Test Plan.

Add the following HTTP Header:

  • Header Name: Accept-Encoding
  • Header Value: gzip,deflate,sdch

To verify:

  1. Add View the Results Tree Listener to the test plan,
  2. Run your test plan,
  3. View the Sampler result tab for one of the http request.

You should see the same as above, the server should be responding with a gzipped response. Another way to verify is in the Summary Report stats:

  • Avg Bytes shows the uncompressed size,
  • KB/sec shows the compressed downloaded size, which should be 6x-10x smaller than uncompressed.

JMeter Gzip Enabled

JMeter will automatically uncompress gzipped responses and show uncompressed response data.

Send Gzipped Requests

The previous solution shows how to enable the server to send Gzipped responses. But, it can also be enabled on the client side, to send gzipped request. The following http headers show a sample gzipped http request:

POST /api/test HTTP/1.1
Content-Type: application/json
Transfer-Encoding: chunked
Content-Encoding: gzip
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.5)
Accept-Encoding: gzip,deflate

The following procedure explains how to send Gzipped Post Requests using JMeter.

Content-Type: application/json
Content-Encoding: gzip
  • Add JSR223 PreProcessor as a child of the request which needs to be encoded, and define the following script:
import org.apache.commons.io.IOUtils;
import java.util.zip.GZIPOutputStream;


String bodyString = sampler.getArguments().getArgument(0).getValue();
byte [] requestBody = bodyString.getBytes();

ByteArrayOutputStream out = new ByteArrayOutputStream(requestBody.length);
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(requestBody);
gzip.close();

sampler.getArguments().getArgument(0).setValue(out.toString(0));

This scripts replaces the body content by gzipping it.

By - CTO.
Tags: Design Performance Http Jsr223 Server

Comments

Daniel  

Hello Jérôme! Is it also possible to send a gzipped soap request?

I added a BeanShell PreProcessor as a child of the request which needs to be encoded, and I defined the following script:

import org.apache.commons.io.IOUtils;
import java.util.zip.GZIPOutputStream;

// String bodyString = sampler.getArguments().getArgument(0).getValue();
String bodyString = ctx.getCurrentSampler().getXmlData();

byte [] requestBody = bodyString.getBytes();

ByteArrayOutputStream out = new ByteArrayOutputStream(requestBody.length);
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(requestBody);
gzip.close();

//sampler.getArguments().getArgument(0).setValue(out.toString(0));
ctx.getCurrentSampler().setXmlData(???);

The last line it’s my problem!!! Jmeter version 3.1

Thanks in advance.

Reply

Jerome
In reply to Daniel
 

Hi Daniel,

I suggest you to:

  • Replace deprecated SOAP/XML-RPC Sampler with the HTTP Request,
  • And us sampler.getArguments().getArgument(0).setValue(out.toString(0)); in order to convert the byte stream to string body.

Also, consider:

  • Upgrading to latest JMeter version (as of now, 5.0 is available): JMeter 3.1 is an older version,
  • And use Groovy instead of Beanshell for better performance.

Best Regards,

 

Thank you

Your comment has been submitted and will be published once it has been approved.

OK

OOPS!

Your post has failed. Please return to the page and try again. Thank You!

OK