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

2019-11-03 14:10:11 浏览数 (2)

背景:

在做性能测试,脚本参数化是一个比较好玩的事情,不同工具参数写法不一样,简单可以从三个方面(随机、唯一,顺序)获取参数进行脚本参数化;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中调试

结果为:

说明:

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

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

0 人点赞