1. 程式人生 > >如何使用jMeter對某個OData服務進行高併發效能測試

如何使用jMeter對某個OData服務進行高併發效能測試

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這個好用的工具來模擬高併發請求。

  1. Download JMeter from its official website:

http://jmeter.apache.org/

Go to the installation folder, add the following text in file \bin\user.properties:
httpclient4.retrycount=1
hc.parameters.file=hc.parameters

  1. 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.

如果有代理的話,在下圖位置維護代理伺服器資訊。

  1. 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資訊。

  1. 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/odata

How 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使用技巧對大家工作有所幫助。

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":