性能测试工具-Ngrinder使用-添加一个简单脚本(groovy)

 

Ngrinder支持2种语言:groovy,python。

Groovy 代码能够与 Java 代码很好地结合,so以下记录都是使用groovy。有兴趣可以了解groovy语言:https://www.w3cschool.cn/groovy/

一.创建脚本

1.在Ngrinder首页,点击脚本进入脚本添加/编写页面

2.点击新建脚本,类型选择Groovy,被测试URL按需求填写,也可以不写,后期在代码中修改,以下做个简单请求,如图:

 

3.点击创建后,自动生成代码结构,如图

package org.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 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)// 每个测试类加这注解
class TestRunner {
	public static GTest test
	public static HTTPRequest request

	@BeforeProcess// 在每个进程启动前执行
	public static void beforeProcess() {
		HTTPPluginControl.getConnectionDefaults().timeout = 6000
		test = new GTest(1, "我的博客")
		request = new HTTPRequest()
		test.record(request);
		grinder.logger.info("before process.");
	}

	@BeforeThread // 在每个线程执行前执行
	public void beforeThread() {
		grinder.statistics.delayReports=true;
		grinder.logger.info("before thread.");
	}
        @Before // 在每个 @Test 注解的方法执行前执行 

public void before() {

      // 设置变量、多个 @Test 方法共用的逻辑等

    }
@Test public void test(){ HTTPResponse result = request.GET("https://www.cnblogs.com/xiaowei89426/p/9356694.html") 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)); } } }

  点击右上角->验证脚本,返回200,无报错,即请求成功:

至此一个简单的脚本成功

 

 

posted on 2018-07-23 20:25  冰蓝小猪宝宝  阅读(2294)  评论(1编辑  收藏  举报

导航