前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >性能工具之nGrinder参数化脚本编写简单介绍

性能工具之nGrinder参数化脚本编写简单介绍

作者头像
高楼Zee
发布2019-11-03 14:10:11
8870
发布2019-11-03 14:10:11
举报
文章被收录于专栏:7DGroup7DGroup

背景:

在做性能测试,脚本参数化是一个比较好玩的事情,不同工具参数写法不一样,简单可以从三个方面(随机、唯一,顺序)获取参数进行脚本参数化;nGrinder参数化需要一点代码基础才可以实现。

nGrinder平台参数写法简单介绍如:

点击脚本,选择需要参数化的脚本:

新建文件为:resources,并且在该目录下上传参数文件如:

新建脚本:

把相关内容输入进入:

如果有头信息等信息,点击高级即可看到:

参考脚本

代码语言:javascript
复制
import static net.grinder.script.Grinder.grinderimport static org.junit.Assert.*import static org.hamcrest.Matchers.*import net.grinder.plugin.http.HTTPRequestimport net.grinder.plugin.http.HTTPPluginControlimport net.grinder.script.GTestimport net.grinder.script.Grinderimport net.grinder.scriptengine.groovy.junit.GrinderRunnerimport net.grinder.scriptengine.groovy.junit.annotation.BeforeProcessimport net.grinder.scriptengine.groovy.junit.annotation.BeforeThread// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3import org.junit.Beforeimport org.junit.BeforeClassimport org.junit.Testimport org.junit.runner.RunWith
import java.util.Dateimport java.util.Listimport java.util.ArrayList
import HTTPClient.Cookieimport HTTPClient.CookieModuleimport HTTPClient.HTTPResponseimport HTTPClient.NVPair
/** * A simple example using the HTTP plugin that shows the retrieval of a * single page via HTTP. * * This script is automatically generated by ngrinder. * @author liwen
* @Title: Msg
* @Description: 脚本参数化
* @date 2019/10/28 / 22:07 */@RunWith(GrinderRunner)class TestRunner {
    public static GTest test    public static HTTPRequest request    public static NVPair[] headers = []    public static NVPair[] params = []    public static Cookie[] cookies = []
        ////存放参数文件记录    public static lineList = List    //参数行    public static def rowNumber
    @BeforeProcess    public static void beforeProcess() {        HTTPPluginControl.getConnectionDefaults().timeout = 6000        test = new GTest(1, "localhost")        request = new HTTPRequest()
         List<NVPair> headerList = new ArrayList<NVPair>()        headerList.add(new NVPair("Content-Type", "application/x-www-form-urlencoded"))        headerList.add(new NVPair("Transfer-Encoding", "chunked"))        headerList.add(new NVPair("Content-Type", "application/json;charset=utf-8"))        headers = headerList.toArray()
        lineList = new File("./resources/parm.txt").readLines()
        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(){        rowNumber = new Random().nextInt(lineList.size())        String name = lineList.get(rowNumber).toString()        List<NVPair> paramList = new ArrayList<NVPair>()        paramList.add(new NVPair("userName", name))        params = paramList.toArray()
        HTTPResponse result = request.GET("http://localhost:8888/userfind", params)
        if (result.statusCode == 301 || result.statusCode == 302) {            grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode);         } else {            assertThat(result.statusCode, is(200));        }    }}

调试结果为:

在idea中调试

结果为:

说明:

如果是源码部署可以在如图位置新建相关目录与脚本,把上面脚本参数路径修改下即可跑起来:

上面是简单的参数化写法,如果想了解更多的写法请看源码或者查找资料,因为源码大家已经部署成功,知识网络上很多,可以灵活使用即可掌握写法。

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景:
    • nGrinder平台参数写法简单介绍如:
      • 参考脚本
        • 调试结果为:
          • 在idea中调试
            • 说明:
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档