基于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 那样有可视化界面,而是完全用代码来实现,还好的是它提供了很多的工具类,不用重复造轮子。
基于Groovy搭建Ngrinder脚本调试环境的更多相关文章
- 基于Groovy编写Ngrinder脚本常用方法
1.生成随机字符串(import org.apache.commons.lang.RandomStringUtils) 数字:RandomStringUtils.randomNumeric(lengt ...
- ok6410[002] ubuntu1604系统下搭配ckermit和dnw基于RAM的裸机程序调试环境
ubuntu1604系统下搭配ckermit和dnw基于RAM的裸机程序调试环境 系统: ubuntu16.04 裸板: 飞凌公司OK6410开发板 目标:搭建基于ubuntu1604系统和基于RA ...
- 基于Eclipse搭建Hadoop源码环境
Hadoop使用ant+ivy组织工程,无法直接导入Eclipse中.本文将介绍如何基于Eclipse搭建Hadoop源码环境. 准备工作 本文使用的操作系统为CentOS.需要的软件版本:hadoo ...
- Xcode搭建真机调试环境 图文实例
本文介绍的Xcode搭建真机调试环境 图文实例,图文并茂,使我们学习起来更方便些,我们先来看内容. AD: 2013云计算架构师峰会超低价抢票中 Xcode搭建真机调试环境 是本文要介绍的内容,不多说 ...
- vscode加MinGw三步搭建c/c++调试环境
vscode加MinGw三步搭建c/c++调试环境 step1:安装vscode.MinGw 1.1 vscod常规安装:https://code.visualstudio.com/ 1.2 MinG ...
- 通过模拟器和ida搭建Android动态调试环境的问题
这几天在学Android的native层逆向.在按照教程用ida搭建动态调试环境时,第一步是把android_server 放到手机里执行,但是在手机里可以,在genymotion模拟器上就提示 no ...
- 基于Visual Studio Code搭建Golang开发调试环境【非转载】
由于对Docker+kubernetes的使用及持续关注,要理解这个平台的原理,势必需要对golang有一定的理解,基于此开始利用业余时间学习go,基础语法看完之后,搭建开发环境肯定是第一步,虽然能g ...
- 搭建基于qemu + eclipse的kernel调试环境(by quqi99)
作者:张华 发表于:2016-02-06版权声明:能够随意转载.转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 ( http://blog.csdn.net/quqi99 ) 使用q ...
- 微信开发 -- 搭建基于ngrok的微信本地调试环境
第一步,安装ngrok客户端 (1)首先先到官网下载个客户端 http://natapp.cn/,选择适合的客户端类型,本人选择的是windows版 (2)下载后,解压,可以看到如下目录: 第二步,开 ...
随机推荐
- 理解Vue.mixin,带你正确的偷懒
关于Vue.mixin在vue官方文档中是这么解释的: 混入 (mixin) 提供了一种非常灵活的方式,来分发 Vue 组件中的可复用功能.一个混入对象可以包含任意组件选项.当组件使用混入对象时,所有 ...
- 暑期——第八周总结(1,安装好hadoop之后访问http://localhost:50070,无法连接【已解决】 2,Hbase命令详解)
所花时间:7天 代码行:800(Java) 博客量:1篇 了解到知识点 : 一:http://localhost:50070无法访问 安装好hadoop之后 输入所有东西都有 可就是访问50070无法 ...
- mysql安装可能遇到的错误和安装过程
http://jingyan.baidu.com/article/8ebacdf02e392a49f65cd52d.html
- SpringBootSecurity学习(11)网页版登录之URL动态权限
动态权限 前面讨论用户登录认证的时候,根据用户名查询用户会将用户拥有的角色一起查询出来,自动实现判断当前登录用户拥有哪些角色.可以说用户与角色之间的动态配置和判断security做的非常不错.不过在配 ...
- [ProblemSolving][Ubuntu][LyX] The selected document class ... requires external files that are not available...
I installed LyX in my Ubuntu(version LTS 18.04), but I just can't make it work. Every time I open an ...
- Scala 数组和List
Scala 数组和List: import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.Buffer ob ...
- 一致性hash (PHP)
<?php /** * Flexihash - A simple consistent hashing implementation for PHP. * * The MIT License * ...
- 一步步剖析spring bean生命周期
关于spring bean的生命周期,是深入学习spring的基础,也是难点,本篇文章将采用代码+图文结论的方式来阐述spring bean的生命周期,方便大家学习交流. 一 项目结构及源码 1. ...
- Redis info 说明
背景 前面几篇文章介绍完了Redis相关的一些说明,现在看看如何查看Redis的一些性能指标和统计信息,也可以看官网说明. INFO [section] INFO命令返回有关服务器的信息和统计信息,带 ...
- link 和 @import 的区别是什么?
link语法结构: <link href="url" rel="stylesheet" type="text/css"> @im ...