性能測試工具-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,無報錯,即請求成功:

至此一個簡單的腳本成功

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM