基于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资源目录下,不能为空,否则上传到 试压机跑压测会报错

六、运行报错处理

添加之后就可以运行成功

七、注意:

  1. 依赖jar 拉取很慢,有可能会出现超时,多尝试下
  2. 测试脚本必须是Ngrinder标准的Groovy Maven项目,所以 resources资源目录下,不能为空,我这里随便搞了个文件,服务端会校验目录结构
  3. 目录结构,maven引用等,可以参考Ngrinder官方的例子:https://github.com/naver/ngrinder/wiki/Groovy-Maven-Structure

 八、使用感受:

从零开始搭建了一套本地的 脚本开发环境,我觉得还是特别的快,创建maven 自动引入依赖; 语言开发方面只要熟悉java,上手Groovy 很快就能编写脚本; 编写脚本不像Jmeter 或 loadrunner 那样有可视化界面,而是完全用代码来实现,还好的是它提供了很多的工具类,不用重复造轮子。

猜你喜欢

转载自www.cnblogs.com/unknows/p/11771756.html