Apache JMeter 测试 HTTP接口
原创
©著作权归作者所有:来自51CTO博客作者gblfy的原创作品,请联系作者获取转载授权,否则将追究法律责任
一、设置基础组件
1. Apache JMeter 测试 HTTP接口流程
原则:简单的http请求而已,例如:PostMan测试一样的
1.添加一个线程组
2给线程组添加一个取样器,用来发送HTTP请求
3.给线程组添加一个监听器(观察结果树),用来查看具体的请求和响应信息
4.给线程组添加一个监听器(聚合报告),用来查看具体的请求和响应信息时间等信息
2. 添加一个线程组
【Test Plan】-【添加】-【线程用户(用户)】-【线程组】
2. HTTP信息头管理器
【Thread Group】-【添加】-【配置元件】-【HTTP信息头管理器】
3. 添加HTTP请求
【Thread Group】-【添加】-【取样器】-【HTTP请求】
4. 添加一个察看结果树
【Thread Group】-【添加】-【监听器】-【察看结果树】
5. 添加一个聚合报告
【Thread Group】-【添加】-【监听器】-【聚合报告】
二、配置基础组件
2.1. 服务端代码
package com.gblfy.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 模拟JMeter 测试Http接口
*/
@Controller
@Slf4j
public class TestHttpInterface {
@RequestMapping(value = { "/httpService" }, method = RequestMethod.POST, produces = "application/json;charset=UTF-8;")
@ResponseBody
public void recHttpReqJsonData(HttpServletRequest paramRequest, HttpServletResponse paramResponse,
@RequestBody String paramRequestBody) throws Exception {
log.info("HttpServletRequest {}",paramRequest);
log.info("请求报文体 {}",paramRequestBody);
log.info("HttpServletResponse {}",paramResponse);
paramResponse.setHeader("Content-type", "application/json;charset=UTF-8");
String jsonStrReq = "模拟返回响应报文+测试中文乱码问题";
paramResponse.getWriter().write(jsonStrReq);
}
}
2.2. 设置http头信息
注:设置Content-Type=application/json;charset=UTF-8
是因为服务端设置了接收报文的编码格式,这是需求中给出的
2.3. 设置http请求参数
说明
| 参数
|
1.ip地址
| 127.0.0.1
|
2.端口
| 8080
|
3.请求地址url
| /httpService
|
4.编码格式
| utf-8
|
5.发送的请求的报文体
| 见下面
|
{
"insuranceId": 1,
"ServiceName": 1,
"productName": "测试中文乱码",
"description": "模拟纽约发送请求报文",
"技术网站": "gblfy"
}
2.4. 设置请求规则参数
2.5. 发送http请求
三、测试验证
3.1. 服务端验证
3.2. 查看结果树
请求header
请求体(报文)
请求header
响应体(返回报文)
3.3. 查看聚合报告