Update Jmeter variables with beanshell

陌路散爱 提交于 2020-01-11 04:06:06

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/32860529/update-jmeter-variables-with-beanshell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!