nGrinder工具进行接口性能测试
1.背景
之前在这篇文章中性能测试初探—接口性能测试介绍过nGrinder,本文将介绍在nGrinder脚本中使用资源文件中数据作为接口参数和解析生成的CSV结果,生成TPS标准差,TPS波动率,最小/大RT,RT 25/50/75/80/85/90/95/99百分位数(原生结果中无这些结果,这些结果更有利于性能分析)。
2.实现
2-1.创建脚本
如果脚本中需获取参数,可以使用Performance Test菜单下的Test Configuration->Show Advanced Configuration->Parameter输入框中参数值(http://www.cubrid.org/wiki_ngrinder/entry/how-to-pass-a-parameter-to-the-script),但是只支持1-50字符长度;如果参数值较长,可以使用resources(http://www.cubrid.org/wiki_ngrinder/entry/how-to-use-resources), 支持json, csv, txt, properties格式。
脚本示例如下:
定义String[] basic用于存放文件./resources/basicauth.txt中的值(数组元素为文件中的每一行)
HTTP GET:
package org.ngrinder;
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 HTTPClient.HTTPResponse
import HTTPClient.NVPair
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 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 hugang
*/
@RunWith(GrinderRunner)
class TestRunner {
public static GTest test
public static HTTPRequest request
public static File file
public static String[] basic
@BeforeProcess
public static void beforeProcess() {
HTTPPluginControl.getConnectionDefaults().timeout = 6000
test = new GTest(1, "10.75.0.55")
request = new HTTPRequest()
test.record(request);
// 将文件内容转成参数数组
basic = new File("./resources/basicauth.txt") as String[];
// grinder.logger.info(basic[0] + " " + basic[1])
grinder.logger.info("before process.");
}
@BeforeThread
public void beforeThread() {
grinder.statistics.delayReports=true;
grinder.logger.info("before thread.");
}
@Test
public void test(){
// 随机获取auth
int index = (int) (Math.random() * basic.length);
String basicAuth = basic[index];
println(basicAuth);
// GET请求,wiki http://grinder.sourceforge.net/g3/script-javadoc/net/grinder/plugin/http/HTTPRequest.html
// param1: uri, param2: queryData, param3: headers
HTTPResponse result = request.GET("http://10.75.0.55:8080/2/likes/by_me.json",[new NVPair("object_type", "pic")] as NVPair[], [new NVPair("Authorization", basicAuth)] as NVPair[])
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));
// 请求返回的数据
// println(result.text);
// 定义一个事务,接口返回数据校验,是否包含
assertThat(result.text, containsString("\"object_type\":\"pic\""));
}
}
}
HTTP post:
package org.ngrinder;
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 HTTPClient.HTTPResponse
import HTTPClient.NVPair
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 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 hugang
*/
@RunWith(GrinderRunner)
class TestRunner {
public static GTest test
public static HTTPRequest request
public static File file
public static String[] basic
@BeforeProcess
public static void beforeProcess() {
HTTPPluginControl.getConnectionDefaults().timeout = 6000
test = new GTest(1, "10.210.230.28")
request = new HTTPRequest()
test.record(request);
// 将文件内容转成参数数组
basic = new File("./resources/basicauth.txt") as String[];
// grinder.logger.info(basic[0] + " " + basic[1])
grinder.logger.info("before process.");
}
@BeforeThread
public void beforeThread() {
grinder.statistics.delayReports=true;
grinder.logger.info("before thread.");
}
@Test
public void test(){
// 随机获取auth
int index = (int) (Math.random() * basic.length);
String basicAuth = basic[index];
println(basicAuth);
// POST请求,wiki http://grinder.sourceforge.net/g3/script-javadoc/net/grinder/plugin/http/HTTPRequest.html
// param1: uri, param2: queryData, param3: headers
HTTPResponse result = request.POST("http://10.210.230.28/2/statuses/update.json",[new NVPair("status", "text " + Math.random())] as NVPair[], [new NVPair("Authorization", basicAuth)] as NVPair[])
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));
// 请求返回的数据
// println(result.text);
// 定义一个事务,接口返回数据校验,是否包含
assertThat(result.text, containsString("\"created_at\":"));
}
}
}
2-2. 验证脚本
点击Validate Script, 会出现如下信息:
...
2016-02-15 16:56:32,140 INFO Start time is 1455526592140 ms since Epoch
2016-02-15 16:56:32,318 INFO http://10.75.0.55:8080/2/likes/by_me.json?object_type=pic -> 200 OK, 3414 bytes
2016-02-15 16:56:32,342 INFO finished 1 run
2016-02-15 16:56:32,345 INFO elapsed time is 204 ms
2016-02-15 16:56:32,345 INFO Final statistics for this process:
2016-02-15 16:56:32,355 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 32.00 0.00 4.90 3414.00 16735.29 0 1.00 5.00 24.00 "10.75.0.55"
Totals 1 0 32.00 0.00 4.90 3414.00 16735.29 0 1.00 5.00 24.00
...
Tests 为1, Errors 为0 表示脚本验证通过。
2-3.设计场景
3.结果
3.1.汇总信息
如果还需获取详细信息,需下载csv文件:
汇总数据项目地址:https://github.com/neven7/ngrinder-csv-analysis
解析ngrinder csv结果,统计TPS标准差,TPS波动率,最小/大RT,RT 25/50/75/80/85/90/95/99百分位数
步骤:
将ngrinder 生成的csv文件:output.csv,放到工程src/main/resources下
执行src/main/java下ParseCsv.java文件
Console输出结果示例:
TPS平均值:257.88
TPS标准差:33.10
TPS波动率:12.84%
RT平均响应时间:19.43 ms
Min RT:14.90 ms
RT 25百分位数:18.07 ms
RT 50百分位数:19.14 ms
RT 75百分位数:20.33 ms
RT 80百分位数:20.78 ms
RT 85百分位数:21.29 ms
RT 90百分位数:21.86 ms
RT 95百分位数:23.52 ms
RT 99百分位数:25.91 ms
Max RT:46.93 ms
nGrinder工具进行接口性能测试的更多相关文章
- 【原创】相对完整的一套以Jmeter作为工具的性能测试教程(接口性能测试,数据库性能测试以及服务器端性能监测)
准备工作 jmeter3.1,为什么是3.1,因为它是要配合使用的serveragent所支持的最高版本,下载链接 https://pan.baidu.com/s/1dWu5Ym JMeterPlug ...
- jmeter简单的接口性能测试
原文转自:https://blog.csdn.net/lovesoo/article/details/78579547 Apache JMeter是一款纯java编写负载功能测试和性能测试开源工具软件 ...
- JMETER之socket接口性能测试
公司的**产品经过换代升级,终于要上线了,纯java编码,包括POS(PC/安卓平板)版.WEB版.微信版,各终端通过 Webservice服务共享数据资源,因此Webservice各接口的性能测试就 ...
- 性能测试工具 Web Service 性能测试工具比较
[转自]https://testerhome.com/topics/3003 背景 希望选择一款Web Service性能测试工具,能真实模拟大量用户访问网站时的请求,从而获取服务器当前的请求处理能力 ...
- Jmeter Http接口性能测试
Jmeter Http接口性能测试 1. 启动Jmeter Jmeter下载解压即可使用,Jmeter启动,点击D:\ProgramFiles\jmeter\apache-jmeter-2. ...
- 接口性能测试方案 白皮书 V1.0
一. 性能测试术语解释 1. 响应时间 响应时间即从应用系统发出请求开始,到客户端接收到最后一个字节数据为止所消耗的时间.响应时间按软件的特点再可以细分,如对于一个 C/S 软件的响应时间可以细分为网 ...
- Locust 接口性能测试 - 转载一 (后期熟悉实践自己出一套完整的)
转载大佬 ,.. 另外一篇:https://www.cnblogs.com/imyalost/p/9758189.html记录一下接口性能测试的学习 先熟悉一下概念: Locust是使用Pytho ...
- 【技术博客】 利用Postman和Jmeter进行接口性能测试
利用Postman和Jmeter进行接口性能测试 作者:ZBW 版本:v1.1 在Phylab的开发过程中,对于生成报告接口的性能考量十分重要.原有的Latex接口虽然生成的报告美观,但编译Latex ...
- jmeter接口性能测试【CSV文件读取+接口关联+设置集合点】
一.前言 周计划上安排了个接口性能测试的任务,便开始了职业生涯的第一个接口性能测试... 接口进行压测之前,首先需要调通脚本.有两种方式,一种是通过抓包工具(如fiddler)抓取业务接口:另一种是通 ...
随机推荐
- 【JAVAWEB学习笔记】22_ajax:异步校验用户名和站内查询
Js原生Ajax和Jquery的Ajax 学习目标 案例1-异步校验用户名是否存在 案例2-站内查询 一.Ajax概述 1.什么是同步,什么是异步 同步现象:客户端发送请求到服务器端,当服务器返回响应 ...
- shell 文件传 参数
n cross-platform, lowest-common-denominator sh you use: #!/bin/sh value=`cat config.txt` echo " ...
- 阿里云提示Discuz memcache+ssrf GETSHELL漏洞如何解决
一般这个漏洞都是下面文件,source/function/function_core.php 搜索下面代码: $content = preg_replace($_G['setting']['outpu ...
- 【UOJ #107】【APIO 2013】ROBOTS
http://uoj.ac/problem/107 设\(f(l,r,i,j)\)表示\([l,r]\)中的机器人聚集到\((i,j)\)需要花的最小操作数. \(f(l,r,i,j)=\min\le ...
- 【并查集】BZOJ1370- [Baltic2003]Gang团伙
[题目大意] 在某城市里住着n个人,任何两个认识的人不是朋友就是敌人,而且满足: 1. 我朋友的朋友是我的朋友: 2. 我敌人的敌人是我的朋友: 所有是朋友的人组成一个团伙.告诉你关于这n个人的m条信 ...
- 谈HTTPS中间人攻击与证书校验(一)
一.前言 随着安全的普及,https通信应用越发广泛,但是由于对https不熟悉导致开发人员频繁错误的使用https,例如最常见的是未校验https证书从而导致“中间人攻击”,并且由于修复方案也一直是 ...
- [HEOI2013]SAO
题目大意: 一个有向无环图上有n个结点, 现在告诉你n-1个条件(x,y),表示x和y的先后关系. 问原图共有几种可能的拓扑序? 思路: 树形DP. f[i][j]表示对于第i个结点,有j个点在它前面 ...
- 是否排序好了 Exercise07_19
import java.util.Scanner; import java.util.Arrays; /** * @author 冰樱梦 * 时间2018年12月 * 题目:是否排序好了 * */ p ...
- jdk1.6,jdk1.7和jdk1.8多版本切换
目录 一.配置jdk1.7(win7系统) 二.jdk1.8切换回jdk1.7 三.最后强调可能出现不成功的原因 首先要明确jdk一般都是默认安装在C:\Program Files\Java下的,在配 ...
- Java 请求webServce接口 不带参数
最近对接了个webService的接口取数据,从网上良莠不齐的代码中找到了个方法, 具体作者已经记不住是谁了,现在把代码贴出来,希望可以帮到大家,代码如下,简单粗暴 public String get ...