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

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

ive count exp each achieve .com ast onf mod

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的原創文章,請關註公眾號"汪子熙":

技術分享圖片

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