如何使用 Apache JMeter POST GZip 请求
How to POST GZip Request with Apache JMeter
我对使用 Apach JMeter 有疑问。
我们的项目,Android 应用 post json 数据与 "Gzip Compression" 到 API 服务器。
Android 应用程序使用 "Apache HttpClient" 并且它是 "GzipCompressingEntity" class。
为了API 服务器的性能测试,我尝试通过JMeter 的Proxy (="HTTP(S) Test Script Recorder") 记录请求。
但是记录的请求body是空的。
我想要做的是将 "Gziped HTTP request data" 从 Apache JMeter 发送到服务器。
有什么办法吗?
以下是我们 Android 应用请求 header.
的示例
POST /api/test HTTP/1.1
Content-Type: application/json
Transfer-Encoding: chunked
Content-Encoding: gzip
Host: 192.168.11.11:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.5)
Accept-Encoding: gzip,deflate
RequestBody 是 Gziped 二进制数据。
我所做的是
- 运行 JMeter 上的 "HTTP(S) Test Script Recorder(Proxy Server)"。
- 设置 Android 的 http 代理以使用该代理服务器。
- 执行HTTP客户端的Android测试代码(post数据经过Gzip压缩)。
然后,该测试代码以失败告终。 (服务器无响应)
如果直接访问(没有JMeter的Proxy Server),则测试成功。
我可以像这样从JMeter发送压缩请求数据吗?
将 HTTP Header Manager 添加到您的测试计划中并至少添加以下内容 headers:
- Content-Type: application/json
- Content-Encoding: gzip
添加 Beanshell PreProcessor 作为您需要编码的请求的 child 并将以下代码添加到它的 "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));
它会收到你的请求 body,压缩它并即时替换,这样请求就会被 gzip 压缩。
编码错误,需要这样保存在body中
sampler.getArguments().getArgument(0).setValue(new String(compressedBody, 0));
我对使用 Apach JMeter 有疑问。
我们的项目,Android 应用 post json 数据与 "Gzip Compression" 到 API 服务器。 Android 应用程序使用 "Apache HttpClient" 并且它是 "GzipCompressingEntity" class。
为了API 服务器的性能测试,我尝试通过JMeter 的Proxy (="HTTP(S) Test Script Recorder") 记录请求。 但是记录的请求body是空的。
我想要做的是将 "Gziped HTTP request data" 从 Apache JMeter 发送到服务器。 有什么办法吗?
以下是我们 Android 应用请求 header.
的示例POST /api/test HTTP/1.1
Content-Type: application/json
Transfer-Encoding: chunked
Content-Encoding: gzip
Host: 192.168.11.11:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.5)
Accept-Encoding: gzip,deflate
RequestBody 是 Gziped 二进制数据。
我所做的是
- 运行 JMeter 上的 "HTTP(S) Test Script Recorder(Proxy Server)"。
- 设置 Android 的 http 代理以使用该代理服务器。
- 执行HTTP客户端的Android测试代码(post数据经过Gzip压缩)。
然后,该测试代码以失败告终。 (服务器无响应)
如果直接访问(没有JMeter的Proxy Server),则测试成功。 我可以像这样从JMeter发送压缩请求数据吗?
将 HTTP Header Manager 添加到您的测试计划中并至少添加以下内容 headers:
- Content-Type: application/json
- Content-Encoding: gzip
添加 Beanshell PreProcessor 作为您需要编码的请求的 child 并将以下代码添加到它的 "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));
它会收到你的请求 body,压缩它并即时替换,这样请求就会被 gzip 压缩。
编码错误,需要这样保存在body中
sampler.getArguments().getArgument(0).setValue(new String(compressedBody, 0));