欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

nGrinder性能工具源码安装部署过程

程序员文章站 2022-04-01 08:01:38
ngrinderr(version: 3.4.1)是naver(韩国最大互联网公司nhn旗下搜索引擎网站)开源的性能测试工具,直接部署成web服务,支持多用户使用,可扩展性好,可自定义plugin。n...

ngrinderr(version: 3.4.1)是naver(韩国最大互联网公司nhn旗下搜索引擎网站)开源的性能测试工具,直接部署成web服务,支持多用户使用,可扩展性好,可自定义plugin。

ngrinder 是一款在一系列机器上执行 groovy 或 jython 测试脚本的应用,内部引擎是基于 grinder。 ngrinder 使用 controller 和 agent 分别包装了 grinder 的 console 和 agent ,而且扩展了多种功能使其能够支持并发测试。

ngrinder 由两个主要的组件组成

  • controller

提供性能测试的web接口。
协调测试进程。
整理和显示测试的统计结果
让用户创建和修改脚本。

  • agent

在代理服务器上加载运行测试进程和线程。
监控目标机器的系统性能(例如:cpu/memory/网卡/磁盘)

nGrinder性能工具源码安装部署过程

一、前言

  • 为了更好了解 ngrinder 怎么工作?
  • 为二次开发做准备

 二、源码下载

下载地址:https://github.com/naver/ngrinder/releases

nGrinder性能工具源码安装部署过程

也可以直接通过: 方式

nGrinder性能工具源码安装部署过程

三、本地配置

这我们演示直接使用下载 zip 包进行安装:

nGrinder性能工具源码安装部署过程

打开目录启动脚本:

nGrinder性能工具源码安装部署过程

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

nGrinder性能工具源码安装部署过程

四、idea 设置

打开 idea 开发工具:

nGrinder性能工具源码安装部署过程

点击文件导入 project:

nGrinder性能工具源码安装部署过程

点击 open as project:

nGrinder性能工具源码安装部署过程

打开一个新窗口:

nGrinder性能工具源码安装部署过程

等待 maven 加载相应的 jar。

修改代码:

nGrinder性能工具源码安装部署过程

具体代码如下:

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:

nGrinder性能工具源码安装部署过程

选择运行方式:

nGrinder性能工具源码安装部署过程
nGrinder性能工具源码安装部署过程
nGrinder性能工具源码安装部署过程

选择时时更新运行:

nGrinder性能工具源码安装部署过程
nGrinder性能工具源码安装部署过程

注意最好是加上 jvm 启动参数:

-xms1024m -xmx1024m -xx:maxpermsize=200m

防止内存出现异常

nGrinder性能工具源码安装部署过程

点击确定:

nGrinder性能工具源码安装部署过程

启动项目:

nGrinder性能工具源码安装部署过程

五、启动验证

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

nGrinder性能工具源码安装部署过程

登录成功:

nGrinder性能工具源码安装部署过程

六、使用源码调试简单脚本

script-sample工程下的 pom.xml文件增加:

nGrinder性能工具源码安装部署过程

代码如下:

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
   <groupid>junit</groupid>
   <artifactid>junit</artifactid>
   <version>4.12</version>
   <scope>test</scope>
</dependency>

再次在 idea 中全局搜索:

groovy-all

nGrinder性能工具源码安装部署过程

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

<version>2.4.16</version>

七、模仿编写脚本

通过平台生成脚本:

nGrinder性能工具源码安装部署过程

点击 r head

nGrinder性能工具源码安装部署过程

查看脚本:

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 中新建脚本:

nGrinder性能工具源码安装部署过程

选择 groovy 脚本:

nGrinder性能工具源码安装部署过程

输入名字点击保存即可:

nGrinder性能工具源码安装部署过程

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

nGrinder性能工具源码安装部署过程

点击运行:

nGrinder性能工具源码安装部署过程

可以看到提示:

nGrinder性能工具源码安装部署过程

在 idea 菜单栏->run->edit configurations->default->junit->在vm options 填写自定义配置,点击 apply 按钮保存配置即生效:

nGrinder性能工具源码安装部署过程

再次点击:

nGrinder性能工具源码安装部署过程

运行结果如下:

nGrinder性能工具源码安装部署过程

到这里本机脚本调试成功。

八、小结

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

以上就是性能工具之 ngrinder 源码安装的详细内容,更多关于ngrinder 源码安装的资料请关注其它相关文章!