Increment user defined variable in JMeter

Suhas Deshpande picture Suhas Deshpande · Nov 24, 2014 · Viewed 15.5k times · Source

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 ??

Answer

Dmitri T picture Dmitri T · Nov 24, 2014

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.