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:
- 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:
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.
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:
The last line it’s my problem!!! Jmeter version 3.1
Thanks in advance.
In reply to Daniel
Hi Daniel,
I suggest you to:
sampler.getArguments().getArgument(0).setValue(out.toString(0));
in order to convert the byte stream to string body.Also, consider:
5.0
is available): JMeter3.1
is an older version,Best Regards,