iT邦幫忙

0

[JMeter]簡單的多人登入壓力測試

JMeter安裝完成後,就可以開始嘗試做一些簡單的測試了,
我們可以先模擬多人同時登入的情況,進行簡單的壓力測試。

新增Thread Group

開啟JMeter後,系統預設會新增一個空白的執行計畫,
根據需要測試的內容,在測試計畫底下新增一個Thread Group。
https://ithelp.ithome.com.tw/upload/images/20220524/20149099ROQPVmgDXF.png

本次要執行的動作是使用者登入並進入到系統首頁,所以將Thread Group命名為登入
https://ithelp.ithome.com.tw/upload/images/20220524/20149099A7Zfd03QMt.png

接下來設定Thread Properties,前三個欄位說明如下:

  • Number of Threads(users):執行測試時,需要多少個使用者
  • Ramp-Up Period (in seconds):在幾秒內執行完這個測試
  • Loop Count:重複執行次數

當我們如下圖設定時,代表這個測試同時需要100位使用者,
在3秒內執行完登入動作,並且執行1次即可(不重複)。
https://ithelp.ithome.com.tw/upload/images/20220524/20149099rNwYfPhNHr.png

準備測試帳密資料

我們設定這個測試需要由100位使用者同時進行,這時候可以準備一個csv檔,
紀錄100組測試帳密資料。
https://ithelp.ithome.com.tw/upload/images/20220524/201490996mB4tvNpOz.png

接著在登入Thread Group中,新增CSV Data Set Config。
https://ithelp.ithome.com.tw/upload/images/20220524/20149099oAgsRJ04hD.png

CSV Data Set Config設定如下圖。參數說明如下:

  • FileName:CSV檔的來源路徑。
  • Variable Names:需要從CSV檔中取得的變數名稱,可以自行命名,後續設定需要使用到CSV中的資料作為變數時,以這個名稱做指定。
  • Ignore first line:需不需要忽略CSV檔中的第一行,本範例中的CSV檔第一行為標題列,所以選擇True。

https://ithelp.ithome.com.tw/upload/images/20220524/20149099RMeqmit4sh.png

新增Http Request

在Test Plan中,新增一個Http Request,將這個Http Request命名為登入頁面
https://ithelp.ithome.com.tw/upload/images/20220524/20149099wKYtMeV3v5.png

https://ithelp.ithome.com.tw/upload/images/20220524/20149099v6pFgvIirn.png

設定Web Server中的參數,指定要載入的登入頁面,本例使用本機環境載入登入頁面。設定參數如下圖。

  • Protocol[http]:設定目前使用的環境使用的通訊協定,如果是使用非本機環境,請注意該環境是否有使用https,若有的話,本欄位請調整為https。
  • Server Name or IP:只要填寫Server IP或domain name即可,例如本機環境填寫localhost。
  • HTTP Request:依需求設定POST/GET,Path請填寫登入頁面的網址。

https://ithelp.ithome.com.tw/upload/images/20220524/20149099GAv3IGD9Vo.png

若測試的系統有設定RequestVerificationToken,
那就需要登入頁中RequestVerificationToken的值抓出來一併傳送,才能通過登入頁防偽驗證,
在登入頁面新增一個CSS Selector Extractor來抓取。
https://ithelp.ithome.com.tw/upload/images/20220524/20149099DLGzv0uxIC.png

CSS Selector Extractor設定如下圖,
可以將登入頁中的RequestVerificationToken存到變數RequestVerificationToken中。

  • Name of created variable:變數名稱,可以自己命名。
  • CSS Selector expression:要從目標頁面中抓取的變數值,要用CSS取值的方式填寫。
  • Attribute:要取得的Attribute。

https://ithelp.ithome.com.tw/upload/images/20220524/20149099EeaQ6TSi89.png

接下來在Test Plan中,再新增一個Http Request,
這一個Http Request是用來傳送登入資料的,我們將它命名為登入
https://ithelp.ithome.com.tw/upload/images/20220524/20149099gKcE2h9xRd.png

登入頁面一樣,設定Web Server中的參數,但HTTP Request要設定為POST。
https://ithelp.ithome.com.tw/upload/images/20220524/20149099IxEkGGqTuZ.png

為了讓登入可以辨別登入頁面傳進來的參數值,需要在登入中新增Parameters,
這個範例需要的變數值分別為帳號(Account)、密碼(Password)與RequestVerificationToken,如下圖設定。
https://ithelp.ithome.com.tw/upload/images/20220524/20149099bepA5cAbcz.png

最後在Test Plan底下加入HTTP Cookie Manager,在登入後執行的動作,
都會繼續使用登入時產生的Cookie,以保持登入的狀態。
https://ithelp.ithome.com.tw/upload/images/20220524/201490991ZRCaRHP8S.png

進行測試

為了確認測試計畫使否有正確執行,以及進行後續的分析,
可以在Test Plan底下新增View Results Tree以及Summary Report。
新增完畢後,將本範例儲存為jmx檔,才能進行測試。
https://ithelp.ithome.com.tw/upload/images/20220524/20149099KmqvMZhas5.png

點選上方綠色箭頭的符號後,測試計畫會開始執行,等到執行結束後,
可以切換到View Results Tree頁面,確認是否有測試計畫正常執行,
若結果都呈現綠色,代表測試計畫有執行成功。
https://ithelp.ithome.com.tw/upload/images/20220524/20149099Xwn0t8Q36m.png

https://ithelp.ithome.com.tw/upload/images/20220524/20149099znRXSu8iuq.png

切換到Summary Report,可以檢視每一個動作執行的相關數據,例如執行次數、執行時間、錯誤率等等。
https://ithelp.ithome.com.tw/upload/images/20220524/20149099Hs1UWTKXb1.png


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言