性能工具之 nGrinder Post 请求脚本

简介: 【5月更文挑战第4天】性能工具之 nGrinder Post 请求脚本

一、前言

在实际压测工作中 Post 请求分为两种最常见传参情况:

  • 第一种是通过 key-value 方式传参数

    • head参数为:Content-Type:application/x-www-form-urlencoded
  • 第二种是通过 Json 方式传参数

    • head参数为:Content-Type:application/json

前置条件是大家源码部署成功的,这样方面咱们直接在源码的脚本位置添加咱们调试的脚本,下面咱们使用两种方式做例子分别介绍。

二、编写测试服务

在模拟请求的服务端的 SpringBoot 工程的 Controller 层添加如下代码:

@PostMapping("/findinfo")
@ResponseBody
public List<UserTable> findUserPost(UserTable userInfo) {
   
    List<UserTable> UserInfo = userService.findinfo(userInfo);
    return UserInfo;
}

如果上面代码看不明白请参考:

三、编写测试脚本

1、key-value 方式

参考代码:

import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.plugin.http.HTTPRequest
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import static net.grinder.script.Grinder.grinder
import static org.hamcrest.Matchers.is
// import static net.grinder.util.GrinderUtils.
* // You can use this if you're using nGrinder after 3.2.3
import static org.junit.Assert.assertThat

/**
 * @Title: PostDemo
 * @Description: post请求
 * @author 7DGroup
 * @date 2019/10/24 / 12:22
 */

@RunWith(GrinderRunner)
class PostDemo {
   
    public static GTest test
    public static HTTPRequest request
    public static NVPair[] headers = []
    public static NVPair[] params = []
    public static Cookie[] cookies = []


    @BeforeProcess
    public static void beforeProcess() {
   
        HTTPPluginControl.getConnectionDefaults().timeout = 6000
        test = new GTest(1, "localhost:8888")
        request = new HTTPRequest()

        // Set header datas
        List<NVPair> headerList = new ArrayList<NVPair>()

        //POST key/value格式的params
        headerList.add(new NVPair("Content-Type", "application/x-www-form-urlencoded"))
        headers = headerList.toArray()

        // Set param datas
        List<NVPair> paramList = new ArrayList<NVPair>()
        paramList.add(new NVPair("username", "600128"))
        params = paramList.toArray()
        grinder.logger.info("before process.");
    }


    @BeforeThread
    public void beforeThread() {
   
        test.record(this, "test")
        grinder.statistics.delayReports = true;
        grinder.logger.info("before thread.");
    }


    @Before
    public void before() {
   
        request.setHeaders(headers)
        cookies.each {
    CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
        grinder.logger.info("before thread. init headers and cookies");
    }


    @Test
    public void test() {
   
        HTTPResponse result = request.GET("http://localhost:8888/findinfo", params)
        def text = result.getText()
        grinder.logger.info(text)
        assertThat(result.statusCode, is(200))
    }
}

注意别忘记修改启动参数:

-javaagent:D:\maven\repository\net\sf\grinder\grinder-dcr-agent\3.9.1\grinder-dcr-agent-3.9.1.jar

image.png

点击运行:
image.png

运行结果:
image.png

2、Json 请求方式

在测试前,先模拟可以发送 Json 请求的服务端,在 Controler 层中增加一个方法并且使用可以解析 Json 方法的注解为:@RequestBody

具体代码为:

**
 * json请求
 * @param userInfo
 * @return
 */
@PostMapping("/findinfoJson")
@ResponseBody
public List<UserTable> findUserPostJson(@RequestBody UserTable userInfo) {
   
    List<UserTable> UserInfo = userService.findinfo(userInfo);
    return UserInfo;
}

通过上面代码调整即可接受 Json 格式的请求,以下通过源码编写脚本;

import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.plugin.http.HTTPRequest
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import static net.grinder.script.Grinder.grinder
import static org.hamcrest.Matchers.is
// import static net.grinder.util.GrinderUtils.*
// You can use this if you're using nGrinder after 3.2.3
import static org.junit.Assert.assertThat

/**
 * @Title: PostDemo
 * @Description: json请求
 * @author 7DGroup
 * @date 2019/10/24 / 12:22
 */

@RunWith(GrinderRunner)
class PostDemo {
   
    public static GTest test
    public static HTTPRequest request
    public static NVPair[] headers = []
    public static NVPair[] params = []
    public static Cookie[] cookies = []

    @BeforeProcess
    public static void beforeProcess() {
   
        HTTPPluginControl.getConnectionDefaults().timeout = 6000
        test = new GTest(1, "localhost:8888")
        request = new HTTPRequest()
        // Set header datas
        List<NVPair> headerList = new ArrayList<NVPair>()
        headerList.add(new NVPair("Content-Type", "application/json"))
        headers = headerList.toArray()
        grinder.logger.info("before process.");
    }


    @BeforeThread
    public void beforeThread() {
   
        test.record(this, "test")
        grinder.statistics.delayReports = true;
        grinder.logger.info("before thread.");
    }


    @Before
    public void before() {
   
        request.setHeaders(headers)
        cookies.each {
    CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
        grinder.logger.info("before thread. init headers and cookies");
    }


    @Test
    public void test() {
   
        String url = "http://localhost:8888/findinfoJson"
        String parambody = "{\"username\":\"600128\"}"
        HTTPResponse result = request.POST(url, parambody.getBytes())

        //显示结果
        def text = result.getText()
        grinder.logger.info(text)
        assertThat(result.statusCode, is(200))
    }
}

运行结果:
image.png

四、源码分析

通过点击方法的 post 请求可以看出源码中支持那些方式传参:

HTTPResponse result = request.POST(url, parambody.getBytes())

image.png

五、总结

注意 key-value与 Json 在传参的时候注意 Content-Type 的值就行。

源码地址:

目录
相关文章
|
2月前
|
编解码 测试技术 索引
性能工具之 Jmeter 使用 HTTP 请求编写 HLS 脚本
在我们简要介绍了 HLS 协议的基础知识,接下来我们详细介绍一种使用 Jmeter 编写压测 HLS 协议脚本的方法。
112 1
性能工具之 Jmeter 使用 HTTP 请求编写 HLS 脚本
|
2月前
|
JSON 测试技术 数据格式
性能工具之 Locust 工具 Get 与 Post 请求
【4月更文挑战第7天】性能工具之 Locust 工具 Get 与 Post 请求
44 1
|
1天前
|
Shell Python
`pytest-httpserver`是一个pytest插件,它允许你在测试期间启动一个轻量级的HTTP服务器,并模拟HTTP请求和响应。
`pytest-httpserver`是一个pytest插件,它允许你在测试期间启动一个轻量级的HTTP服务器,并模拟HTTP请求和响应。
18 4
|
2月前
|
JSON IDE Java
性能工具之 nGrinder 关联脚本编写
【5月更文挑战第5天】性能工具之 nGrinder 关联脚本编写
24 2
性能工具之 nGrinder 关联脚本编写
|
2月前
|
SQL JSON Java
性能工具之 nGrinder Get 请求脚本编写
【5月更文挑战第3天】性能工具之 nGrinder Get 请求脚本编写
36 8
|
8月前
|
Shell API Docker
ansible api执行远程脚本结果中文乱码报错
ansible api执行远程脚本结果中文乱码报错
|
10月前
|
JSON 数据格式
wrk post lua脚本取excel参数压力测试,判断接口性能
wrk post lua脚本取excel参数压力测试,判断接口性能
177 0
|
12月前
|
前端开发 JavaScript 关系型数据库
宝塔设置PHP定时任务实战记录(定时任务、ajax异步刷新API、shell脚本、访问url)
宝塔设置PHP定时任务实战记录(定时任务、ajax异步刷新API、shell脚本、访问url)
580 0
|
域名解析 运维 网络协议
如何使用cURL获得请求和响应时间?
今天给大家分享一个干货编程小技巧,上至架构师、下至开发者、运维男、QA, 得此利器,事半功倍。
|
应用服务中间件 nginx C语言
Nginx集成Lua实现根据POST请求报文内容自定义负载策略
上游服务调用下游服务的接口,部分接口业务高峰期请求量大,下游服务器压力很大,会影响到其它接口的访问。如果通过增加下游服务器横向扩容会增加成本,且在业务高峰期还是有可能影响其他接口。所以需要使用Lua配置一种可以根据报文内容进行负载的策略(调用接口的URL是固定的,下游服务通过解析报文调用对应接口)。
567 0