性能工具之 nGrinder Get 請求腳本編寫

      網友投稿 682 2025-03-31

      前言

      實現一個get 請求 demo

      腳本編寫

      前言

      實現一個get 請求 demo

      腳本編寫

      前言

      做性能測試腳本是一個實際下功夫的地方,工作中常見也就 是 key-value,json 方式比較多,那么 nGrinder 腳本咱們怎么編寫以下簡單介紹。

      性能工具之 nGrinder Get 請求腳本編寫

      實現一個get 請求 demo

      首先,通過 SpringBoot 編寫一個工程實現增刪改查,通過 Get 請求獲取:

      http://localhost:8888/findinfo?username=600128

      該工程 controller 層中用最簡單 Get 請求查詢數據,該代碼為:

      @GetMapping("/findinfo") @ResponseBody public List findUser(UserTable userInfo) { List UserInfo = userService.findinfo(userInfo); return UserInfo; }

      @ResponseBody 注解會自動轉換 Json 顯示到頁面。該工程很簡單,就不展示其他代碼,大家在做練習的時候,可以找自己公司的項目或者自己寫一個 demo 工程,進行練習。

      接口層:

      public interface UserService { List findinfo(UserTable userInfo); }

      實現層:

      @Service public class UserServiceImpl implements UserService { @Override public List findinfo(UserTable userInfo) { UserTableExample example = new UserTableExample(); UserTableExample.Criteria criteria = example.createCriteria(); criteria.andUsernameEqualTo(userInfo.getUsername()); return userTableMapper.selectByExample(example); } }

      數據庫 Dao 層:

      該層通過 Generator 插件生成

      Generator 插件參考代碼:

      Pom.xml 配置:

      org.springframework.boot spring-boot-maven-plugin true org.mybatis.generator mybatis-generator-maven-plugin 1.3.2 ${basedir}/src/main/resources/generator/generatorConfig.xml true true

      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 headerList = new ArrayList() headerList.add(new NVPair("Content-Type","application/x-www-form-urlencoded")) headerList.add(new NVPair("Connection", "keep-alive")) headers = headerList.toArray() // Set param datas List paramList = new ArrayList() paramList.add(new NVPair("username", "600128")) params = paramList.toArray() // Set cookie datas List cookieList = new ArrayList() cookieList.add(new Cookie("Cookie", "Idea-96adee05=87213429-7753-4183-99d0-c1c3362ce5d0; Hm_lvt_eb54a8c74fe4cb91ad8ca004e48cd3f9=1545999605; Pycharm-5b892e29=6772acb9-e87b-4d37-98e3-bff82e412d17; csrftoken=KDr8oYtD0gKh7UbS5fWSROzEOX6rjX3CN26pz9PQu9AczLMGFhw53ZrjAhoIOTE7; Hm_lvt_f2c884fc06fca522c4105429259b8a73=1558506687; Webstorm-173930bd=052a6829-b802-4eb0-a1e2-e348f1012604; _ga=GA1.1.1061327805.1562753587; UM_distinctid=16c02a6b049e1-02b6b032525db4-e343166-144000-16c02a6b04a120; CNZZDATA4617777=cnzz_eid%3D1637582848-1563412391-%26ntime%3D1563412391; Hm_lvt_0cb375a2e834821b74efffa6c71ee607=1563412574; bad_id22bdcd10-6250-11e8-917f-9fb8db4dc43c=a32ab941-a8f9-11e9-9989-93f7b4f25032; Idea-2b3f02ca=87213429-7753-4183-99d0-c1c3362ce5d0; cookie_lang=0; JSESSIONID=801DE60D9A00C391F80ABE0CD94A6E25", "localhost:8888", "", new Date(), true)) cookies = cookieList.toArray() 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("http://localhost:8888/findinfo", params) def text = result.getText() grinder.logger.info(text) //斷言HTTP請求狀態碼 assertThat(result.statusCode, is(200)) } }

      再次運行:

      點擊運行配置加上:

      -javaagent:D:\maven\repository\net\sf\grinder\grinder-dcr-agent\3.9.1\grinder-dcr-agent-3.9.1.jar

      配置說明如下:

      之后點擊運行即可:

      結果如下:

      關鍵點需要注意這里:

      List headerList = new ArrayList() headerList.add(new NVPair("Content-Type", "application/x-www-form-urlencoded")) headerList.add(new NVPair("Connection", "keep-alive")) headers = headerList.toArray()

      注意頭信息:

      public static NVPair[] headers = [] public static NVPair[] params = [] public static Cookie[] cookies = []

      查看源碼就知道怎么傳值,這里列舉 cookie 源碼傳值說明:

      通過源碼查看得知如果傳 cookie 需要 new cookie 實體通過構造方法進行傳值入:

      List cookieList = new ArrayList() cookieList.add(new Cookie("Cookie", "Idea-96adee05=87213429-7753-4183-99d0-c1c3362ce5d0; Hm_lvt_eb54a8c74fe4cb91ad8ca004e48cd3f9=1545999605; Pycharm-5b892e29=6772acb9-e87b-4d37-98e3-bff82e412d17; csrftoken=KDr8oYtD0gKh7UbS5fWSROzEOX6rjX3CN26pz9PQu9AczLMGFhw53ZrjAhoIOTE7; Hm_lvt_f2c884fc06fca522c4105429259b8a73=1558506687; Webstorm-173930bd=052a6829-b802-4eb0-a1e2-e348f1012604; _ga=GA1.1.1061327805.1562753587; UM_distinctid=16c02a6b049e1-02b6b032525db4-e343166-144000-16c02a6b04a120; CNZZDATA4617777=cnzz_eid%3D1637582848-1563412391-%26ntime%3D1563412391; Hm_lvt_0cb375a2e834821b74efffa6c71ee607=1563412574; bad_id22bdcd10-6250-11e8-917f-9fb8db4dc43c=a32ab941-a8f9-11e9-9989-93f7b4f25032; Idea-2b3f02ca=87213429-7753-4183-99d0-c1c3362ce5d0; cookie_lang=0; JSESSIONID=801DE60D9A00C391F80ABE0CD94A6E25", "localhost:8888", "", new Date(), true)) cookies = cookieList.toArray() grinder.logger.info("before process.");

      查看@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 headerList = new ArrayList() headerList.add(new NVPair("Content-Type", "application/x-www-form-urlencoded")) headerList.add(new NVPair("Connection", "keep-alive")) headers = headerList.toArray() // Set param datas List paramList = new ArrayList() paramList.add(new NVPair("username", "600128")) params = paramList.toArray() // Set cookie datas List cookieList = new ArrayList() cookieList.add(new Cookie("Cookie", "Idea-96adee05=87213429-7753-4183-99d0-c1c3362ce5d0; Hm_lvt_eb54a8c74fe4cb91ad8ca004e48cd3f9=1545999605; Pycharm-5b892e29=6772acb9-e87b-4d37-98e3-bff82e412d17; csrftoken=KDr8oYtD0gKh7UbS5fWSROzEOX6rjX3CN26pz9PQu9AczLMGFhw53ZrjAhoIOTE7; Hm_lvt_f2c884fc06fca522c4105429259b8a73=1558506687; Webstorm-173930bd=052a6829-b802-4eb0-a1e2-e348f1012604; _ga=GA1.1.1061327805.1562753587; UM_distinctid=16c02a6b049e1-02b6b032525db4-e343166-144000-16c02a6b04a120; CNZZDATA4617777=cnzz_eid%3D1637582848-1563412391-%26ntime%3D1563412391; Hm_lvt_0cb375a2e834821b74efffa6c71ee607=1563412574; bad_id22bdcd10-6250-11e8-917f-9fb8db4dc43c=a32ab941-a8f9-11e9-9989-93f7b4f25032; Idea-2b3f02ca=87213429-7753-4183-99d0-c1c3362ce5d0; cookie_lang=0; JSESSIONID=801DE60D9A00C391F80ABE0CD94A6E25", "localhost:8888", "", new Date(), true)) cookies = cookieList.toArray() 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("http://localhost:8888/findinfo", params) def text = result.getText() grinder.logger.info(text) // 斷言HTTP請求狀態碼 assertThat(result.statusCode, is(200)) }

      源碼地址:

      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小時內刪除侵權內容。

      上一篇:Nginx(2):架構設計與工作流程
      下一篇:批量制作與打印帶照片的員工簡歷
      相關文章
      va亚洲va日韩不卡在线观看| 国产亚洲福利精品一区| 久久亚洲国产精品五月天婷| 亚洲JIZZJIZZ妇女| 亚洲黄页网在线观看| 亚洲jjzzjjzz在线播放| 国产av天堂亚洲国产av天堂| 亚洲一级片免费看| 2022中文字字幕久亚洲| 亚洲精品无码av天堂| 亚洲七七久久精品中文国产| 精品亚洲一区二区三区在线播放 | 国产精品亚洲五月天高清| 色拍自拍亚洲综合图区| 亚洲精品高清视频| 亚洲日本在线观看视频| 亚洲性日韩精品国产一区二区| 亚洲国产成人久久综合碰| 亚洲人成网站18禁止一区| 久久亚洲精品无码观看不卡| 超清首页国产亚洲丝袜| 亚洲熟妇丰满多毛XXXX| 亚洲成色在线综合网站| 亚洲av永久无码制服河南实里| 亚洲AV日韩AV永久无码下载| 亚洲视频.com| 亚洲国产成人综合| 亚洲中文字幕久久精品无码VA| 亚洲日韩AV一区二区三区四区| 亚洲AV无码国产精品永久一区| 国产在亚洲线视频观看| 久久久亚洲精品蜜桃臀| 亚洲国产三级在线观看| 久久久久亚洲Av片无码v| 中文字幕亚洲精品资源网| 亚洲一区免费视频| 亚洲成熟丰满熟妇高潮XXXXX| 亚洲人xxx日本人18| 亚洲性一级理论片在线观看| 精品久久亚洲中文无码| 亚洲第一成年免费网站|