Jmeter参数化高阶运用JSR223 Sampler,日期根据线程数递增批量造数据

先来看看官方解释:

The JSR223 Sampler allows JSR223 script code to be used to perform a sample or some computation required to create/update variables.

 

JSR223 Test Elements using Script file or Script text + checked Cache compiled script if available are now compiled if ScriptEngine supports this feature, this enables great performance enhancements.

  

 

 

 

JMeter processes function and variable references before passing the script field to the interpreter, so the references will only be resolved once. Variable and function references in script files will be passed verbatim to the interpreter, which is likely to cause a syntax error. In order to use runtime variables, please use the appropriate props methods, e.g.

props.get("START.HMS");
props.put("PROP1","1234");



实战内容
需求:根据每天的日期,线程组进行递增+1,
根据线程组的递增,日期也随着递增,从而出现多少个线程,就在当前数量上递增日期,
比如当前日期2019-08-07,线程组1,那么就在当前日期+1的日期上进行插入一条数据;
如果需要压测 10个线程执行,那么就会在当前日期+10天之后,每一天都会插入数据,

var n=${__threadNum};             

function dateAdd(startDate,n) {
        startDate = new Date(startDate);
        startDate = +startDate + 1000*60*60*24*n;
        startDate = new Date(startDate);
        var nextStartDate = startDate.getFullYear()+"-"+(startDate.getMonth()+1)+"-"+startDate.getDate();
        return nextStartDate;
    }
vars.put('n',${__threadNum});
a = dateAdd('2019-08-07',n);
vars.put('a',a);

 


如果还不明白可以直接debug看看数据就清楚了
首先,线程组压测10个线程

 

 


JSR223 Sampler 定义好日期的开始时间


 

 



核对每一次debug的结果

第一次请求

 

第一次返回

 

 

 

 

第二次请求

 

 

 

 

第二次返回

 

 

 

 

 

即,当设置了10个线程压测,每跑一次线程,“n”就会根据线程数来定义值

 

第一个线程执行,“n”就是1

第二个线程执行,“n”就是2

......

以此类推

 

 

 

 

第10次,就是n=10,那么值就是当前设定的日期上+10

 

 

 

 

最终,在请求中的键值对添加上调用的参数即可

"timeFrom":"${a} 11:59:00","timeTo":"${a} 12:39:00"

 

参数 “${a}” 对应的就是日期

这样就可以实现批量根据日期跑数据了

 

 

 

 



posted @ 2022-04-07 17:36  LaMw  阅读(1287)  评论(0编辑  收藏  举报