How to enable Gzip compression in JMeter to allow requesting Gzipped content, using HTTP Header Managers and JSR223 Pre Processors. design , performance , http , jsr223 , server https://octoperf.com/blog/2017/10/24/enable-gzip-compression-jmeter/ OctoPerf ZI Les Paluds, 276 Avenue du Douard, 13400 Aubagne, France +334 42 84 12 59 contact@octoperf.com Jmeter 382 2021-01-04

Enable Gzip Compression In JMeter

OctoPerf is JMeter on steroids!
Schedule a Demo

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:

1
2
3
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:

1
2
3
4
5
6
7
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:

1
2
3
4
5
6
7
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.

1
2
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:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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.

Enable Gzip Compression In JMeter
Tags:
Share:

Comments (2)

  • 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:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    
    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

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

Want to become a super load tester?
OctoPerf Superman