The clever use of Jmeter Bean Shell-dynamically setting the number of cycles

Requirements: according to the interface return value (such as 9) to request another interface in a loop, how to set the number of loops, and get the index of the loop

Solutions:

Actual solution:

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
	
	String responseData = prev.getResponseDataAsString(); 
	log.info(responseData);
	JSONArray jsonArr = JSONObject.parseObject(responseData).getJSONArray("data");
	int siez = jsonArr.size();
	vars.putObject("size", siez + "");
	

 

${__BeanShell(Integer.parseInt(vars.getObject("size").toString()))}

important point:

1. Vars.put or vars.putObject can not directly set the int type, so naturally the int value cannot be obtained when it is obtained, and when counting, the maximum value needs to be set to ensure that the maximum value can start from 0 each time the loop is completed The value must be int type, otherwise the default is 0 

2. ${__BeanShell()} mainly uses Jmeter's BeanShell function, pay attention to adding two underscores in front

3. For example, the address of the API that variables like vars can use

ctx 地址:http://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html

vars 地址:http://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html

prev地址:http://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html

4. For example, functions like BeanShell

Official address

http://jmeter.apache.org/usermanual/functions.html

Or view translation

http://www.testclass.net/jmeter/jmeter-doc-20/

Or refer to this article

https://www.jianshu.com/p/c66b846e2ada

 

 

 

 

Guess you like

Origin blog.csdn.net/l1509214729/article/details/101375248