在做性能测试,脚本之间的关联是一个比较棘手的问题,nGrinder脚本是怎么关联,其实也是比较简单的,简单前提条件是自己具备一定的知识,也就是需要代码基础、http协议知识、网络知识等这些基础知识,这样才能开展工作。
想了解更多、更详细关联知识请查找相关资料。
在编写nGrinder脚本之前请学习下groovy语法这样方便写脚本,脚本编写建议在idea中上写脚本与调试脚本,这样有语法提示能很快写出脚本与调试脚本,写完脚本后直接复制到线上脚本中在微调验证就能使用。
本次脚本编写与调试需要解析JSON所以需要上传fastjson-1.2.62.jar用例解析JSON脚本,下载地址为:
https://mvnrepository.com/artifact/com.alibaba/fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
1、如果是线上直接写脚本需要上传jar:
在脚本页面的脚本文件夹中新建lib文件夹,之后再lib文件中上传相关的jar包,如下图:
点击文件夹:
注意:一定在脚本文件相关的地方新建lib文件夹,并且在lib下中上传jar如:
2、如果是idea中写代码与调试脚本,需要在脚本文件中新建lib文件夹之后在把jar包加入工程中去如:
相关工程:https://github.com/357712148/nGrinder/blob/master/script-sample/test-with-login/OneAndTwo.groovy
工程下载地址:
https://github.com/357712148/nGrinder.git
点击
再次点击:
选择脚本工程
再次选择:
上面操作后即可实现代码与调试脚本,如果还是有问题,自己微调即可。
线上调试:
import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import com.alibaba.fastjson.JSONArray
import groovy.json.JsonParser
import groovy.json.JsonSlurper
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 com.alibaba.fastjson.JSONObject
import static net.grinder.script.Grinder.grinder
import static org.hamcrest.Matchers.is
// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3
import static org.junit.Assert.assertThat
/**
* @Title: OneAndTwo* @Description: this is
* @author liwen* @date 2019/10/27 / 11:00
*/
@RunWith(GrinderRunner)
class OneAndTwo {
public static GTest test
// 定义 HTTPRequest 静态变量 request,用于发送 HTTP 请求
public static HTTPRequest request
// 定义 NVPair 数组 headers ,用于存放通用的请求头数据
public static NVPair[] headers = []
// 定义 NVPair 数组 params ,用于存放请求参数数据
public static NVPair[] params = []
// 定义 Cookie 数组 cookies ,用于存放通用的 cookie 数据
public static Cookie[] cookies = []
//存储第一个请求得参数
def paramName = new ArrayList()
@BeforeProcess
public static void beforeProcess() {
// 设置请求响应超时时间(ms)
HTTPPluginControl.getConnectionDefaults().timeout = 6000
// 创建GTest对象,第一个参数1代表有多个请求/事务时的执行顺序ID,
// 第二个参数是请求/事务的名称,会显示在summary结果中,有多个请求/事务时,要创建多个GTest对象
test = new GTest(1, "User_find_01")
//创建 HTTPRequest 对象,用于发起 HTTP 请求
request = new HTTPRequest()
// Set header datas
List<NVPair> headerList = new ArrayList<NVPair>()
headerList.add(new NVPair("Content-Type", "application/x-www-form-urlencoded"))
headerList.add(new NVPair("Connection", "keep-alive"))
headers = headerList.toArray()
// Set param datas
// List<Cookie> cookieList = new ArrayList<Cookie>()
// cookieList.add(new Cookie("Cookie", "null", "localhost:8888", "", new Date(), true))
// cookies = cookieList.toArray()
grinder.logger.info("before process.");
}
@BeforeThread
public void beforeThread() {
// 注册事件,启动test,第二个参数要与@Test注解的方法名保持一致,有多个请求/事务时,要注册多个事件
test.record(this, "test")
//配置延迟报告统计结果
grinder.statistics.delayReports = true;
grinder.logger.info("before thread.");
}
@Before
public void before() {
//在这里可以添加headers属性和cookies
// request.setHeaders(headers)
cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
grinder.logger.info("before thread. init headers and cookies");
}
@Test
public void test() {
getUserFind()
getItem()
}
public void getUserFind() {
// 发送GET请求
HTTPResponse resu = request.GET("http://localhost:8888/userfind")
def text1 = resu.getText()
JSONObject jsonObject = JSONObject.parseObject(text1);
JSONObject object = jsonObject.getJSONObject("extend")
JSONArray array = object.getJSONArray("info")
for (int i = 0; i < array.size(); i++) {
JSONObject object1 = JSONObject.parseObject(array.get(i).toString())
Object name = object1.get("userName");
paramName.add(String.valueOf(name))
}
assertThat(resu.statusCode, is(200))
}
public void getItem() {
List<NVPair> paramList = new ArrayList<NVPair>()
//获取参数的第一个值
paramList.add(new NVPair("userName", paramName.get(0)))
params = paramList.toArray()
// Set cookie datas
HTTPResponse result = request.GET("http://localhost:8888/findName", params)
def text = result.getText()
grinder.logger.info("这是第二请求" + text)
// 断言HTTP请求状态码
assertThat(result.statusCode, is(200))
}
}
这是相应的测试工程代码下载地址:
https://github.com/357712148/bodygit.git
代码截图: