性能工具之nGrinder參數化腳本編寫簡單介紹
- 2019 年 11 月 3 日
- 筆記
背景:
在做性能測試,腳本參數化是一個比較好玩的事情,不同工具參數寫法不一樣,簡單可以從三個方面(隨機、唯一,順序)獲取參數進行腳本參數化;nGrinder參數化需要一點程式碼基礎才可以實現。
nGrinder平台參數寫法簡單介紹如:
點擊腳本,選擇需要參數化的腳本:

新建文件為:resources,並且在該目錄下上傳參數文件如:

新建腳本:

把相關內容輸入進入:

如果有頭資訊等資訊,點擊高級即可看到:

參考腳本
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中調試

結果為:

說明:
如果是源碼部署可以在如圖位置新建相關目錄與腳本,把上面腳本參數路徑修改下即可跑起來:

上面是簡單的參數化寫法,如果想了解更多的寫法請看源碼或者查找資料,因為源碼大家已經部署成功,知識網路上很多,可以靈活使用即可掌握寫法。