nGrinder Loadrunner vs nGrinder

s

d

功能 参数类型 取值方式 迭代方式 Loadrunner实现方式 nGrinder实现方式
参数化 文件  sequential

(顺序取值)

 

Each Iteration

(每次迭代)

 在参数列表中设置,按照参数化的数据顺序,一个一个的来取,取到最后一行时继续从头开始取。

迭代一次值改变一次,迭代内值不变

 


变量声明:
public static List<String> lineList //存放参数文件记录
public def custno
public def lineNumber = 0
读取文件,在BeforeProcess块中定义:
lineList = new File("./resources/custno.dat").readLines("UTF-8")
顺序取值,在具体某个Test()中定义一次:
lineNumber = lineNumber%(lineList.size()-1)+1 //考虑了0行是列名行的情况,实际值从第1行开始
custno = lineList.get(lineNumber)

参数化 文件  sequential

(顺序取值)

 

Each Occurrence
(每次出现)

 在参数列表中设置,按照参数化的数据顺序,一个一个的来取,取到最后一行时继续从头开始取。

每次出现的值都不一样

 

变量声明:
public static List<String> lineList //存放参数文件记录
public def custno
public def lineNumber = 0
读取文件,在BeforeProcess块中定义:
lineList = new File("./resources/custno.dat").readLines("UTF-8")
顺序取值,在Test块中各出现处插入如下程序:
lineNumber = lineNumber%(lineList.size()-1)+1 //考虑了0行是列名行的情况,实际值从第1行开始
custno = lineList.get(lineNumber)

参数化 文件  sequential

(顺序取值)

 Once(只取一次)  在参数列表中设置,实际只取第一行  lineNumber取固定值1
参数化 文件  Random

(随机取值)

 Each Iteration

(每次迭代)

 在参数列表中设置,随机取值  

变量声明:
public static List<String> lineList //存放参数文件记录
public def custno
public def rowNumber
读取文件,在BeforeProcess块中定义:
lineList = new File("./resources/custno.dat").readLines("UTF-8")
随机取值,在某一个test中定义:
def rowNumbertemp = new Random().nextInt(lineList.size())
if(rowNumbertemp == 0) {
rowNumbertemp = 1
}
rowNumber = rowNumbertemp
custno = lineList.get(rowNumber) //在其他需要使用的地方直接用rowNumber参数

   文件  Random

(随机取值)

 

Each Occurrence
(每次出现)

 在参数列表中设置,每次出现的值都不一样  

在出现处插入如下程序:
def rowNumber = new Random().nextInt(lineList.size())
if(rowNumber == 0) {
rowNumber = 1
}
custno = lineList.get(rowNumber)

   文件  Random

(随机取值)

 Once(只取一次)  在参数列表中设置,每个虚拟用户值不一样,但每次迭代取值都一样  

在BeforeThread处插入如下程序:
def rowNumber = new Random().nextInt(lineList.size())
if(rowNumber == 0) {
rowNumber = 1
}
custno = lineList.get(rowNumber)

   文件  

Unique
(唯一性取值)

 

Each Iteration
(每次迭代)

 在参数列表中设置,结束策略有三种,分别为:1、AbortVuser;2、ContinueInCycleManner;3、ContinueWithLastValue。

通过下拉框选择结束策略

 

参数文件第一行是标题
文件最后不留空行
编码实现唯一性取值,参考TestRunner.groovy的代码:
首先声明全局类变量
//声明类的全局变量
public static final List<String> usernameList = new File("./resources/username.txt").readLines()

private enum WhenOutOfValues {
AbortVuser, ContinueInCycleManner, ContinueWithLastValue
}
private int invokeTimes = 0 //调用方法计数,不可随意修改
//迭代唯一取值方法
//update on Each Iteration
private int getCounterByIteration() {
assertTrue(grinder.runNumber >= 0)
int counter = getCounter(grinder.runNumber)
int line2 =counter + 1 ;
int MaxLine = usernameList.size() - 1;
WhenOutOfValues outHandler = WhenOutOfValues.AbortVuser
if (line2 > MaxLine) {
if (outHandler.equals(WhenOutOfValues.AbortVuser)) {
grinder.stopThisWorkerThread()
} else if (outHandler.equals(WhenOutOfValues.ContinueInCycleManner)) {
line2 = (line2 - 1) % MaxLine + 1
} else if (outHandler.equals(WhenOutOfValues.ContinueWithLastValue)) {
line2 = MaxLine;
}
}
return line2
}
//取唯一值的计算方法
private int getCounter(int iteration){
int intAgents = Integer.parseInt(grinder.getProperties().get("grinder.agents").toString())
int intProcs = Integer.parseInt(grinder.properties.get("grinder.processes").toString())
int intThreads = Integer.parseInt(grinder.properties.get("grinder.threads").toString())
int agent_no = grinder.agentNumber
int proc_no = grinder.processNumber
int thread_no = grinder.threadNumber

int counter = agent_no * intProcs * intThreads + proc_no * intThreads + thread_no + intAgents * intProcs * intThreads * iteration
return counter
}
在@Test方法内部调用,既可只调用一次,将值赋给一个全局变量,也可多次调用,一次迭代里多次调用的值均一样
int line = this.getCounterByIteration();
String name2 = usernameList.get(line)

   文件  Unique
(唯一性取值)
 

Each Occurrence
(每次出现)

 在参数列表中设置,结束策略有三种,分别为:1、AbortVuser;2、ContinueInCycleManner;3、ContinueWithLastValue。通过下拉框选择结束策略  

参数文件第一行是标题
文件最后不留空行
编码实现唯一性取值,参考TestRunner.groovy的代码:
首先声明全局类变量
//声明类的全局变量
public static final List<String> usernameList = new File("./resources/username.txt").readLines()
private enum WhenOutOfValues {
AbortVuser, ContinueInCycleManner, ContinueWithLastValue
}
private int invokeTimes = 0 //调用方法计数,不可随意修改

//update on Each occurrrence
private int getCounterByOccurrence() {
int counter = getCounter(invokeTimes)
invokeTimes++
int line2 =counter + 1 ;
int MaxLine = usernameList.size() - 1;
WhenOutOfValues outHandler = WhenOutOfValues.AbortVuser
if (line2 > MaxLine) {
if (outHandler.equals(WhenOutOfValues.AbortVuser)) {
grinder.stopThisWorkerThread()
} else if (outHandler.equals(WhenOutOfValues.ContinueInCycleManner)) {
line2 = (line2 - 1) % MaxLine + 1
} else if (outHandler.equals(WhenOutOfValues.ContinueWithLastValue)) {
line2 = MaxLine;
}
}
return line2
}
//取唯一值的计算方法
private int getCounter(int iteration){
int intAgents = Integer.parseInt(grinder.getProperties().get("grinder.agents").toString())
int intProcs = Integer.parseInt(grinder.properties.get("grinder.processes").toString())
int intThreads = Integer.parseInt(grinder.properties.get("grinder.threads").toString())
int agent_no = grinder.agentNumber
int proc_no = grinder.processNumber
int thread_no = grinder.threadNumber

int counter = agent_no * intProcs * intThreads + proc_no * intThreads + thread_no + intAgents * intProcs * intThreads * iteration
return counter
}
在@Test方法内部各出现处调用
int line = this.getCounterByOccurrence();
String name = usernameList.get(line)

   文件  Unique
(唯一性取值)
 Once(只取一次)  -  

参数文件第一行是标题
文件最后不留空行
编码实现唯一性取值,参考TestRunner.groovy的代码:
首先声明全局类变量
//声明类的全局变量
public static final List<String> usernameList = new File("./resources/username.txt").readLines()
private enum WhenOutOfValues {
AbortVuser, ContinueInCycleManner, ContinueWithLastValue
}
private int invokeTimes = 0 //调用方法计数,不可随意修改
//迭代唯一取值方法
//update Once
private int getCounterByOnce() {
int counter = getCounter(0)
int line2 =counter + 1 ;
int MaxLine = usernameList.size() - 1;
if (line2 > MaxLine) {
if (outHandler.equals(WhenOutOfValues.AbortVuser)) {
grinder.stopThisWorkerThread()
} else if (outHandler.equals(WhenOutOfValues.ContinueInCycleManner)) {
line2 = (line2 - 1) % MaxLine + 1
} else if (outHandler.equals(WhenOutOfValues.ContinueWithLastValue)) {
line2 = MaxLine;
}
}
return line2
}

//取唯一值的计算方法
private int getCounter(int iteration){
int intAgents = Integer.parseInt(grinder.getProperties().get("grinder.agents").toString())
int intProcs = Integer.parseInt(grinder.properties.get("grinder.processes").toString())
int intThreads = Integer.parseInt(grinder.properties.get("grinder.threads").toString())
int agent_no = grinder.agentNumber
int proc_no = grinder.processNumber
int thread_no = grinder.threadNumber

int counter = agent_no * intProcs * intThreads + proc_no * intThreads + thread_no + intAgents * intProcs * intThreads * iteration
return counter
}
在@Test方法内部调用
int line = this.getCounterByOnce();
String name = usernameList.get(line)

   文件  同行取值  -  支持  

变量声明:
public static List<String> cityLineList //存放参数文件记录
读取文件,在BeforeProcess块中定义:
cityLineList = new File("./resources/City.txt").readLines("UTF-8")
//x、y与city相关,需要同行取值,在Test方法内,city赋值处进行x、y的赋值
city = cityLineList.get(cityRowNumber).split(",")[1]
x = cityLineList.get(cityRowNumber).split(",")[5]
y = cityLineList.get(cityRowNumber).split(",")[4]

  文件  文件读取方式  按列号取值  支持  支持,见同行取值中的[]
  文件   文件读取方式  按列名取值  支持  不支持
  文件    文件读取方式  文件列分隔符   支持逗号、tab键、空格做分隔符  支持所有分隔符,见同行取值中的.split(",")
   文件    文件读取方式  

从第几行开始取值

 支持从任何一行开始  支持从任何一行开始,见同行取值中cityRowNumber + n;同时cityLineList.size() - n
   文件  参数模拟(simulate parameter) -  支持  支持,将Test方法中实际发送的请求注释,并将取值打印到日志中grinder.logger.info("x:"+x); 运行,查看日志中实际取值
   Date/Time  原值  

Each Occurrence
(每次出现)

 

参数类型选择Date/Time
选择格式化字符串,例如
%Y-%m-%dT%H:%M:%S.000得到结果是
2017-10-31T16:27:10.993

 

在Test()出现处插入如下程序:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:SSS");
String formatStr =formatter.format(new Date());
//输出结果01-11-2017 16:51:11:251
可以指定不同的日期格式化字符串

    Date/Time  增加偏移量  

Each Occurrence
(每次出现)

 日期时间参数,指定offset如增加1天和8小时,通过界面设置完成  

在Test()出现处插入如下程序:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:SSS");
Date today = new Date()
Calendar c = Calendar.getInstance();
c.setTime(today);
c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天
c.add(Calendar.HOUR,8)//今天+8小时
Date tomorrow = c.getTime();
String formatStr =formatter.format(tomorrow);

   Group Name  -  -  参数类型选择Group Name,场景中设置group的名称。  代码指定变量值即可。
   Iteration Number  -  -  当前某个agent某一进程下某线程已执行的迭代次数,从1开始计数,每次迭代加1  grinder.runNumber,从0开始
  Load Generator Name - - 参数设置为Load Generator Name InetAddress.getLocalHost().getHostName()
  Random Number - - 参数设置类型Random,[min,max],包含最小值和最大值的闭区间,默认1~100 new Random().nextInt(MAX),0~MAX的区间,包含0不包含MAX
  Table - - 支持 暂未实现
  Unique Number -

Each Iteration
(每次迭代)

参数类型设置为"Unique Number"
指定取值范围,范围默认1-100
超出范围后的方法可选:终止;循环;取最后一次的结果

private int invokeTimes = 0 //调用方法计数,不可随意修改
private enum WhenOutOfValues {
AbortVuser, ContinueInCycleManner, ContinueWithLastValue
}
//迭代唯一取值方法
//update on Each occurrrence
private int getCounterByOccurrence() {
int counter = getCounter(invokeTimes)
invokeTimes++
return counter + 1
}
//取唯一值的计算方法
private int getCounter(int iteration){
int intAgents = Integer.parseInt(grinder.getProperties().get("grinder.agents").toString())
int intProcs = Integer.parseInt(grinder.properties.get("grinder.processes").toString())
int intThreads = Integer.parseInt(grinder.properties.get("grinder.threads").toString())
int agent_no = grinder.agentNumber
int proc_no = grinder.processNumber
int thread_no = grinder.threadNumber

int counter = agent_no * intProcs * intThreads + proc_no * intThreads + thread_no + intAgents * intProcs * intThreads * iteration
return counter
}
在@Test方法内部调用
int line2 = this.getCounterByOccurrence();
int MaxLine = 100;
WhenOutOfValues outHandler = WhenOutOfValues.ContinueInCycleManner

if (line2 > MaxLine) {
if (outHandler.equals(WhenOutOfValues.AbortVuser)) {
grinder.stopThisWorkerThread()
} else if (outHandler.equals(WhenOutOfValues.ContinueInCycleManner)) {
line2 = line2 % MaxLine + 1
} else if (outHandler.equals(WhenOutOfValues.ContinueWithLastValue)) {
line2 = MaxLine;
}
}

  Unique Number -

Each Occurrence
(每次出现)

 

参数类型设置为"Unique Number"
指定取值范围,范围默认1-100
超出范围后的方法可选:终止;循环;取最后一次的结果

 

private int invokeTimes = 0 //调用方法计数,不可随意修改
private enum WhenOutOfValues {
AbortVuser, ContinueInCycleManner, ContinueWithLastValue
}
//迭代唯一取值方法
//update on Each occurrrence
private int getCounterByOccurrence() {
int counter = getCounter(invokeTimes)
invokeTimes++
return counter + 1
}
//取唯一值的计算方法
private int getCounter(int iteration){
int intAgents = Integer.parseInt(grinder.getProperties().get("grinder.agents").toString())
int intProcs = Integer.parseInt(grinder.properties.get("grinder.processes").toString())
int intThreads = Integer.parseInt(grinder.properties.get("grinder.threads").toString())
int agent_no = grinder.agentNumber
int proc_no = grinder.processNumber
int thread_no = grinder.threadNumber

int counter = agent_no * intProcs * intThreads + proc_no * intThreads + thread_no + intAgents * intProcs * intThreads * iteration
return counter
}
在@Test方法内部调用
int line2 = this.getCounterByOccurrence();
int MaxLine = 100;
WhenOutOfValues outHandler = WhenOutOfValues.ContinueInCycleManner

if (line2 > MaxLine) {
if (outHandler.equals(WhenOutOfValues.AbortVuser)) {
grinder.stopThisWorkerThread()
} else if (outHandler.equals(WhenOutOfValues.ContinueInCycleManner)) {
line2 = line2 % MaxLine + 1
} else if (outHandler.equals(WhenOutOfValues.ContinueWithLastValue)) {
line2 = MaxLine;
}
}

   Unique Number  -  Once(只取一次)  

参数类型设置为"Unique Number"
指定取值范围,范围默认1-100
超出范围后的方法可选:终止;循环;取最后一次的结果

 

全局代码
private int invokeTimes = 0 //调用方法计数,不可随意修改
private enum WhenOutOfValues {
AbortVuser, ContinueInCycleManner, ContinueWithLastValue
}
//update Once
private int getCounterByOnce() {
int counter = getCounter(0)
return counter + 1
}
//取唯一值的计算方法
private int getCounter(int iteration){
int intAgents = Integer.parseInt(grinder.getProperties().get("grinder.agents").toString())
int intProcs = Integer.parseInt(grinder.properties.get("grinder.processes").toString())
int intThreads = Integer.parseInt(grinder.properties.get("grinder.threads").toString())
int agent_no = grinder.agentNumber
int proc_no = grinder.processNumber
int thread_no = grinder.threadNumber

int counter = agent_no * intProcs * intThreads + proc_no * intThreads + thread_no + intAgents * intProcs * intThreads * iteration
return counter
}
在@Test方法内部的代码
int line2 = this.getCounterByOnce();
int MaxLine = 100;
WhenOutOfValues outHandler = WhenOutOfValues.ContinueInCycleManner

if (line2 > MaxLine) {
if (outHandler.equals(WhenOutOfValues.AbortVuser)) {
grinder.stopThisWorkerThread()
} else if (outHandler.equals(WhenOutOfValues.ContinueInCycleManner)) {
line2 = line2 % MaxLine + 1
} else if (outHandler.equals(WhenOutOfValues.ContinueWithLastValue)) {
line2 = MaxLine;
}
}

           
           
           
           
           

end

原文地址:https://www.cnblogs.com/lindows/p/10517839.html