如何使用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.git

咱們公司某團隊開發了一個OData服務,如今我接到任務,要測試這個服務在高併發訪問場景下的性能指標,好比5萬個請求同時到來後,每一個請求的平均響應時間,所以我選擇了jMeter這個好用的工具來模擬高併發請求。github

  1. Download JMeter from its official website:

http://jmeter.apache.org/web

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

  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請求,維護下列設置:app

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 - 記得這個勾別打上dom

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

相關文章
相關標籤/搜索