性能工具之 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 请求

源码地址:

目录
相关文章
|
数据处理 对象存储
如何利用 OSS 转换 webp 文件为jpeg
如何利用 OSS 转换 webp 文件为jpeg,OSS 样式使用
2709 1
|
12月前
|
Linux
Linux 麒麟系统安装 make rpm 包步骤
下载make-4.3 RPM包,通过终端进入下载目录,使用`sudo yum install ./make-4.3-1.ky10.x86_64.rpm`安装,自动解决依赖,最后运行`make --version`验证是否成功。推荐此方法避免手动处理依赖问题,确保安装顺利。
1936 3
|
9月前
|
人工智能 自然语言处理 前端开发
准时上线:当AI建站邂逅“Just in Time”哲学
本文提出“Just in Time AI建站”新范式,打破传统AI一次性生成的局限,以渐进式共创替代“生成-返工”循环。云指AI在用户需要时(雏形、调优、内容、获客各阶段)提供精准、局部、即时支持,让建站从技术任务回归创作本质——适时、适度、适配,做用户全程陪伴的智能伙伴。
424 5
|
11月前
|
安全 数据可视化 Java
java远程调试在线服务的教程超详细
很多时候,由于本地环境跟服务端环境有差异,或者服务端的数据有差异,造成在服务端运行出错,但是本地不出错的情况,因此,就需要做远程调试。 java是一个很有意思的语言,它有很多好用的工具,就比如,java是支持在线调试的,只要在服务端启动调试端口就行了。 但是很多教程都没有考虑安全问题,很多教程都是直接连接服务端的调试端口,直接暴露调试端口会导致服务端被攻击,因此,最近很多公司因为调试而导致出现安全事故。因此,我们这个教程不单是告诉大家如何调试,还将教会大家如何做安全。安全是远程调试的必修课。
|
12月前
【麒麟Kylin】cmake-3.16.5 rpm包安装步骤详解 附常见问题
本文介绍在麒麟系统上安装CMake 3.16.5的完整步骤:从下载RPM安装包、确认文件位置,到使用终端通过rpm或yum命令安装,并验证版本。适用于初学者快速部署CMake环境。(238字符)
1365 2
|
12月前
|
Linux 编译器 数据安全/隐私保护
gcc-c++-7.3.0 rpm安装方法 Linux麒麟KY10完整步骤
本文介绍在麒麟系统中手动安装 GCC C++ 编译器的完整步骤。从下载 RPM 安装包、确认文件路径,到使用终端通过 `yum` 或 `rpm` 命令安装,涵盖权限处理、依赖解决及版本冲突等常见问题,并提供验证安装方法,确保 `g++` 成功配置。
2274 1
|
12月前
|
存储 运维 监控
日志服务SLS:日志采集与分析
日志服务SLS是阿里云提供的一站式日志解决方案,支持采集、存储、分析、投递全链路管理。通过Logtail、SDK、API实现多场景日志接入,结合查询语法、可视化图表与机器学习,助力运维监控、安全审计与成本优化,广泛应用于Nginx分析、错误排查及智能异常检测,提升企业数字化运营效率。(238字)
1093 0
|
Linux 数据库 数据安全/隐私保护
Python web Django快速入门手册全栈版,共2590字,短小精悍
本教程涵盖Django从安装到数据库模型创建的全流程。第一章介绍Windows、Linux及macOS下虚拟环境搭建与Django安装验证;第二章讲解项目创建、迁移与运行;第三章演示应用APP创建及项目汉化;第四章说明超级用户创建与后台登录;第五章深入数据库模型设计,包括类与表的对应关系及模型创建步骤。内容精炼实用,适合快速入门Django全栈开发。
923 1
|
JavaScript NoSQL 关系型数据库
当下弹幕互动游戏源码开发教程及功能逻辑分析
当下很多游戏开发者或者想学习游戏开发的人,想要了解如何制作弹幕互动游戏,比如直播平台上常见的那种,观众通过发送弹幕来影响游戏进程。需要涵盖教程的步骤和功能逻辑的分析。

热门文章

最新文章