JMeter官網:https://jmeter.apache.org/。
關於JMeter是什麼、能幹什麼,JMeter官網首頁說明得很清楚,可以自己查看。
在這個地址 https://archive.apache.org/dist/jmeter/binaries/ ,可以下載JMeter的所有版本,目前最新版本是apache-jmeter-5.2.1.zip,這個在首頁即可下載:
JMeter是運行在JVM上的,所以需要先配置好Java環境。JMeter支持遠程運行,是以線程方式運行。JMeter通過線程組來驅動多個線程運行測試腳本對被測試服務器發起負載,每一個負載機上都可以運行多個線程組。JMeter運行場景不僅可以再GUI方式中完成,還可以使用命令行,而且命令行運行的方式對於負載機的資源消耗會更小。
【1】JMeter基本介紹
將下載的zip解壓並進入bin目錄啓動jmeter.bat即可,默認界面如下所示(本文使用版本5.2):
① 更換主題
默認主題如下:
在Options-Look and Feel步驟下可以修改默認主題,這裏修改爲System(修改完會提示重啓):
③ 更換語言
對英語功底差的同學來說,更偏向於漢化。JMeter不需要漢化,它自身可以選擇語言。Options-Choose Language-chinese(Simplified):
④ 工作區介紹
如下圖所示,這裏分爲三部分:
區域①是一個目錄樹,存放測試設計過程中使用到的元件;執行過程中默認從根節點開始順序遍歷樹上的元件。什麼是元件?比如我們要向服務器發送一個http post請求,這個請求是由一個http請求取樣器來完成的,這個叫“HTTP請求”的取樣器就是元件。在區域①中添加進來的都是元件。
區域②是測試計劃編輯區域,在【用戶定義的變量】區域我們可以定義整個測試計劃共用的全局變量,這些變量對多個線程組有效。我們還可以對線程組的運行進行設置:比如“獨立運行每個線程組”,“Run teardown Thread Groups after shutdown of main threads”等。另外我們還可以在此添加測試計劃依賴的jar包,比如JDBC方式連接數據庫的驅動。
區域③是菜單欄,圖標是菜單快捷方式,快捷菜單功能從左到右依次是:
- 新建測試計劃
- 選擇測試計劃模板創建一個新的測試計劃
- 選擇已經存在的測試計劃並打開;
- 保存測試計劃
- 剪切選定的元件及子元件
- 複製選定的元件及子元件
- 粘貼複製的元件及子元件
- 展開目錄樹
- 收起目錄樹
- 禁用或啓用元件,禁用元件的子元件也會被禁用
- 本機開始運行當前測試計劃,按線程組的設置來啓動
- 立即開始在本機運行當前測試計劃
- 停止運行狀態的測試計劃,當前線程執行完成後停止
- 停止運行測試計劃,立即終止,類似於殺進程
- 清除運行過程中元件顯示的響應數據,比如查看結果樹中的內容,聚合報告中的內容,但不能清除日誌控制檯中的內容
- 清除所有元件的響應數據,包括日誌
- 查找
- 重置查找
- 函數助手對話框,這些函數在做參數化時會用到
- 幫助文檔
⑤ Summary Report(彙總報告)
首先添加一個線程組:
添加監聽器元件:
界面如下所示:
字段說明:
- Label:取樣器別名
- #Samples:取樣器運行次數
- Average:請求(事務)的評價響應時間
- Min:請求的最小響應時間
- Max:請求的最大響應時間
- Std.Dev:響應時間的標準偏差
- Error%:事務錯誤率
- Throughout:吞吐率,即常說的TPS
- Received KB/sec:每秒接收數據包流量,單位KB
- Sent KB/sec:每秒發送數據包流量,單位KB
- Avg.Bytes:平均數據流量,但是Byte
⑥ Aggregate Report(聚合報告)
在線程組添加監聽器元件:
字段說明:
- Label:請求別名
- #Samples:取樣器運行次數
- Average:請求(事務)的評價響應時間,單位毫秒。這個平均值是所有請求的響應時間取平均值
- Median:響應時間中間值
- 90%Line:90%事務平均響應時間
- 95%Line:95%事務平均響應時間
- 99%Line:99%事務平均響應時間
- Min:請求的最小響應時間
- Max:請求的最大響應時間
- Error%:事務錯誤率
- Throughout:吞吐率,即常說的TPS
- Received KB/sec:每秒接收數據包流量,單位KB
- Sent KB/sec:每秒發送數據包流量,單位KB
【2】Windows下JMeter GUI運行測試
也就是Windows下使用JMeter圖形界面進行測試。
① 在測試計劃上右鍵添加線程組
② 在線程組上右鍵添加請求和監聽器元件
③ 編輯線程組
線程組界面如下:
解釋如下:
- 線程數:加載多少線程
- Ramp-Up時間:加載線程時間。如上圖即爲一秒內加載1000個線程
- 循環次數:如上圖循環十次則表示1000個線程每個執行10次總樣本數爲10000
④ 編輯HTTP請求
格式如下圖所示:
這裏是基本HTTP請求,至於高級格式後續更新。
編輯完線程組和HTTP請求後,就可以執行測試計劃了。
⑤ 查看結果樹
如下圖所示,每個請求都詳細記錄了請求參數、請求頭、響應結果、響應頭以及取樣器基本信息。
⑥ Aggregate Graph(聚合圖)和Aggregate Report(聚合報告)
Aggregate Graph如下:
Aggregate Report只是沒有圖表,其他與Aggregate Graph一樣。
⑦ 彙總報告(Summary Report)
彙總報告如下:
【3】Linux下JMeter 非GUI運行測試-命令執行
非GUI運行測試會減少資源消耗,JMeter同樣支持命令方式執行測試計劃,命令實例如下:
java -jar %JMETER_HOME%/bin/ApacheJMeter.jar -n -t %JMETER_HOME%/script/Test Plan.jmx -r -l result.jtl
or
%JMETER_HOME%/bin/jmeter -n -t %JMETER_HOME%/script/Test Plan.jmx -l %JMETER_HOME%/result/results.jtl
這兩種方式都可以運行測試計劃,JMeter運行測試計劃實際上是通過運行ApacheJMeter.jar來完成的。
① 在Windows下cmd打開命令窗口執行jmeter -help
結果
可以看到JMeter提供了幾種常用命令執行測試計劃的方式:
Microsoft Windows [版本 6.1.7601]
版權所有 (c) 2009 Microsoft Corporation。保留所有權利。
E:\softinstall\apache-jmeter-5.2-me\bin>jmeter -help
_ ____ _ ____ _ _ _____ _ __ __ _____ _____ _____ ____
/ \ | _ \ / \ / ___| | | | ____| | | \/ | ____|_ _| ____| _ \
/ _ \ | |_) / _ \| | | |_| | _| _ | | |\/| | _| | | | _| | |_) |
/ ___ \| __/ ___ \ |___| _ | |___ | |_| | | | | |___ | | | |___| _ <
/_/ \_\_| /_/ \_\____|_| |_|_____| \___/|_| |_|_____| |_| |_____|_| \_\ 5.2
Copyright (c) 1999-2019 The Apache Software Foundation
To list all command line options, open a command prompt and type:
jmeter.bat(Windows)/jmeter.sh(Linux) -?
--------------------------------------------------
To run Apache JMeter in GUI mode, open a command prompt and type:
jmeter.bat(Windows)/jmeter.sh(Linux) [-p property-file]
--------------------------------------------------
To run Apache JMeter in NON_GUI mode:
Open a command prompt (or Unix shell) and type:
jmeter.bat(Windows)/jmeter.sh(Linux) -n -t test-file [-p property-file] [-l results-file] [-j log-file]
--------------------------------------------------
To run Apache JMeter in NON_GUI mode and generate a report at end :
Open a command prompt (or Unix shell) and type:
jmeter.bat(Windows)/jmeter.sh(Linux) -n -t test-file [-p property-file] [-l results-file] [-j log-file] -e -o [Path to output folder]
--------------------------------------------------
To generate a Report from existing CSV file:
Open a command prompt (or Unix shell) and type:
jmeter.bat(Windows)/jmeter.sh(Linux) -g [csv results file] -o [path to output folder (empty or not existing)]
--------------------------------------------------
To tell Apache JMeter to use a proxy server:
Open a command prompt and type:
jmeter.bat(Windows)/jmeter.sh(Linux) -H [your.proxy.server] -P [your proxy server port]
---------------------------------------------------
To run Apache JMeter in server mode:
Open a command prompt and type:
jmeter-server.bat(Windows)/jmeter-server(Linux)
---------------------------------------------------
E:\softinstall\apache-jmeter-5.2-me\bin>
② window下查看所有命令
E:\softinstall\apache-jmeter-5.2-me\bin>jmeter.bat -?
_ ____ _ ____ _ _ _____ _ __ __ _____ _____ _____ ____
/ \ | _ \ / \ / ___| | | | ____| | | \/ | ____|_ _| ____| _ \
/ _ \ | |_) / _ \| | | |_| | _| _ | | |\/| | _| | | | _| | |_) |
/ ___ \| __/ ___ \ |___| _ | |___ | |_| | | | | |___ | | | |___| _ <
/_/ \_\_| /_/ \_\____|_| |_|_____| \___/|_| |_|_____| |_| |_____|_| \_\ 5.2
Copyright (c) 1999-2019 The Apache Software Foundation
--?
print command line options and exit
-h, --help
print usage information and exit
-v, --version
print the version information and exit
-p, --propfile <argument>
the jmeter property file to use
-q, --addprop <argument>
additional JMeter property file(s)
-t, --testfile <argument>
the jmeter test(.jmx) file to run. "-t LAST" will load last
used file
-l, --logfile <argument>
the file to log samples to
-i, --jmeterlogconf <argument>
jmeter logging configuration file (log4j2.xml)
-j, --jmeterlogfile <argument>
jmeter run log file (jmeter.log)
-n, --nongui
run JMeter in nongui mode
-s, --server
run the JMeter server
-E, --proxyScheme <argument>
Set a proxy scheme to use for the proxy server
-H, --proxyHost <argument>
Set a proxy server for JMeter to use
-P, --proxyPort <argument>
Set proxy server port for JMeter to use
-N, --nonProxyHosts <argument>
Set nonproxy host list (e.g. *.apache.org|localhost)
-u, --username <argument>
Set username for proxy server that JMeter is to use
-a, --password <argument>
Set password for proxy server that JMeter is to use
-J, --jmeterproperty <argument>=<value>
Define additional JMeter properties
-G, --globalproperty <argument>=<value>
Define Global properties (sent to servers)
e.g. -Gport=123
or -Gglobal.properties
-D, --systemproperty <argument>=<value>
Define additional system properties
-S, --systemPropertyFile <argument>
additional system property file(s)
-f, --forceDeleteResultFile
force delete existing results files and web report folder if
present before starting the test
-L, --loglevel <argument>=<value>
[category=]level e.g. jorphan=INFO, jmeter.util=DEBUG or com
.example.foo=WARN
-r, --runremote
Start remote servers (as defined in remote_hosts)
-R, --remotestart <argument>
Start these remote servers (overrides remote_hosts)
-d, --homedir <argument>
the jmeter home directory to use
-X, --remoteexit
Exit the remote servers at end of test (non-GUI)
-g, --reportonly <argument>
generate report dashboard only, from a test results file
-e, --reportatendofloadtests
generate report dashboard after load test
-o, --reportoutputfolder <argument>
output folder for report dashboard
命令參數說明:
- -n:非GUI方式運行
- -t:指定運行的測試腳本地址與名稱
- -p:指定讀取JMeter屬性文件,比如jmeter.properties文件中設置的
- -l:記錄測試結果到文件,指定文件地址與名稱
- -s:以服務器方式運行(就是我們說的遠程方式,啓動Agent)
- -H:設置代理,一般填寫代理IP
- -P:設置代理端口
- -u:代理賬號
- -a:代理口令
- -J:定義JMeter屬性,等同於在jmeter.properties中進行設置
- -G:定義JMeter全局屬性,等同於在Global.properties中進行設置,線程間可以共享
- -D:定義系統屬性,等同於在system.properties中進行設置
- -S:加載系統屬性文件,可以通過此參數指定加載一個系統屬性文件,此文件可以用戶自己定義
- -L:定義JMeter日誌基本,比如DEBUG、INFO、ERROR等
- -r:開啓遠程負載機(非GUI方式),遠程機器列表在jmeter.properties中指定
- -R:開啓遠程負載機,-R可以指定負載機IP,此選項會覆蓋jmeter.properties中remote_hosts的設置
- -d:指定JMeter Home目錄
- -X:停止遠程執行
③ Linux下執行測試計劃
這裏需要一些前提步驟:
- 將測試項目(測試計劃對應的測試目標項目)部署在linux上;
- 保存剛纔window下測試計劃腳本並放到linux上;
- 將jmeter部署在linux上;
- linux上必須安裝好java環境以及項目環境
將項目在linux上運行起來後,如下圖所示,將jmeter和測試計劃腳本傳到linux上:
將其解壓到當前文件路徑下:
tar -xvf apache-jmeter-5.2.tgz
執行命令進行測試並同時監控服務器負載狀況:
./apache-jmeter-5.2/bin/jmeter -n -t ./TestPlan.jmx -l ./results.jtl
將生成的結果文件下載下來用jmeter打開:
在執行測試計劃時用top命令監控服務器負載狀況如下所示:
修改測試計劃進行多次測試,從而逼近在當前服務器環境下該系統最大負荷。