京寵展信息指南
682
2025-03-31
前言
實現一個get 請求 demo
腳本編寫
前言
實現一個get 請求 demo
腳本編寫
前言
做性能測試腳本是一個實際下功夫的地方,工作中常見也就 是 key-value,json 方式比較多,那么 nGrinder 腳本咱們怎么編寫以下簡單介紹。
實現一個get 請求 demo
首先,通過 SpringBoot 編寫一個工程實現增刪改查,通過 Get 請求獲取:
http://localhost:8888/findinfo?username=600128
該工程 controller 層中用最簡單 Get 請求查詢數據,該代碼為:
@GetMapping("/findinfo") @ResponseBody public List
@ResponseBody 注解會自動轉換 Json 顯示到頁面。該工程很簡單,就不展示其他代碼,大家在做練習的時候,可以找自己公司的項目或者自己寫一個 demo 工程,進行練習。
接口層:
public interface UserService { List
實現層:
@Service public class UserServiceImpl implements UserService { @Override public List
數據庫 Dao 層:
該層通過 Generator 插件生成
Generator 插件參考代碼:
Pom.xml 配置:
maven 插件編寫參考:
配置上面后點擊:
運行即可就能生成數據庫連接 SQL 語句。
腳本編寫
打開上一節使用源碼部署的工程,在介紹源碼運行腳本地方新建一個腳本,參考如下代碼修改成自己練習的腳本。
在 nGrinder 中新建的腳本編寫如下代碼:
import org.junit.FixMethodOrder 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 org.junit.Before import org.junit.BeforeClass import org.junit.Test import org.junit.runner.RunWith import java.util.Date import java.util.List import java.util.ArrayList import HTTPClient.Cookie import HTTPClient.CookieModule import HTTPClient.HTTPResponse import HTTPClient.NVPair @RunWith(GrinderRunner) class PostGetDemo { 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 = [] @BeforeProcess public static void beforeProcess() { // 設置請求響應超時時間(ms) HTTPPluginControl.getConnectionDefaults().timeout = 6000 // 創建GTest對象,第一個參數1代表有多個請求/事務時的執行順序ID,第二個參數是請求/事務的名稱,會顯示在summary結果中,有多個請求/事務時,要創建多個GTest對象 test = new GTest(1, "localhost:8888") //創建 HTTPRequest 對象,用于發起 HTTP 請求 request = new HTTPRequest() // Set header datas List
再次運行:
點擊運行配置加上:
-javaagent:D:\maven\repository\net\sf\grinder\grinder-dcr-agent\3.9.1\grinder-dcr-agent-3.9.1.jar
配置說明如下:
之后點擊運行即可:
結果如下:
關鍵點需要注意這里:
List
注意頭信息:
public static NVPair[] headers = [] public static NVPair[] params = [] public static Cookie[] cookies = []
查看源碼就知道怎么傳值,這里列舉 cookie 源碼傳值說明:
通過源碼查看得知如果傳 cookie 需要 new cookie 實體通過構造方法進行傳值入:
List
查看@BeforeThread注解下會執行 test.record方法源碼如下:
/** * Instrument the supplied {@code target} object's method which has the given name. Subsequent * calls to {@code target}'s given method will be recorded against the statistics for this * {@code Test}. * 提供的具有給定名稱的{@code target}對象方法。 后繼的對{@code target}給定方法的調用將根據此方法的統計信息進行記錄 * @param target Object to instrument. * @param methodName method name to instrument * @throws NonInstrumentableTypeException If {@code target} could not be instrumented. * @since 3.2.1 */ public final void record(Object target, String methodName) throws NonInstrumentableTypeException { if (StringUtils.isNotEmpty(context)) { record(target, new MethodNameFilter(methodName)); } }
解釋:
target:指腳本對象,這里是 this;
methodNam:是需要統計的方法名,通常都是被 @Test 注釋的方法。如果未配置,方法會正常執行,但是沒有統計結果數據;
以下代碼是可以復制出來修改的代碼
import org.junit.FixMethodOrder 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 org.junit.Before import org.junit.BeforeClass import org.junit.Test import org.junit.runner.RunWith import java.util.Date import java.util.List import java.util.ArrayList import HTTPClient.Cookie import HTTPClient.CookieModule import HTTPClient.HTTPResponse import HTTPClient.NVPair @RunWith(GrinderRunner) class PostGetDemo { 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 = [] @BeforeProcess public static void beforeProcess() { // 設置請求響應超時時間(ms) HTTPPluginControl.getConnectionDefaults().timeout = 6000 // 創建GTest對象,第一個參數1代表有多個請求/事務時的執行順序ID,第二個參數是請求/事務的名稱,會顯示在summary結果中,有多個請求/事務時,要創建多個GTest對象 test = new GTest(1, "localhost:8888") //創建 HTTPRequest 對象,用于發起 HTTP 請求 request = new HTTPRequest() // Set header datas List
源碼地址:
https://github.com/zuozewei/blog-example/blob/master/Performance-testing/01-test-tool/nGrinder/nGrinder-demo/script-sample/test-with-login/PostGetDemo.groovy
相關系列:
性能工具之 nGrinder 入門安裝及使用
性能工具之 nGrinder 源碼安裝
性能工具之 nGrinder Get 請求腳本編寫
性能工具之 nGrinder Post 請求腳本
性能工具之 nGrinder 關聯腳本編寫
性能工具之 nGrinder 參數化腳本編寫
性能工具之 nGrinder 區域配置
數據庫
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。