Increment user defined variable in JMeter

18.1k views Asked by At

I have a register REST service to be tested for performance. only required parameter mobilenumber has to be changed for every loop/thread request

How can I increment my User defined variable (by increment value stored as another udv) while threading/looping to multiple tests as a part of load test.

Trying to have a jsr 233 pre-processor for all successful register requests

I don't want to use csv as I have only one parameter to be changed

I am reading about var.get() and vars.put() functions which can be used but not getting thorugh it correctly.


Trying the following code - the log in shown below the code

log.info("Before execution")
log.info("${mobileNumber}") // my udv mobileNumber
log.info("${increment}") // my udv increment value

inc = ${increment}

long number = Long.parseLong(vars.get("mobileNumber"))

number = number + inc

log.info("Incremented number")
log.info(String.valueOf(number))

vars.put("mobileNumber", String.valueOf(number))

log.info("After Execution")
log.info("${mobileNumber}")

Log :

2014/11/25 09:25:26 INFO  - jmeter.threads.JMeterThread: Thread started: Dummy services 1-1 
2014/11/25 09:25:26 INFO  - jmeter.protocol.java.sampler.JSR223Sampler: Before execution 
2014/11/25 09:25:26 INFO  - jmeter.protocol.java.sampler.JSR223Sampler: 9990000000 
2014/11/25 09:25:26 INFO  - jmeter.protocol.java.sampler.JSR223Sampler: 1000 
2014/11/25 09:25:26 INFO  - jmeter.protocol.java.sampler.JSR223Sampler: Incremented number 
2014/11/25 09:25:26 INFO  - jmeter.protocol.java.sampler.JSR223Sampler: 9990001000 
2014/11/25 09:25:26 INFO  - jmeter.protocol.java.sampler.JSR223Sampler: After Execution 
2014/11/25 09:25:26 INFO  - jmeter.protocol.java.sampler.JSR223Sampler: 9990000000 
2014/11/25 09:25:26 INFO  - jmeter.threads.JMeterThread: Thread finished: Dummy services 1-1 

As per log, Increment is successful but the vars.put() is not successful ??

4

There are 4 answers

3
Dmitri T On BEST ANSWER

Given than mobile numbers can be up to 15 digits long you need to use Long Java class.

Assuming that your UDV holding mobile number is "number" you can use the following code snippet:

long number = Long.parseLong(vars.get("number")); 
number = number + 1;
vars.put("number", String.valueOf(number));

You can also consider using __RandomString() JMeter Function to generate random number of any length (the function uses String representation).

For extra information on vars, ctx and other JMeter API objects exposes to scripting-enabled test elements refer to How to use BeanShell: JMeter's favorite built-in component guide.

1
olyv On

I usually use BSFPre(Post)processor + Groovy. In this case it would look like

i = ${YOUR_UDV}
i += 1
vars.putObject("YOUR_UDV", i.toString())
0
Suhas Deshpande On

If we are updating a JMeter variable we cannot access the updated value using ${varName} format (Not sure of Reasons). I successfully got the updated value using vars.get("varName"} function.

log.info("Before execution")
log.info("${mobileNumber}") // my udv mobileNumber
log.info("${increment}") // my udv increment value

inc = ${increment}

long number = Long.parseLong(vars.get("mobileNumber"))

number = number + inc

log.info("Incremented number")
log.info(String.valueOf(number))

vars.put("mobileNumber", String.valueOf(number))

log.info("After Execution")
log.info("${mobileNumber}")
log.info(vars.get("mobileNumber"))

Log:

2014/11/26 09:16:06 INFO  - jmeter.protocol.java.sampler.JSR223Sampler: After Execution 
2014/11/26 09:16:06 INFO  - jmeter.protocol.java.sampler.JSR223Sampler: 8762200000 
2014/11/26 09:16:06 INFO  - jmeter.protocol.java.sampler.JSR223Sampler: 8762201000

Thnaks for help Dmtri and olyv

0
AudioBubble On

Universal inc long variable:

long number = Long.parseLong(vars.get(Parameters)); 
number = number + 1;
vars.put(Parameters, String.valueOf(number));

Parameters: Variable name

inc long variable with Groovy