性能测试工具 nGrinder 入门

xinxi · 2020年01月31日 · 最后由 皮一下很开心 回复于 2020年02月18日 · 2829 次阅读

前言

nGrinder 是一个用于在多台机器上运行用 jython(在 JVM 上运行的 python) 编写的测试脚本的应用程序。它的内部引擎是基于 Grinder。nGrinder 分别用控制器和 agent 将 Grinder 的控制台和 agent 包装起来,并扩展了支持多个并发测试的特性.

本文对 nGrinder 做一个入门了解,掌握基本使用.

测试环境

准备自己搭建一套测试环境,这个架构和我们公司的服务架构基本相似.

架构: java + eureka + gateway + app
框架: springclound

eureka 注册中心

把所以微服务注册到 eureka 中,方便后续的服务调度.

gateway 是网关

gateway 是所有的服务的入口,通过路由转发到具体微服务上.

app 是具体的应用

app 是具体的一个微服务应用,具体业务中有会有 N 个微服务.

代码地址

https://github.com/xinxi1990/backendSpring.git

nGrinder 介绍

下面是 nGrinder 的两个介绍,可自行查阅.

https://github.com/naver/ngrinder

http://naver.github.io/ngrinder/

工具对比

因为平时自己都用 jmeter 去做压测,所以找了一张对比图.

image

架构图

image

脚本执行

image

下载 controller

controller 主要是负责展示页面、调度任务的.

https://github.com/naver/ngrinder/releases/tag/ngrinder-3.4.3-20190709
java -XX:MaxPermSize=200m -jar ngrinder-controller-3.4.war --port 999

下载 monitor

monitor 用来监控发压数据

启动 monitor

sh run_monitor.sh

下载 agent

agent 用来发压

image

agent

sh run_agent.sh

nGrinder 平台

nGrinder 提供了一个 web 平台,可以创建任务,执行任务等.

访问:127.0.0.1:9999

账号/密码: admin/admin

image

任务列表

image

任务详情

image

脚本

脚本支持 groovy 和 jpython 两种语言,个人感觉 groovy 要好一点

groovy

import HTTPClient.Cookie
import HTTPClient.CookieModule
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.Before
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
    public static NVPair[] headers = []
    public static NVPair[] params = []
    public static Cookie[] cookies = []

    @BeforeProcess
    public static void beforeProcess() {
        HTTPPluginControl.getConnectionDefaults().timeout = 6000
        test = new GTest(1, "www.baidu.com")
        request = new HTTPRequest()

        // 设置请求头数据
        List<NVPair> headerList = new ArrayList<NVPair>()
        headerList.add(new NVPair("test_header", "test_header_value"))
        headers = headerList.toArray()


        // 设置请求参数
        List<NVPair> paramList = new ArrayList<NVPair>()
        paramList.add(new NVPair("paramname", "vaule"))
        params = paramList.toArray()


        // 设置 cookie 信息
        List<Cookie> cookieList = new ArrayList<Cookie>()
        cookieList.add(new Cookie("username", "testname", "www.baidu.com", "/", new Date(), false))
        cookies = cookieList.toArray()


        // 记录日志
        grinder.logger.info("before process.")
    }

    @BeforeThread
    public void beforeThread() {
        test.record(this, "test")
        // 配置延迟报告统计结果
        grinder.statistics.delayReports = true
        // 记录日志
        grinder.logger.info("before thread.")
    }

    @Before
    public void before() {
        // 设置本次请求头
        request.setHeaders(headers)
        // 设置本次请求的 cookies
        cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
        // 记录日志
        grinder.logger.info("before thread. init headers and cookies")
    }

    @Test
    public void test() {
        HTTPResponse result = request.GET("http://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))
        }
    }
}


报告

在报告中可以看到 tps、曲线图等数据.

image

svn

内置了 svn 地址,可以在 idea 工具中直接导入

image

idea 开发脚本

脚本如下,这个脚本是 fork 了一个韩国那边同学的脚本改量的.

脚本中使用 junit 框架,导入了 grinder 依赖,脚本开发难度一般.

image

https://github.com/xinxi1990/nGrinderScript.git

结语

nGrinder 和 jmeter 在使用上有很大差别,nGrinder 的脚本适用有脚本开发经验的.jmter 支持多协议、入手比较容易.

参考

nGrinder 简易使用教程

https://www.cnblogs.com/jwentest/p/7136727.html

nGrinder - Groovy 脚本指南

https://testerhome.com/topics/12173

性能测试工具-Ngrinder 使用 - 添加一个简单脚本(groovy)

https://www.cnblogs.com/xiaowei89426/p/9356694.html

https://moyadu.oschina.io/diligentpractice/books/chapter-Grinder/nGrinderdevelop/

共收到 3 条回复 时间 点赞

以前在搞 weblogic 自动部署的时候,WLST 用过一段时间 jython,写过几天就忘了,也没因此爱上 python

看到这个真的很怀念上家公司。架构相似百分之 90,相对于 groovy,个人还是更喜欢 jython

槽神 回复

😂 可能没对上你的口味

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册