如何使用 Apache JMeter 发布 GZip 请求 [英] How to POST GZip Request with Apache JMeter

查看:40
本文介绍了如何使用 Apache JMeter 发布 GZip 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于使用 Apache JMeter 的问题.

I have a question for using Apach JMeter.

我们的项目,Android 应用程序将带有Gzip 压缩"的 json 数据发布到 API 服务器.使用Apache HttpClient"及其GzipCompressingEntity"类的 Android 应用程序.

Our project, Android apps post json data with "Gzip Compression" to API server. The Android apps using "Apache HttpClient" and it's "GzipCompressingEntity" class.

对于 API 服务器的性能测试,我尝试通过 JMeter 的代理 (="HTTP(S) 测试脚本记录器") 记录请求.但是记录的请求正文是空的.

For performance testing of API server, I tryed to recording the request by JMeter's Proxy (="HTTP(S) Test Script Recorder"). But the recorded request body was empty.

我想要做的是从 Apache JMeter 向服务器发送Gziped HTTP 请求数据".有什么办法吗?

What I want to do is to send "Gziped HTTP request data" from Apache JMeter to server. Is there any way for that?

以下是我们 Android 应用的请求标头示例.

Following is sample of our Android app's request 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 是经过压缩的二进制数据.

我所做的是

  1. 在 JMeter 上运行HTTP(S) 测试脚本记录器(代理服务器)".
  2. 设置 Android 的 http 代理以使用该代理服务器.
  3. 执行HTTP Client的Android测试代码(post数据经过Gzip压缩).

然后,该测试代码以失败告终.(服务器无响应)

then, that test code finished with failure. (no response from server)

如果直接访问(没有 JMeter 的代理服务器),则测试成功.我可以像这样从JMeter发送压缩的请求数据吗?

If access directly (without JMeter's Proxy Server), that test succeeded. Can I send the compressed request data from JMeter just like this?

推荐答案

  1. HTTP Header Manager 添加到您的测试计划中,然后至少添加以下标题:

  1. Add HTTP Header Manager to your test plan and add at least the following headers:

  • 内容类型:应用程序/json
  • 内容编码:gzip

添加 Beanshell PreProcessor 作为您请求的子项需要编码并将以下代码添加到它的脚本"区域:

Add Beanshell PreProcessor as a child of the request which you need to encode and add the following code to it's "Script" area:

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));

它将获取您的请求正文,对其进行压缩并动态替换,以便对请求进行 gzip 压缩.

It will get your request body, compress it and substitute on the fly so the request will be gzipped.

这篇关于如何使用 Apache JMeter 发布 GZip 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆