首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >性能工具之Ngrinder之Post请求脚本编写介绍

性能工具之Ngrinder之Post请求脚本编写介绍

作者头像
高楼Zee
发布2019-10-29 16:26:47
1.2K0
发布2019-10-29 16:26:47
举报
文章被收录于专栏:7DGroup7DGroup

原文作者:李文

背景:

官方网站为:http://naver.github.io/ngrinder/

在实际压测工作中psot请求分为两种最常见传参情况,以下分别介绍这两种脚本编写:

  • 第一种是通过key-->value方式传参数,head参数为:Content-Type:application/x-www-form-urlencoded
  • 第二种是通过json方式传参数,head参数为:Content-Type:application/json

咱们开启post脚本之旅

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

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

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

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

性能工具之Ngrinder之Get请求脚本编写

1、post请求方式

参考代码

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 liwen
 * @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

点击运行

结果为:

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格式的请求,以下通过源码编写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 liwen
 * @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))
    }
}

点击请求参考结果:

显示结果:

源码分析说明

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

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

通过源码就知道post请求怎么参数化,感兴趣的朋友可以打开源码玩一玩;

下次分享通过外部文件获取参数与脚本之间怎么关联,为最后做实战做基础工作。

总结:注意k-v与josn在传参的时候注意Content-Type的值就行;

送大家一句:

当我们遇到别人,问我们问题的时候,我们可以回答,“不知道”,但是“不知道”,可以是解决问题的开始,也可以是问题的,结束心态,智者不是全知,而是选择开始。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-10-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 7DGroup 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景:
    • 咱们开启post脚本之旅
      • 1、post请求方式
        • 结果为:
      • 2、json请求方式
        • 参考代码
        • 显示结果:
        • 源码分析说明
        • 总结:注意k-v与josn在传参的时候注意Content-Type的值就行;
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档