性能工具之 nGrinder 源码安装

简介: 【5月更文挑战第2天】性能工具之 nGrinder 源码安装

一、前言

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

二、源码下载

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

image.png
也可以直接通过:https://github.com/naver/ngrinder.git 方式

image.png

三、本地配置

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

打开目录启动脚本:
image.png

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

四、IDEA 设置

打开 IDEA 开发工具:
image.png

点击文件导入 Project:
image.png

点击 Open as Project:
image.png

打开一个新窗口:

image.png
等待 maven 加载相应的 jar。

修改代码:
image.png
具体代码如下:

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:
image.png
选择运行方式:

image.png
image.png
image.png

选择时时更新运行:
image.png
image.png

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

-Xms1024m -Xmx1024m -XX:MaxPermSize=200m

防止内存出现异常

image.png

点击确定:

image.png

启动项目:
image.png

五、启动验证

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

http://localhost:8081/ngrinder/login

image.png

登录成功:
image.png

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

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

代码如下:

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

再次在 idea 中全局搜索:

groovy-all

image.png

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

<version>2.4.16</version>

七、模仿编写脚本

通过平台生成脚本:
image.png

点击 R HEAD

image.png

查看脚本:

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

选择 Groovy 脚本:
image.png
输入名字点击保存即可:
image.png

新建完毕把刚才脚本复制过来修改下方法名称:
image.png
点击运行:
image.png

可以看到提示:
image.png

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

image.png
再次点击:
image.png
运行结果如下:
image.png
到这里本机脚本调试成功。

八、小结

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

源码地址:

目录
相关文章
|
9月前
|
缓存 监控 关系型数据库
《Linux 简易速速上手小册》第10章: 性能监控与优化(2024 最新版)
《Linux 简易速速上手小册》第10章: 性能监控与优化(2024 最新版)
72 0
|
7月前
|
存储 监控 数据可视化
性能测试:主流性能剖析工具介绍
**性能剖析**是识别应用性能瓶颈的关键,涉及指标收集、热点分析、优化建议及可视化报告。常用工具有:**JConsole**监控JVM,**VisualVM**多合一分析,**JStack**分析线程,**FlameGraph**展示CPU耗时,**SkyWalking**分布式跟踪,**Zipkin**追踪服务延迟。这些工具助力开发人员提升系统响应速度和资源效率。
|
6月前
|
测试技术 Apache 开发者
性能测试新纪元!Python携手JMeter与Locust,开启应用性能优化新篇章
【8月更文挑战第6天】应用性能测试是软件开发的关键环节。随着云技术和微服务架构的发展,传统测试方法已难以满足需求。Python 作为一种灵活强大的语言,在性能测试中扮演重要角色。本文探讨 Python 与 Apache JMeter 及 Locust 的结合如何开启性能优化新篇章。JMeter 适用于多种协议的压力测试,而 Locust 用 Python 定义测试场景,两者各具特色。
161 0
|
9月前
|
监控 Java 测试技术
性能工具之 nGrinder 入门安装及使用
【5月更文挑战第1天】性能工具之 nGrinder 入门安装及使用
118 5
性能工具之 nGrinder 入门安装及使用
|
9月前
|
Java 测试技术 Apache
性能工具之JMeter5.0核心源码浅析
【5月更文挑战第14天】性能工具之JMeter5.0核心源码浅析
109 1
性能工具之JMeter5.0核心源码浅析
|
7月前
|
监控 测试技术 C#
推荐一个使用 C# 开发的轻量级压测工具
推荐一个使用 C# 开发的轻量级压测工具
|
7月前
|
监控 Java 测试技术
性能测试:主流压测工具介绍
**性能压测工具摘要:** - Apache AB:命令行基准测试工具,简单轻量,用于评估服务器性能。 - Apache JMeter:开源Java框架,支持多种协议,适合复杂负载测试,提供丰富的图表和报告。 - nGrinder:开源,提供直观界面,支持分布式测试,录制回放脚本,实时监控。 - Locust:Python基础,适用于HTTP/WebSocket测试,可扩展,实时监控。 - LoadRunner:Micro Focus的商业工具,广泛支持多种协议,功能强大。
|
9月前
|
数据采集 缓存 Rust
通过Rust实现公司电脑监控软件的性能优化算法
使用Rust语言开发高效的公司电脑监控软件,通过实时监测CPU、内存、网络等性能数据,确保企业环境的稳定性。文中通过代码示例展示了数据采集模块,如读取CPU使用率,并利用缓存机制减少文件系统访问,提升性能。此外,还介绍了如何将监控数据通过HTTP客户端提交到网站进行分析和管理,以优化运维流程。
276 3
|
9月前
|
算法 Java 测试技术
性能工具之代码级性能测试工具ContiPerf
【2月更文挑战第23天】性能工具之代码级性能测试工具ContiPerf
435 1
性能工具之代码级性能测试工具ContiPerf
|
监控 前端开发 JavaScript