Bootstrap

【nGrinder】性能压测平台记录文档(2)

Jmeter和nGrinder对比

性能压测工具之前使用的是jmeter,这次说的是nGrinder,先直接搬运两者之间的比较。

 创建HTTP压测脚本

第一步:创建压测脚本

另外,如果需要使用json传参的形式,可以将header头中设置成

Content-Type = application/json

第二步:验证脚本,添加断言

import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static 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
import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import groovy.json.JsonSlurper
/**
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 Amei
 */
 @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() {
 // HTTP请求超时时间,单位毫秒
 HTTPPluginControl.getConnectionDefaults().timeout = 6000
 test = new GTest(1, "lvapi.XXXXXXX.com")
 request = new HTTPRequest()
 // Set header datas
 List<NVPair> headerList = new ArrayList<NVPair>()
 headerList.add(new NVPair("user-agent", "LiveMe/4.3.68 (iPhone; iOS 13.3.1; Scale/3.00)"))
 headerList.add(new NVPair("Content-Type", "application/x-www-form-urlencoded"))
 headerList.add(new NVPair("xd", "e9a061661a7b40056fcb792ff6b047005fa3a638"))
 headerList.add(new NVPair("d", "a3a36e4b25e47ecc47bf8ea611ca230a1acd3c89"))
 headerList.add(new NVPair("t", "1623921491103"))
 headers = headerList.toArray()
 // Set param datas
 List<NVPair> paramList = new ArrayList<NVPair>()
 paramList.add(new NVPair("sig", "ffcbb8e0b9f29d65c9be7e5022220683"))
 paramList.add(new NVPair("tmx_session_id", "6b7cefd0fbe74eab98cc62319c5013d2"))
 paramList.add(new NVPair("token", "XXf86f8d0cfb8ba3ec0c7225aee3d0d223"))
 paramList.add(new NVPair("tongdun_black_box", "4oJq1sjb1abaI9e8QdNQZVRf1pRC3VepVpbtYrmlTtZLXCzU4EYp1pVm2tb92qvR3V3WMqVuOD7jRDZB3tJcRX7cXCyoKsQp3CjU1dep2pNkXCzhTEzkZc7kNrVUKsNjKsbTMGFRPTq8LCJl2oIwIcbPUoIiIdNb2UbaI9e8MTYoMpaoMTQrMDQtMDItN9EqMoIiIdBo1sZf1GVU0WrbI9epMDIvLCJsZXJp0WzkI9e8MouoL9285Q=="))
 paramList.add(new NVPair("tuid", "905575739146371072"))
 paramList.add(new NVPair("tz", "GMT 08:00"))
 paramList.add(new NVPair("uid", "905575739146371072"))
 paramList.add(new NVPair("vid", "16239170244781621879"))
 params = paramList.toArray()
 // Set cookie datas
 List<Cookie> cookieList = new ArrayList<Cookie>()
 cookieList.add(new Cookie("", "", "", "", new Date(32503647599000L), 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.POST("https://lvapi.XXXXXXX.com/giftV2/bagGiftList?vercode=4.3.68.2&rel=1&mcc=460&countryCode=CN&mnc=02&cl=zh-Hans-CN&ptvn=2&data=1&ver=4.3.68&os=iOS&cn2=appstore&api=16741140&model=iPhone10%2C3", 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));
    def jsonData = new JsonSlurper().parseText(result.text)
    assertThat(jsonData.status, is("200"))

}
}
}

2021-06-17 06:09:02,041 INFO  The Grinder version 3.9.1
2021-06-17 06:09:02,043 INFO  Java(TM) SE Runtime Environment 1.8.0_291-b10: Java HotSpot(TM) 64-Bit Server VM (25.291-b10, mixed mode) on Linux amd64 3.10.0-1127.el7.x86_64
2021-06-17 06:09:02,044 INFO  time zone is EDT (-0400)
2021-06-17 06:09:02,070 INFO  worker process 0 of agent number 0
2021-06-17 06:09:02,077 INFO  Instrumentation agents: byte code transforming instrumenter for Java; byte code transforming instrumenter for Java
2021-06-17 06:09:02,473 INFO  registered plug-in net.grinder.plugin.http.HTTPPlugin
2021-06-17 06:09:02,485 INFO  before process.
2021-06-17 06:09:02,486 INFO  Running "bagGiftList.groovy" using GroovyScriptEngine running with groovy version: 2.2.1
2021-06-17 06:09:02,512 INFO  before thread.
2021-06-17 06:09:02,512 INFO  starting, will do 1 run
2021-06-17 06:09:02,512 INFO  Start time is 1623924542512 ms since Epoch
2021-06-17 06:09:02,521 INFO  before thread. init headers and cookies
2021-06-17 06:09:04,373 INFO  https://lvapi.XXXXXX.com/giftV2/bagGiftList?vercode=4.3.68.2&rel=1&mcc=460&countryCode=CN&mnc=02&cl=zh-Hans-CN&ptvn=2&data=1&ver=4.3.68&os=iOS&cn2=appstore&api=16741140&model=iPhone10,3 -> 200 OK, 67 bytes
2021-06-17 06:09:04,380 INFO  finished 1 run
2021-06-17 06:09:04,380 INFO  elapsed time is 1868 ms
2021-06-17 06:09:04,381 INFO  Final statistics for this process:
2021-06-17 06:09:04,383 INFO  
             Tests        Errors       Mean Test    Test Time    TPS          Mean         Response     Response     Mean time to Mean time to Mean time to 
                                       Time (ms)    Standard                  response     bytes per    errors       resolve host establish    first byte   
                                                    Deviation                 length       second                                 connection                
                                                    (ms)                                                                                                    

Test 1       1            0            1857.00      0.00         0.54         67.00        35.87        0            54.00        327.00       1844.00       "lvapi.aaalive.com"

Totals       1            0            1857.00      0.00         0.54         67.00        35.87        0            54.00        327.00       1844.00      

  Tests resulting in error only contribute to the Errors column.          
  Statistics for individual tests can be found in the data file, including
  (possibly incomplete) statistics for erroneous tests. Composite tests   
  are marked with () and not included in the totals.                      



2021-06-17 06:09:02,488 INFO  validation-0: Starting threads
2021-06-17 06:09:04,383 INFO  validation-0: Finished
脚本装饰器讲解

本地脚本调试

IntelliJ IDEA安装包下载地址:https://www.jetbrains.com/idea/download/#section=mac

注:如果报错:The subversion command line tools are no longer provided by Xcode,请看文档后面的FQA

FQA

1.报错:The subversion command line tools are no longer provided by Xcode.

参见解决方案:https://blog.csdn.net/wueasy/article/details/105304818

➜ ssh_google svn --version

svn: error: Failed to locate 'svn'.

svn: error: The subversion command line tools are no longer provided by Xcode.

ssh_google brew install svn

相关参考资料

nGrinder框架简介

nGrinder 架构简介_ngrinder3.4 多场景test-CSDN博客

使用手册

https://github.com/naver/ngrinder/wiki

脚本指南

nGrinder 的 Groovy 脚本使用指南(Groovy 脚本结构)_ngrinder3.5 groovy-CSDN博客

Script Gallery

nGrinder - Groovy 脚本指南 · 测试之家

nGrinder 改造 - 在详细报告里增加更多统计项 · 测试之家

nGrinder-Groovy脚本参数化方式收集 - 简书

HTTP 请求详解

nGrinder 的 Groovy 脚本使用指南(HTTP 请求详解)_ngrinder设置思考时间-CSDN博客

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;