性能測試四十九:ngrinder壓測平台


 

下載地址:https://sourceforge.net/projects/ngrinder/files/

 ngrinder工作原理:這里的controller就是ngrinder平台

 

部署(以windows為例):

把下載下來的ngrinder-controller.war重命名為ngrinder.war放到tomcat下面,啟動tomcat

 

啟動完瀏覽器訪問:http://localhost:8080/ngrinder

默認用戶名admin,密碼admin,選擇中文

登錄成功后下載agent和monitor

 

解壓並啟動agent

在機器足夠的情況下,應該把agent單獨部到一台機器上,由於機器有限這里和ngrinder部到同一台機器

先編輯配置文件:__agent.conf,修改ngrinder所在的ip和端口號(默認端口號16001)

雙擊啟動文件,下面這樣就算啟動成功了

 

把前面下載的monitor上傳至服務器,並解壓

 

進入到該目錄,里面的東西和agent一樣

啟動,這里第一次報錯,說沒有找到本機路由,后來查看,是因為當前host里面沒有配置當前使用的這個ip

 改host文件

 再次啟動,成功

 

 回到ngrinder,編寫腳本

這里對java和python哪個熟一點就選哪個

先確認接口是通的:http://192.168.1.163:8080/PerfTeach/TimeMonitor?id=1

 

Groovy(java版):

點創建后會生成如下代碼:

 

 

import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
import net.grinder.plugin.http.HTTPRequest
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.script.GTest
import net.grinder.script.Grinder
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith

import java.util.Date
import java.util.List
import java.util.ArrayList

import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair

/**
* A simple example using the HTTP plugin that shows the retrieval of a
* single page via HTTP.
*
* This script is automatically generated by ngrinder.
*
* @author admin
*/
@RunWith(GrinderRunner)
class TestRunner {

public static GTest test
public static HTTPRequest request
public static NVPair[] headers = []
public static NVPair[] params = []
public static Cookie[] cookies = []

@BeforeProcess
public static void beforeProcess() {
HTTPPluginControl.getConnectionDefaults().timeout = 6000
test = new GTest(1, "192.168.1.163")
request = new HTTPRequest()
// Set header datas
List<NVPair> headerList = new ArrayList<NVPair>()
headerList.add(new NVPair("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/21.0"))
headers = headerList.toArray()
// Set param datas
List<NVPair> paramList = new ArrayList<NVPair>()
paramList.add(new NVPair("", ""))
params = paramList.toArray()
// Set cookie datas
List<Cookie> cookieList = new ArrayList<Cookie>()
cookieList.add(new Cookie("uid", "abc", "192.168.1.163", "/", new Date(32503647599000L), false))
cookies = cookieList.toArray()
grinder.logger.info("before process.");
}

@BeforeThread
public void beforeThread() {
test.record(this, "test")
grinder.statistics.delayReports=true;
grinder.logger.info("before thread.");
}

@Before
public void before() {
request.setHeaders(headers)
cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
grinder.logger.info("before thread. init headers and cookies");
}

@Test
public void test(){
HTTPResponse result = request.GET("http://192.168.1.163:8080/PerfTeach/TimeMonitor?id=1", params)

if (result.statusCode == 301 || result.statusCode == 302) {
grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode);
} else {
assertThat(result.statusCode, is(200));
}
}
}

點驗證腳本,在控制台最下面有日志,可以看到通不通過

 

上面那個日志比較亂,為了方便確認接口是否通過,可以在代碼里面加斷言

調試階段(腳本調通以后,就刪掉這行代碼):在請求完成后,打印響應文本

在else里面,把原來的斷言注釋掉,加一個響應文本與應該返回的文本的斷言

再次驗證腳本,看日志,通過

 

刪除掉打印響應文本那行代碼,並保存

 

在性能測試中,創建測試

設計相關場景

 

 設置好以后,點保存並運行,這里會彈框選擇馬上運行還是預約運行,這里選馬上運行就可以了

 

 agent里面的日志

ngrinder這里也有相應的指標數據

跑完過后,會自動生成測試報告,點詳細測試報告,可以看到詳細指標

在首頁這里,還可以輸入url快速開始

 


免責聲明!

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



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