ngrinder解读goovy最基本的GET请求脚本

此文搬运自本人csdn博客 https://blog.csdn.net/lijing7...

上一篇文章中讲了ngrinder怎么快速发送一个GET请求,在此详细解读一下其中的脚本。
(前提是你已经了解了groovy的基本代码结构,如果还不了解的先看这里--搭建 nGrinder 性能测试平台 并快速使用

一、自动生成GET请求脚本

1、配置 Create a script

在ngrinder管理台主页,点击script-->Create a script,并填写脚本名称和请求的url,如下所示:

clipboard.png

点击 Create 按钮,nGrinder会自动生成对应的脚本结构,如果没有参数需要设置的话,可以直接运行了。

二、详细解决GET请求脚本

ngrinder自动生成的脚本如下所示:

解析见代码中的注释

import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
import net.grinder.plugin.http.HTTPRequest
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.script.GTest
import net.grinder.script.Grinder
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import 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.3
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.FixMethodOrder
import org.junit.runners.MethodSorters

import java.util.Date
import java.util.List
import java.util.ArrayList

import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import 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 admin
 */
@RunWith(GrinderRunner)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class TestRunner {

    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() {
        // 设置请求响应超时时间(ms),超过则抛出异常
        HTTPPluginControl.getConnectionDefaults().timeout = 6000
        
        // 创建GTest对象,第一个参数1代表有多个请求/事务时的执行顺序ID
        // 第二个参数是请求/事务的名称,会显示在summary结果中
        // 有多个请求/事务时,要创建多个GTest对象
        test = new GTest(1, "www.baidu.com")  
        request = new HTTPRequest()
        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(){
        // 发送GET请求
        HTTPResponse result = request.GET("https://www.baidu.com", params)
        // 断言HTTP请求状态码
        assertThat(result.statusCode, is(200))
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值