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

性能工具之nGrinder关联脚本编写简单介绍

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

背景:

在做性能测试,脚本之间的关联是一个比较棘手的问题,nGrinder脚本是怎么关联,其实也是比较简单的,简单前提条件是自己具备一定的知识,也就是需要代码基础、http协议知识、网络知识等这些基础知识,这样才能开展工作。

常见的获取请求结果方法有:

  1. 通过正则表达方式获取结果;
  2. 通过xpath方式获取相关结果;
  3. 通过JSON解析获取相关结果

关联介绍

  • 关联的目的是后面请求需要,如果不需要就不需要关联。
  • 关联获取结果做断言

想了解更多、更详细关联知识请查找相关资料。

在编写nGrinder脚本之前请学习下groovy语法这样方便写脚本,脚本编写建议在idea中上写脚本与调试脚本,这样有语法提示能很快写出脚本与调试脚本,写完脚本后直接复制到线上脚本中在微调验证就能使用。

脚本编写简单演示

本次脚本编写与调试需要解析JSON所以需要上传fastjson-1.2.62.jar用例解析JSON脚本,下载地址为:

https://mvnrepository.com/artifact/com.alibaba/fastjson

代码语言:javascript
复制
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.62</version>
</dependency>

1、如果是线上直接写脚本需要上传jar:

在脚本页面的脚本文件夹中新建lib文件夹,之后再lib文件中上传相关的jar包,如下图:

点击文件夹:

注意:一定在脚本文件相关的地方新建lib文件夹,并且在lib下中上传jar如:

2、如果是idea中写代码与调试脚本,需要在脚本文件中新建lib文件夹之后在把jar包加入工程中去如:

相关工程:https://github.com/357712148/nGrinder/blob/master/script-sample/test-with-login/OneAndTwo.groovy

工程下载地址:

https://github.com/357712148/nGrinder.git

该工程下载后需要处理下才可以使用:

点击

再次点击:

选择脚本工程

再次选择:

上面操作后即可实现代码与调试脚本,如果还是有问题,自己微调即可。

idea中调试并且测试

线上调试:

代码示例

代码语言:javascript
复制
    import HTTPClient.Cookie
    import HTTPClient.CookieModule
    import HTTPClient.HTTPResponse
    import HTTPClient.NVPair
    import com.alibaba.fastjson.JSONArray
    import groovy.json.JsonParser
    import groovy.json.JsonSlurper
    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 com.alibaba.fastjson.JSONObject
    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: OneAndTwo* @Description: this is
     * @author liwen* @date 2019/10/27 / 11:00
     */
    @RunWith(GrinderRunner)
    class OneAndTwo {
        public static GTest test
        // 定义 HTTPRequest 静态变量 request,用于发送 HTTP 请求
        public static HTTPRequest request
        // 定义 NVPair 数组 headers ,用于存放通用的请求头数据
        public static NVPair[] headers = []
        // 定义 NVPair 数组 params ,用于存放请求参数数据
        public static NVPair[] params = []
        // 定义 Cookie 数组 cookies ,用于存放通用的 cookie 数据
        public static Cookie[] cookies = []
        //存储第一个请求得参数
        def paramName = new ArrayList()
        @BeforeProcess
        public static void beforeProcess() {
            // 设置请求响应超时时间(ms)
            HTTPPluginControl.getConnectionDefaults().timeout = 6000
            // 创建GTest对象,第一个参数1代表有多个请求/事务时的执行顺序ID,
            // 第二个参数是请求/事务的名称,会显示在summary结果中,有多个请求/事务时,要创建多个GTest对象
            test = new GTest(1, "User_find_01")
            //创建 HTTPRequest 对象,用于发起 HTTP 请求
            request = new HTTPRequest()
            // Set header datas
            List<NVPair> headerList = new ArrayList<NVPair>()
            headerList.add(new NVPair("Content-Type", "application/x-www-form-urlencoded"))
            headerList.add(new NVPair("Connection", "keep-alive"))
            headers = headerList.toArray()
            // Set param datas
    //        List<Cookie> cookieList = new ArrayList<Cookie>()
    //        cookieList.add(new Cookie("Cookie", "null", "localhost:8888", "", new Date(), true))
    //        cookies = cookieList.toArray()
            grinder.logger.info("before process.");
        }
        @BeforeThread
        public void beforeThread() {
    //        注册事件,启动test,第二个参数要与@Test注解的方法名保持一致,有多个请求/事务时,要注册多个事件
            test.record(this, "test")
            //配置延迟报告统计结果
            grinder.statistics.delayReports = true;
            grinder.logger.info("before thread.");
        }
        @Before
        public void before() {
            //在这里可以添加headers属性和cookies
    //        request.setHeaders(headers)
            cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
            grinder.logger.info("before thread. init headers and cookies");
        }
        @Test
        public void test() {
            getUserFind()
            getItem()
        }
        public void getUserFind() {
            // 发送GET请求
            HTTPResponse resu = request.GET("http://localhost:8888/userfind")
            def text1 = resu.getText()
            JSONObject jsonObject = JSONObject.parseObject(text1);
            JSONObject object = jsonObject.getJSONObject("extend")
            JSONArray array = object.getJSONArray("info")
            for (int i = 0; i < array.size(); i++) {
                JSONObject object1 = JSONObject.parseObject(array.get(i).toString())
                Object name = object1.get("userName");
                paramName.add(String.valueOf(name))
            }
            assertThat(resu.statusCode, is(200))
        }
        public void getItem() {
            List<NVPair> paramList = new ArrayList<NVPair>()
            //获取参数的第一个值
            paramList.add(new NVPair("userName", paramName.get(0)))
            params = paramList.toArray()
            // Set cookie datas
            HTTPResponse result = request.GET("http://localhost:8888/findName", params)
            def text = result.getText()
            grinder.logger.info("这是第二请求" + text)
            // 断言HTTP请求状态码
            assertThat(result.statusCode, is(200))
        }
    }

结果显示:

这是相应的测试工程代码下载地址:

https://github.com/357712148/bodygit.git

代码截图:

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景:
    • 常见的获取请求结果方法有:
      • 关联介绍
        • 脚本编写简单演示
          • 该工程下载后需要处理下才可以使用:
            • idea中调试并且测试
              • 代码示例
            • 结果显示:
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档