jmeter系列-如何實現像loadrunner一樣,多個並發用戶先通過登錄初始化,然后做並發的接口性能壓測


自動轉開發后,就很少關注性能測試方面的東西,最近在幫朋友做一個性能壓測,由於朋友那邊的公司比較小,環境比較簡單,而且是對http服務進行的壓測,所以最終

選用了jmeter來實現這個壓測。

  

如下就是我們的場景,多個並發用戶,每個用戶需要先登錄,而且只需要登錄一次,每個並發登錄成功后,會產生一個accessToken,也是就是代表了登錄服務器端登錄鑒權通過后,

返回給請求調用的一個標志,在后面具體的http接口請求中,每個並發用戶都需要在請求中傳入這個accessToken,不然的話,服務器端請求的鑒權就失敗。

1、需要做登錄,而且登錄只需要一次即可,這個時候,我們能想到的就是用jmeter 邏輯控制器中的僅一次控制器來實現。

看下面的這個圖,我們新建了一個僅一次控制器,然后在這個控制器下面添加了一個登錄操作的http請求。

這是一個標准的restful 形式的登錄接口。從圖片中的請求路徑就可以看出來。

 

2、你肯定也看到了,這個路徑中的/MJJX/users/login/${user},${user}我們做了參數化,因為我們要支持多個並發用戶。那么如何來參數化的呢,我們用了jmeter配置元件下的CSV Data Set Config組件。

下圖中可以看到,我們在本地有一個參數化文件。然后設置了user,idid變量用於登錄。上一步驟中的${user}變量就是從這個地方取過來的。

3、登錄成功后,那么我們需要獲取服務端返回的accesstoken值,這里我們使用了后置處理器下面的JSON Path PostProcessor組件來處理。我們通過json path $.data.accesstoken來獲取accesstoken

注意:這里我們選擇的是Sub-sanples-only。 因為我們只需要對子流程進行處理。

JsonPath的語法可以參考(參考自github):

https://github.com/json-path/JsonPath

Path Examples

Given the json

{
    "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } }, "expensive": 10 }


JsonPath (click link to try) Result
$.store.book[*].author The authors of all books
$..author All authors
$.store.* All things, both books and bicycles
$.store..price The price of everything
$..book[2] The third book
$..book[-2] The second to last book
$..book[0,1] The first two books
$..book[:2] All books from index 0 (inclusive) until index 2 (exclusive)
$..book[1:2] All books from index 1 (inclusive) until index 2 (exclusive)
$..book[-2:] Last two books
$..book[2:] Book number two from tail
$..book[?(@.isbn)] All books with an ISBN number
$.store.book[?(@.price < 10)] All books in store cheaper than 10
$..book[?(@.price <= $['expensive'])] All books in store that are not "expensive"
$..book[?(@.author =~ /.*REES/i)] All books matching regex (ignore case)
$..* Give me every thing
$..book.length() The number of books

 

3、登錄請求處理完之后,我們就可以處理后面壓測接口的http請求了,下面這就是一個普通的post請求了。參數中傳入了前面獲取到的accesstoken。

 4、之后就是我們常規壓測時,需要添加的一些組件。比如響應斷言,聚合報告,Summary Report等。

上面的都做完后,就可以開始壓測了。

 【原文歸作者所有,歡迎轉載,但是保留版權,並且轉載時,需要注明出處


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM