1

I've encountered a problem when trying to update a Jmeter variable with a beanshell script. I've followed this manual and i have seen this topic and both say the same:

  1. To update a variable use vars.put("variable", "newValue");
  2. The value that you put can only be a String.

Now I want to use this code:

String x = vars.get("counter");

int y = Integer.parseInt(x);
y = y + 1;

String z = "" + y;

vars.put("counter", z);
// print(z);

My variable counter is a user parameter (tried before as user defined variable) with the value 1. I can see my script is working because the print(z) returns the value 2. Now I also expact that my variable counter is updated in the user parameters so that when I run this again it gives me the value 3. This is not the case: The value is not updated, so everythime I run the script it returns me the value 2.

Anyone who can help me with this?

Community
  • 1
  • 1
Bastian
  • 13
  • 1
  • 1
  • 3

2 Answers2

0

I do not see any issues in your script. It should work fine.

Remember that all these beanshell variables are specific to the thread. Ie, If Thread1 increments it to 2, the current value of 'counter' for the Thread2 will still be 1.

I think you run your test for more threads/users with only one iteration. This is why it prints 2 for all users. If you have more loop counts/set it to forever, counter will get incremented.

You can upload your jmx file if it still does not resolve the issue.


EDIT:

I just checked your jmeter test. Even though you increment the counter value by 1 in the Beanshell Sampler, Yolu are setting the counter back to 1 as part of User Parameters. Remove it. After removing them, It works fine for me.

vins
  • 13,336
  • 2
  • 29
  • 42
  • Thank you very much for your quick reply! Unfortunatly I couldn't get it to work. I have set the loop count to forever, while using just one thread. This didn't help. Also I expect that when I run everything once that my variable "counter" is updated. If I manually run everything once again, it takes my new value, and updates it again. Anyway: Here is my [.jmx file](http://www.filedropper.com/testplan2). I hope you have a solution for this. Thanks in advance! – Bastian Oct 01 '15 at 06:57
  • That's almost it! I see it working now. Super! This might be not possible, but to completely satisfy my needs I would also like to save the latest value that has been generated by the beanshell scirpt in the User defined variables. Do you think this can be done? – Bastian Oct 01 '15 at 11:53
  • What do you mean by you want to save? Beanshell variables scope is within the test duration only. If you want to save the value somewhere you need to write that to the DB/CSV/TXT file..etc You can not save that in JMeter itself!! – vins Oct 01 '15 at 15:05
  • @Bastian, If it works & I have clarified your question, Please accept this as the answer by clicking on the tick mark! – vins Oct 01 '15 at 15:06
0

Put the cookie manager in your script and you will be fine.

Now you have a global variable counter = 1, in one request you use a local variable counter ant set it to 2. When another request tries to pick up the value of counter it gets the global value 1, because the local one is trashed.

Imlus
  • 57
  • 5
  • Additionally don't throw the loop idea away, as threads are independent and start from the default values. – Imlus Oct 01 '15 at 09:02
  • Thanks for your answers! I have deleted the global variable 'counter = 1'. I now just have the local variable 'counter = 1'. I don't know what a cookie manager is doing and how it would help me? Is the idea of a cookie manager that i remean in the same session? The idea of this counter is that I can sent in the same JMS message multiple times per day so that (1) The message is not duplicate and (2) I can see how many times the same message has been sent. – Bastian Oct 01 '15 at 09:43
  • You can look at the cookie manager as your local variable warehouse - it keeps all your local values for that thread(user), like sessionid, local counter and so on. You don't need to delete the global one, you just need the cookie manager to let your thread remember the local one till the session ends. – Imlus Oct 01 '15 at 10:13