一、设置基础组件

1. Apache JMeter 测试 HTTP接口流程

原则:简单的http请求而已,例如:PostMan测试一样的
1.添加一个线程组
2给线程组添加一个取样器,用来发送HTTP请求
3.给线程组添加一个监听器(观察结果树),用来查看具体的请求和响应信息
4.给线程组添加一个监听器(聚合报告),用来查看具体的请求和响应信息时间等信息

2. 添加一个线程组

【Test Plan】-【添加】-【线程用户(用户)】-【线程组】

Apache JMeter 测试 HTTP接口_json

2. HTTP信息头管理器

【Thread Group】-【添加】-【配置元件】-【HTTP信息头管理器】

Apache JMeter 测试 HTTP接口_线程组_02

3. 添加HTTP请求

【Thread Group】-【添加】-【取样器】-【HTTP请求】

Apache JMeter 测试 HTTP接口_json_03

4. 添加一个察看结果树

【Thread Group】-【添加】-【监听器】-【察看结果树】

Apache JMeter 测试 HTTP接口_json_04

5. 添加一个聚合报告

【Thread Group】-【添加】-【监听器】-【聚合报告】

Apache JMeter 测试 HTTP接口_线程组_05

二、配置基础组件

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头信息

Apache JMeter 测试 HTTP接口_json_06


注:设置​​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"
}

Apache JMeter 测试 HTTP接口_spring_07

2.4. 设置请求规则参数

Apache JMeter 测试 HTTP接口_spring_08

2.5. 发送http请求

Apache JMeter 测试 HTTP接口_线程组_09

三、测试验证

3.1. 服务端验证

Apache JMeter 测试 HTTP接口_线程组_10

3.2. 查看结果树

请求header

Apache JMeter 测试 HTTP接口_JMeter_11


请求体(报文)

Apache JMeter 测试 HTTP接口_线程组_12


请求header

Apache JMeter 测试 HTTP接口_线程组_13


响应体(返回报文)

Apache JMeter 测试 HTTP接口_json_14

3.3. 查看聚合报告

Apache JMeter 测试 HTTP接口_JMeter_15