---
title: Enable Gzip Compression In JMeter
url: https://blog.octoperf.com/enable-gzip-compression-in-jmeter/
date: '2017-10-24'
authors:
- jloisel
categories:
- JMeter
tags:
- Design
- Performance Testing
- HTTP
- JSR223
description: How to enable Gzip compression in JMeter to allow requesting Gzipped content, using HTTP Header Managers and JSR223 Pre Processors.
---

# Enable Gzip Compression In JMeter

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](https://en.wikipedia.org/wiki/HTTP_compression), the client advertises itself as capable of handling various compression formats:

```http
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
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[^1]** to the Thread Group[^2] in your Test Plan[^3].  

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](https://blog.octoperf.com/img/blog/enable-gzip-compression-jmeter/jmeter-gzip-enabled.png)

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:

```http
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.

- Add an HTTP Header Manager[^1] with the following headers:

```http
Content-Type: application/json
Content-Encoding: gzip
```

 - Add JSR223 PreProcessor[^4] as a child of the request which needs to be encoded, and define the following script:

```java
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.

[^1]: https://jmeter.apache.org/usermanual/component_reference.html#HTTP_Header_Manager
[^2]: https://jmeter.apache.org/usermanual/test_plan.html#thread_group
[^3]: https://jmeter.apache.org/usermanual/build-test-plan.html
[^4]: https://jmeter.apache.org/usermanual/component_reference.html#JSR223_PreProcessor
