编程语言
首页 > 编程语言> > 性能工具之Ngrinder源码部署

性能工具之Ngrinder源码部署

作者:互联网

转载:https://cloud.tencent.com/developer/article/1526398

背景

为了更好了解nGrinder怎么工作或者为下次二次开发便开启使用源码部署。

下载源码:

https://github.com/naver/ngrinder/releases

也可以直接通过:https://github.com/naver/ngrinder.git这种方式下部署

本次使用下载zip进行安装:

点击目录再点击:

等待执行成功便把如下安装到本地创库:

打开idea:

点击文件导入:

点击Open as Project:

打开新窗口:

等待maven加载相应的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

选择tomcat所在目录:

选择:

添加运行war:

选择时时更新运行:

注意最好是这样:

-Xms1024m -Xmx1024m -XX:MaxPermSize=200m防止内存出现异常显现

点击确定:

启动:

打开浏览器验证是否成功:

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

查看版本号,统一修改为:

<version>2.4.16</version>

模仿编写脚本

通过平台生成脚本:

点击:

查看脚本

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));
}
}
}

复制脚本:

在idea中新建脚本脚本

选择groovy脚本

输入名字点击保存即可

新建完毕把刚才脚本复制过来修改下方法名称

如下:

点击运行:

提示:

解决方法:

在Idea菜单栏->Run->Edit Configurations->Default->Junit->在VM options填写自定义配置,点击Apply按钮保存配置即生效

再次点击:

运行结果如下:

本机脚本调试成功:

下次再次分享本地参数化与post请求

送大家一句话:

学习不等于学会!知道不等于做到!唯有严格的反复操练!才是灵活应用的保证!学习向学会走近一步,知道向做到拉近了一些,反复的练习向应用靠近了距离,我们只要走,就会见到别人的微笑。

标签:grinder,部署,springframework,源码,Ngrinder,org,import,net,junit
来源: https://www.cnblogs.com/ceshi2016/p/13845285.html