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百分位数

步骤:

  1. 将ngrinder 生成的csv文件:output.csv,放到工程src/main/resources下

  2. 执行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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/neven7/article/details/50669459

nGrinder工具进行接口性能测试的更多相关文章

  1. 【原创】相对完整的一套以Jmeter作为工具的性能测试教程(接口性能测试,数据库性能测试以及服务器端性能监测)

    准备工作 jmeter3.1,为什么是3.1,因为它是要配合使用的serveragent所支持的最高版本,下载链接 https://pan.baidu.com/s/1dWu5Ym JMeterPlug ...

  2. jmeter简单的接口性能测试

    原文转自:https://blog.csdn.net/lovesoo/article/details/78579547 Apache JMeter是一款纯java编写负载功能测试和性能测试开源工具软件 ...

  3. JMETER之socket接口性能测试

    公司的**产品经过换代升级,终于要上线了,纯java编码,包括POS(PC/安卓平板)版.WEB版.微信版,各终端通过 Webservice服务共享数据资源,因此Webservice各接口的性能测试就 ...

  4. 性能测试工具 Web Service 性能测试工具比较

    [转自]https://testerhome.com/topics/3003 背景 希望选择一款Web Service性能测试工具,能真实模拟大量用户访问网站时的请求,从而获取服务器当前的请求处理能力 ...

  5. Jmeter Http接口性能测试

    Jmeter Http接口性能测试 1.      启动Jmeter Jmeter下载解压即可使用,Jmeter启动,点击D:\ProgramFiles\jmeter\apache-jmeter-2. ...

  6. 接口性能测试方案 白皮书 V1.0

    一. 性能测试术语解释 1. 响应时间 响应时间即从应用系统发出请求开始,到客户端接收到最后一个字节数据为止所消耗的时间.响应时间按软件的特点再可以细分,如对于一个 C/S 软件的响应时间可以细分为网 ...

  7. Locust 接口性能测试 - 转载一 (后期熟悉实践自己出一套完整的)

    转载大佬   ,.. 另外一篇:https://www.cnblogs.com/imyalost/p/9758189.html记录一下接口性能测试的学习 先熟悉一下概念: Locust是使用Pytho ...

  8. 【技术博客】 利用Postman和Jmeter进行接口性能测试

    利用Postman和Jmeter进行接口性能测试 作者:ZBW 版本:v1.1 在Phylab的开发过程中,对于生成报告接口的性能考量十分重要.原有的Latex接口虽然生成的报告美观,但编译Latex ...

  9. jmeter接口性能测试【CSV文件读取+接口关联+设置集合点】

    一.前言 周计划上安排了个接口性能测试的任务,便开始了职业生涯的第一个接口性能测试... 接口进行压测之前,首先需要调通脚本.有两种方式,一种是通过抓包工具(如fiddler)抓取业务接口:另一种是通 ...

随机推荐

  1. salt 常用命令整理

    salt 常用命令整理 ***********模块*********** 查看模块列表module salt 'minion' sys.list_modules 查看指定module的function ...

  2. HDU 6060 RXD and dividing(dfs 思维)

    RXD and dividing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  3. AM335x内核模块驱动之LED

    在Ubuntu的任意可操作的文件才建立text目录 在text中建立zyr-hello.c: #include<linux/kernel.h> #include<linux/modu ...

  4. 最小生成树---->prim算法的应用 hdu1863

    畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  5. [CodeForces-440D]Berland Federalization

    题目大意: 给你一棵树,你可以删掉一些边,使得分除去的子树中至少有一棵大小为k. 问最少删去多少边,以及删边的具体方案. 思路: 树形DP. f[i][j]表示以i为根,子树中去掉j个点最少要删边的数 ...

  6. DiskFileUpload上传与Spring的CommonsMultipartResolver上传对比

    最近在做一个小小的上传功能竟被虐得体无完肤, 在使用tomcat内置的DiskFileUpload获取前台jsp传来的附件参数和其他表单参数, 竟然出现莫名其妙的乱码问题, 即使tomcat的serv ...

  7. asp.net 将repeater上数据导出到excel

    1,首先得到一个DataTable public DataTable GetTable(string sql) { SqlConnnection con=new SqlConnection(Confi ...

  8. 如何更新phpStorm SVN项目地址

    公司内网服务器换了IP地址,导致phpStorm 中的SVN更新不上去 如何在phpStorm中配置SVN这里就不赘述了,网上很多,自行百度 这里说一下如何更改phpStorm 中SVN地址 找到项目 ...

  9. Inverted bipolar transistor doubles as a signal clamp

    A number of circuits, such as level detectors and AM demodulators, benefit from a rectifier with a l ...

  10. Fixed DC-DC Regulator Output Uses A Digitally Controlled Potentiometer

    http://electronicdesign.com/print/passives/fixed-dc-dc-regulator-output-uses-digitally-controlled-po ...