ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

性能工具之Ngrinder源码部署

2020-10-20 11:00:54  阅读:228  来源: 互联网

标签:grinder 部署 springframework 源码 Ngrinder org import net junit


转载: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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有