Try  HackMD Logo HackMD
tags: DevOps JMeter tutorials

JMeter for Performance Testing

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Intro

專案上線前為了確認系統穩定性會進行完整的效能/壓力測試,此次專案使用Apache JMeter™作為壓力測試的工具。

Install

由於Jmeter 100% 使用Java撰寫, 安裝JRE即可執行, 不過還是建議安裝JDK( 部分功能需要使用JDK中的Keytool )

Tutorial

使用JMeter主要分為三個步驟:

  1. Test Plan Building(測試腳本建立)
  2. Test Running(執行測試)
  3. Test Analysis(測試結果輸出)

Test Plan Building

於官網下載JMeter並解壓縮後進到 bin 目錄後會看到數個執行檔

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

由於目前要撰寫測試腳本,需使用GUI模式,故點選 jmeter.batjmeterw.cmd ( 啟動後會關閉Shell Console )

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. Test Plan右鍵 Add → Threads(Users) → Thread Group 新增一個 Thread Group(執行續群組), 並可針對該群組設定參數

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    • Action to be taken after a Sampler error:當有執行續收到Error Response( Http Code非200 )時要採取甚麼行動
    • Number of Threads (users):執行續數量( 模擬使用者數量 )
    • Ramp-up period (seconds):幾秒內達到所設定的執行續數量
    • Loop Count:每個執行續要執行幾次( -1 或勾選 infinite 代表次數無限 )
    • Same user on each iteration:預設是勾選, 每次使用相同的Session( 建議取消勾選達到較真實的測試場景 )
    • Specify Thread lifetime:指定執行續存活時長( 若Loop Count為無限的話建議設定 )
  2. Thread Group 右鍵 Add → Logic Controller → Transaction Controller 新增 Transaction Controller

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  3. Transaction Controller右鍵 Add → Sampler → HTTP Request 新增 HTTP Request

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

經過以上設定一個基本的測試腳本就完成了, 可以開始進行測試!!

Test Running

當撰寫腳本時若要測試腳本正確性可以以GUI模式執行測試(點選綠色三角start鍵), 正式進行測試時請使用Command-line模式

Don't run load test using GUI mode

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Test Analysis

Thread Group 右鍵 Add → Listener 可新增多種圖表

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

可根據需求新增不同圖表, 但其實JMeter有產出完整測試報告的功能( 僅限使用CLI Mode )
要注意的是, 部分圖表需要進行一些設定才能正常顯示, 設定方式為新增properties於 /bin/user.properties

  1. Generate report dashboard after load test:
    使用cli mode時給 -e 參數, 告知JMeter測試完成後產出報表。

  2. Filtering Configuration:
    JMeter Report Generator部分圖表採白名單制, 若該Http Request的Name不存在於設定的白名單內, 則該圖表不會收集此Http Request資訊。
    jmeter.reportgenerator.exporter.html.series_filter=^({http-request-name})(-success|-failure)?$
    user.properties中加上上列property並替換掉 {http-request-name}, 若有多個Name則以 | 隔開。

  3. Transaction Controller configuration:
    確保 Transaction Controller 中的 Generate parent sample 是uncheck的狀態。

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  4. Specify report output folder:
    使用cli mode時給 -o 參數指定報表輸出資料夾(注意:每次執行測試時皆須清空此資料夾)。

ex: jmeter -n -t /xxx/xxx.jmx -e -o /jmeter/report

以上列指令執行測試結束後JMeter會在 /jmeter/file/report 底下產出如下圖的資料目錄, 開啟 index.html 即可查看完整報表。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Advanced

HTTP Header manager

Test Plan右鍵 Add → Config Element → HTTP Header manager 新增 HTTP Header manager

HTTP Header manager 可以讓使用者針對每個Request自訂header資訊, ex: User-Agent, Content-Type, Authorization等, 且可以在任一層新增此設定(若在Test Plan層新增則表示該測試計畫底下所有Http Request 皆附加此設定)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

User Defined Variables

Test Plan右鍵 Add → Config Element → User Defined Variables 新增 User Defined Variables

User Defined Variables 概念類似於定義全域變數, 之後可以用替換字元的方式取得該值, 通常會定義Server Name ( 方便測試不同環境, ex: sit, dev ), 這樣同一個腳本只要更改Server Name的變數值就可以切換測試環境。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

CSV Data Set Config

Thread Group右鍵 Add → Config Element → CSV Data Set Config 新增 CSV Data Set Config

若想要模擬較真實的測試場景可以先建立好測試用資料, 並由JMeter帶給每一個執行續使用, 讓每個Request的資料更像是由不同使用者建立的

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

以下介紹較常用的參數:

  • Filename:指定檔案路徑
  • File encoding:指定檔案編碼
  • Delimiter:指定CSV的分隔符號
  • Recycle on EOF:當讀取至表單最末行時是否要再從第一筆開始讀取
  • Stop thread on EOF:當讀取至表單最末行時是否要停止後續thread的執行(停止發送新的Request)
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

可以看到上圖csv的首行我們定義了各欄位的key值,接下來只要在Http Request使用該key值的替換字元JMeter便會自動將value注入, 且會依序讀取每列資料提供給各個執行續使用。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Regular Expression Extractor

Http Request右鍵 Add → Post Preprocessors → Regular Expression Extractor 新增 Regular Expression Extractor

若API有相依性( API-B需使用API-A回傳的資料 ), 則可於API-A的Response提取所需變數並提供給API-B。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Extended Resources

  • BlazeMeter
    如果系統流程繁瑣, 一筆一筆新增Http Request會花費非常多時間, 此時建議可以使用 BlazeMeter 以螢幕錄製的方式建立測試腳本, 並可匯入JMeter 做後續調整。

Reference

Apache JMeter™
Day 20 Jmeter 壓力測試工具