基于Groovy搭建Ngrinder脚本调试环境
介绍
最近公司搭建了一套压力测试平台,引用的是开源的项目 Ngrinder,做了二次开发,在脚本管理方面,去掉官方的SVN,引用的是Git,其他就是做了熔断处理等。
对技术一向充满热情的我,必须先来拥抱下传说中的压测平台。
一、开发脚本环境配置项:
安装JDK1.7+,Git,Maven
二、新建一个maven项目
三、创建一个groovy脚本TestRunner.groovy,添加以下内容
这个脚本写的就是,向服务端发送Json 格式请求,比较简单,未涉及到上下文参数化,混合场景配置比例,方法介绍等,到时需要再写2篇专题
import HTTPClient.HTTPResponse import HTTPClient.NVPair import net.grinder.plugin.http.HTTPPluginControl import net.grinder.plugin.http.HTTPRequest import net.grinder.script.GTest import net.grinder.scriptengine.groovy.junit.GrinderRunner import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread import org.junit.Test import org.junit.runner.RunWith import static net.grinder.script.Grinder.grinder import static org.hamcrest.Matchers.is import static org.junit.Assert.assertThat // 每个测试类加这注解 @RunWith(GrinderRunner) class TestRunner{ public static GTest test public static HTTPRequest request // 在每个进程启动前执行 @BeforeProcess static void beforeProcess() { HTTPPluginControl.getConnectionDefaults().timeout = 8000 test = new GTest(1, "查询贷款数量") request = new HTTPRequest() grinder.logger.info("before process."); } // 在每个线程执行前执行 @BeforeThread void beforeThread() { //监听目标方法,如果打标不会生成该方法的报告 test.record(this,"loanCountTest"); // 延时生成报告 grinder.statistics.delayReports=true; grinder.logger.info("before thread."); } private NVPair[] headers() { return [ new NVPair("Content-type", "application/json;charset=UTF-8") ]; } @Test void loanCountTest(){ def json = "{\"uid\": \"1_1154249\"}"; HTTPResponse result = request.POST("http://hdai.com/query-loaning-count",json.getBytes(), headers()); grinder.logger.info(result.getText()); 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)); } } }
四、拷贝以下内容到新建的 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>ngrinder</groupId> <artifactId>loan-xx-perf</artifactId> <version>0.0.1</version> <properties> <ngrinder.version>3.4</ngrinder.version> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <repositories> <repository> <id>ngrinder.maven.repo</id> <url>https://github.com/naver/ngrinder/raw/ngrinder.maven.repo/releases</url> </repository> <repository> <id>ymm-central-cache</id> <url>http://maven.aliyun.com/nexus/service/local/repositories/central/content/</url> </repository> <repository> <id>aliyun</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.ngrinder</groupId> <artifactId>ngrinder-groovy</artifactId> <version>${ngrinder.version}</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> <additionalProjectnatures> <projectnature> org.eclipse.jdt.groovy.core.groovyNature </projectnature> <projectnature> org.eclipse.m2e.core.maven2Nature </projectnature> </additionalProjectnatures> </configuration> </plugin> </plugins> </build> </project>
五、完成后,目录机构如下
resources资源目录下,不能为空,否则上传到 试压机跑压测会报错
六、运行报错处理
添加之后就可以运行成功
七、注意:
- 依赖jar 拉取很慢,有可能会出现超时,多尝试下
- 测试脚本必须是Ngrinder标准的Groovy Maven项目,所以 resources资源目录下,不能为空,我这里随便搞了个文件,服务端会校验目录结构
- 目录结构,maven引用等,可以参考Ngrinder官方的例子:https://github.com/naver/ngrinder/wiki/Groovy-Maven-Structure
八、使用感受:
从零开始搭建了一套本地的 脚本开发环境,我觉得还是特别的快,创建maven 自动引入依赖; 语言开发方面只要熟悉java,上手Groovy 很快就能编写脚本; 编写脚本不像Jmeter 或 loadrunner 那样有可视化界面,而是完全用代码来实现,还好的是它提供了很多的工具类,不用重复造轮子。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2018-10-31 Java获取时间,将当前时间减一年,减一天,减一个月