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, 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 Manager1 to the Thread Group2 in your Test Plan3.
Add the following HTTP Header:
- Header Name:
Accept-Encoding
- Header Value:
gzip,deflate,sdch
To verify:
- Add
View the Results Tree
Listener to the test plan, - Run your test plan,
- 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 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.
- Add an HTTP Header Manager1 with the following headers:
Content-Type: application/json
Content-Encoding: gzip
- Add JSR223 PreProcessor4 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.