For project reason I have to measure the performance of OData service being accessed parallelly. And I plan to use the open source tool JMeter to generate a huge number of request in parallel and measure the average response time. Since I am a beginner for JMeter, I write down what I have learned into this blog. I will continue to explorer the advanced feature of JMeter in my daily work.
我們公司某團隊開發了一個OData服務,現在我接到任務,要測試這個服務在高并發通路場景下的性能名額,比如5萬個請求同時到來後,每個請求的平均響應時間,是以我選擇了jMeter這個好用的工具來模拟高并發請求。

- Download JMeter from its official website:
Go to the installation folder, add the following text in file binuser.properties:
httpclient4.retrycount=1
hc.parameters.file=hc.parameters
-
Create a new test plan for example Customer_Query_OData_test, and right click on it and create a thread group from context menu.
建立一個新的測試plan,基于其再建立一個線程組:
Below configuration means I would like to generate three request in parallel via three threads, each thread is executed only once. And there is no delay during the spawn of each threads ( Ramp-Up Period = 0 )
下列設定意思是我想建立三個并發請求,每個請求通過一個線程實作,每個線程僅僅執行一次。每個線程派生後的延時是0秒,意思是主線程同時建立三個線程。
建立一個新的HTTP請求,維護下列設定:
Create a new Http Request and maintain the following settings:
(1) Protocol: https
(2) Server name:
(3) Http request method: GET
(4) Http path: /sap/c4c/odata/v1/c4codata/AccountCollection/ - 這就是OData服務的相對路徑了
(5) Use KeepAlive: do NOT select this checkbox - 記得這個勾别打上
In Parameter tab, maintain query option $search with value ‘Wang’
這個意思就是每個并發請求同時發起OData查詢,參數為我的名字Wang
Switch to Advanced tab, choose “HttpClient4” from drop down list for Implementation, and maintain proxy server name and port number.
如果有代理的話,在下圖位置維護代理伺服器資訊。
- Create a new HTTP Header Manager and specify the basic authentication header field and value.
在HTTP Header Manager裡維護通路這個Odata服務的credential。因為我們開發的OData服務支援Basic Authentication這種認真方式,是以我在此處的HTTP header字段裡維護Authentication資訊。
- Create a listener for the test plan. In my test I simply choose the most simple one: View Results in Table.
建立listener,主要用途當然是顯示測試結果了。我使用的是jMeter自帶的Listener,Table類型的,以表格形式顯示高并發請求和響應的各項名額。
Once done, start the test:
一切就緒,點選這個綠色的三角形開始測試:
After the test is finished, double click on View Result Listener and the response time for each request and the average response time is displayed there:
測試完畢後,輕按兩下我們之前建立的Table Result Listener,我這三個并發請求的性能名額就顯示出來了。可以看到三個請求中,最快的請求用了5.1秒,最慢的6.9秒
當然,jMeter也支援指令行方式使用:
Or you can use command line to achieve the same:
-n: use non-GUI mode
-t: specify which test plan you want to run
-l: specify the path of output result file
為了檢驗jMeter采集的資料是否正确可靠,我還花時間寫了一個Java程式,用JDK自帶的線程池産生并發請求,測試的結果和jMeter是一緻的。
And I have written a simple Java application to generate parallel request via multiple thread and the result measured in Java program is consistent with the one got from JMeter.
The source code could be found from my github:
我的Java程式放在我的github上:
https://github.com/i042416/JavaTwoPlusTwoEquals5/tree/master/src/odataHow to generate random query for each thread in JMeter
到目前為止,我的三個并發請求進行搜尋的參數都是寫死的Wang,這個和實際場景不太符合。有沒有辦法生成一些随機的搜尋字元串,這樣更貼近真實使用場景呢?
Suppose we would like each thread in JMeter to generate different customer query via OData with the format JerryTestCustomer_<1~100>, we can simply create a new user parameter:
當然有辦法:右鍵菜單,Add->Pre Processors(預處理器)->User Parameters:
參數名Parameter name,取為uuid
參數值Parameter value: use JMeter predefined function __Random to generate random number.
使用jMeter自帶的随機數生成函數__Random。
是以最後參數uuid的值為${__Random(1,100)},意思是生成1到100内的随機正整數
and in http request, just specify reference to this variable via ${uuid}:
在http請求裡,用固定的字首JerryTestCustomer_加上随機參數,以此來構造随機搜尋字元串:
So that in the end each thread will issue different query to OData service end point.
通過Table Result listener,能觀察到這次确實每個請求發起的搜尋都使用了不同的字元串了。
希望這篇文章介紹的jMeter使用技巧對大家工作有所幫助。
本文來自雲栖社群合作夥伴“汪子熙”,了解相關資訊可以關注微信公衆号"汪子熙"。