当前位置: 首页 > article >正文

【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博客


http://www.kler.cn/news/367186.html

相关文章:

  • Java-图书管理系统
  • 传奇996_5——使用补丁制作武器
  • 校园表白墙源码修复版
  • el-table相关的功能实现
  • 基于Springboot无人驾驶车辆路径规划系统(源码+定制+开发)
  • 使用 docker 的方式部署 NFS server 提供文件共享能力
  • web3对象如何连接以太网络节点
  • python之数据结构与算法(数据结构篇)-- 集合
  • Redis 事务 总结
  • Docker 安装使用
  • 一文掌握异步web框架FastAPI(五)-- 中间件(测试环境、访问速率限制、请求体解析、自定义认证、重试机制、请求频率统计、路径重写)
  • 三、Hadoop 常用命令集总览
  • facebook账号类型有哪些?
  • 【解决】使用Hypermark将Markdown文件转化为HTML文件
  • Axure PR 9 多级下拉清除选择器 设计交互
  • 图解:什么是多租户?
  • 专题十六_栈_队列_优先级队列_算法专题详细总结
  • 判断自己的mac是macOS x64 还是macOS ARM64
  • ALIGN_ Tuning Multi-mode Token-level Prompt Alignment across Modalities
  • csp-j2024泄题事件
  • huggingface的lora与resume方法训练模型(以BERT为列)
  • unordered_map和unordered_set相关知识详细梳理
  • Linux | 配置docker环境时yum一直出错的解决方法
  • [软件工程]—嵌入式软件开发流程
  • 探索Python安全字符串处理的奥秘:MarkupSafe库揭秘
  • 华为配置 之 端口隔离