nGrinderr(version: 3.4.1)是NAVER(韓國最大互聯(lián)網(wǎng)公司NHN旗下搜索引擎網(wǎng)站)開源的性能測試工具,直接部署成web服務(wù),支持多用戶使用,可擴展性好,可自定義plugin。
nGrinder 是一款在一系列機器上執(zhí)行 Groovy 或 Jython 測試腳本的應(yīng)用,內(nèi)部引擎是基于 Grinder。 nGrinder 使用 controller 和 agent 分別包裝了 Grinder 的 console 和 agent ,而且擴展了多種功能使其能夠支持并發(fā)測試。
nGrinder 由兩個主要的組件組成
提供性能測試的web接口。
協(xié)調(diào)測試進程。
整理和顯示測試的統(tǒng)計結(jié)果
讓用戶創(chuàng)建和修改腳本。
在代理服務(wù)器上加載運行測試進程和線程。
監(jiān)控目標機器的系統(tǒng)性能(例如:CPU/MEMORY/網(wǎng)卡/磁盤)
下載地址:https://github.com/naver/ngrinder/releases
也可以直接通過:https://github.com/naver/ngrinder.git 方式
這我們演示直接使用下載 zip 包進行安裝:
打開目錄啟動腳本:
等待執(zhí)行成功便把如下 jar 包安裝到本地倉庫:
打開 IDEA 開發(fā)工具:
點擊文件導(dǎo)入 Project:
點擊 Open as Project
:
打開一個新窗口:
等待 maven 加載相應(yīng)的 jar。
修改代碼:
具體代碼如下:
package org.ngrinder.perftest.service; import org.ngrinder.infra.config.Config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.context.annotation.Profile; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * Dynamic creation of {@link PerfTestService} depending on the cluster enable or disable. * * @author JunHo Yoon * @since 3.1 */ @Configuration @Profile("production") @EnableScheduling @EnableTransactionManagement @EnableAspectJAutoProxy public class PerfTestServiceConfig implements ApplicationContextAware { @Autowired private Config config; private ApplicationContext applicationContext; /** * Create PerTest service depending on cluster mode. * * @return {@link PerfTestService} */ @Bean(name = "perfTestService") public PerfTestService perfTestService() { if (config.isClustered()) { return applicationContext.getAutowireCapableBeanFactory().createBean(ClusteredPerfTestService.class); } else { return applicationContext.getAutowireCapableBeanFactory().createBean(PerfTestService.class); } // return applicationContext.getAutowireCapableBeanFactory().createBean( // config.isClustered() ? ClusteredPerfTestService.class : PerfTestService.class); } @Override public void setApplicationContext(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } }
再次配置 Tomcat:
選擇運行方式:
選擇時時更新運行:
注意最好是加上 JVM 啟動參數(shù):
-Xms1024m -Xmx1024m -XX:MaxPermSize=200m
防止內(nèi)存出現(xiàn)異常
點擊確定:
啟動項目:
打開瀏覽器驗證是否成功:
http://localhost:8081/ngrinder/login
登錄成功:
在 script-sample
工程下的 pom.xml
文件增加:
代碼如下:
!-- https://mvnrepository.com/artifact/junit/junit --> dependency> groupId>junit/groupId> artifactId>junit/artifactId> version>4.12/version> scope>test/scope> /dependency>
再次在 idea 中全局搜索:
groovy-all
查看版本號,統(tǒng)一修改為:
version>2.4.16/version>
通過平臺生成腳本:
點擊 R HEAD
:
查看腳本:
importstatic net.grinder.script.Grinder.grinder importstatic org.junit.Assert.* importstatic 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 importHTTPClient.Cookie importHTTPClient.CookieModule importHTTPClient.HTTPResponse importHTTPClient.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) classTestRunner{ publicstaticGTest test publicstaticHTTPRequest request publicstaticNVPair[] headers = [] publicstaticNVPair[] params= [] publicstaticCookie[] cookies = [] @BeforeProcess publicstaticvoid beforeProcess() { HTTPPluginControl.getConnectionDefaults().timeout = 6000 test = newGTest(1, "www.baidu.com") request = newHTTPRequest() grinder.logger.info("before process."); } @BeforeThread publicvoid beforeThread() { test.record(this, "test") grinder.statistics.delayReports=true; grinder.logger.info("before thread."); } @Before publicvoid before() { request.setHeaders(headers) cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) } grinder.logger.info("before thread. init headers and cookies"); } @Test publicvoid test(){ HTTPResponse result = request.GET("https://www.baidu.com/", 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)); } } }
復(fù)制腳本:
在 idea 中新建腳本:
選擇 Groovy 腳本:
輸入名字點擊保存即可:
新建完畢把剛才腳本復(fù)制過來修改下方法名稱:
點擊運行:
可以看到提示:
在 Idea 菜單欄->Run->Edit Configurations->Default->Junit->在VM options
填寫自定義配置,點擊 Apply 按鈕保存配置即生效:
再次點擊:
運行結(jié)果如下:
到這里本機腳本調(diào)試成功。
下次再次分享本地參數(shù)化與 Post 請求
以上就是性能工具之 nGrinder 源碼安裝的詳細內(nèi)容,更多關(guān)于nGrinder 源碼安裝的資料請關(guān)注腳本之家其它相關(guān)文章!