I am trying to pick up a variable from Debug Sampler and replace the characters of the variables with some Randomly generated number. I have added a BeanShell Sampler for writing the custom code. Below is the piece of code:
String myvariable = vars.get("Corr_ContextN"); chars1 = new ArrayList(); chars2 = new ArrayList(); for(int i =0; i<myvariable.length(); i++) { chars1.add(myvariable.charAt(i)); } String value = chars1.toString(); Random randomnumber = new Random(); for (int idx=1; idx < 15; ++idx) { chars2.add(randomnumber.nextInt(100)); } String Newvalue1 = chars2.toString(); vars.put("NewVariable", Newvalue1 );
By the above way I get a New variable in the Debug Sampler (NewVariable) with a list of random numbers. But I want to replace the existing variable "Corr_ContextN" with this NewVariable created. In other words the existing variable should be replaced by some dynamically generated number/variable.
Please help me out.
Just change the last line to be:
vars.put("Corr_ContextN", Newvalue1 );
P.S. Consider using JSR223 Sampler instead of Beanshell. You can even reuse the same code which will perform much better being compiled by Groovy engine.
P.P.S. Be informed that much easier would be using __Random function which is capable of storing randomly generated numbers into JMeter Variables as well as returning them in place where the function is called. Check out How to Use JMeter Functions post series to learn about functions in JMeter.