Not able to update the user-defined variable value in Jmeter - jmeter

I have defined one variable in Jmeter with some default value. I want to update the value of this variable based on certain conditions. I am using bean shell processor to update the value of this variable.
So the scenario which I am trying is, I am executing my test plan for some fixed number of times(say 2 minutes), and I am also counting(using counter) the number of request jmeter is making to the endpoint. So based on the one specific counter's value, I want to update the value of this user-defined variable. So to update this, I am using below line in bean shell processor -
vars.put("variableName","value");
So the above statement is only changing the value of the variable for that specific count, after that, it is again referring to previous value. Like,
if(count==100){
vars.put("variableName","newvalue");
}
So it is updating this value only at the 100th request, and when 101th request comes up, it again having the previous value.
Can someone guide me on, how can I update this value for the variable. Also I am using one thread group with 10 threads.

I cannot reproduce your issue, refer the test plan below for demo:
So I would recommend double-checking your if(count==100) condition, JMeter Variables are normally being stored as Strings so my expectation is that the condition should be amended to look like:
if (vars.get("count").equals("100"))
Also since JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting so consider migrating on next available opportunity.

Related

How set a variable as a parameter value in JMeter

I'm trying to set a variable as a parameter value in a Backend Listener. I tried to add the variable as
${"testname"}
and also as
$["testname"]
but both options didn't work. JMeter consider them as a string and not as a variable. Any ideas how I can do that ?
The correct syntax for JMeter Variables is ${variable_name_here} so you need to change it to ${testname} and it should start working as expected.
If there will still be problems make sure that the variable is defined and has its respective value, it can be done using Debug Sampler and View Results Tree listener combination
P.S. Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting instead of Beanshell (and in fact any other languages) so while your script is relatively small maybe it's a good time for migration.

How to handle escape characters (backslash) in jmeter

while executing the API's using jmeter tool in the response i get some value as '1K\/vyEh'.
while storing this value in the variable it is observed that the backslash does not get stored in the variable resulting in saving the value as '1K/vyEh'
I need guidance of how to save the value as is in the variable
There are too many "unknowns" in your question so unfortunately it is not possible to provide a comprehensive answer without seeing:
at least partial (better full) response
and the way you're storing the value into a JMeter Variable
Here is a quick demo that in theory it is possible:
The demo assumes:
Dummy Sampler
JSR223 PostProcessor
Groovy language and regular expressions

How can I embed a test data set in my JMeter test plan?

At the moment, my JMeter test uses a CSV Data Set Config to iterate through a limited set of input data for each HTTP request that I do.
But I don't want to deal with the hassle of an external file (uploading it to my test runner, etc.) - I'd like to just embed the data into the jmx file itself.
I was hoping for something like a "test data" node, that would work similarly to a CSV data set (with Recycle on EOF especially) and I'd just copy/paste the data into the test plan instead of working with an external file.
I'm thinking I might be able to work around it with a JSR223 preprocessor - but is there a better built-in way?
Edit: As per comment: the data cannot be generated.
If you want to do this via JSR223 Test Elements and Groovy language correct syntax would be
vars.put("messageId", "wibble");
vars is a shorthand for JMeterVariables class instance, see the JavaDoc for available functions and properties.
Easier way would be going for User Defined Variables or User Parameters or even better Set Variables Action
You can create a text contains keys and values separated with tab, copy all text
Notice if you have property file you can replace = with tab
Add to JMeter GUI User Defined Variables and click Add from Clipboard
It'll load all your variables to JMeter without "do that by hand using JMeter's GUI"
.
This is my first go at a script based approach using a JSR223 preprocessor node:
// This is where the data is embedded. Up to a couple of hundred entries
// is probably fine, more than that will likely be a bad idea.
def messageIdList = ["graffle", "wibble", "wobble", "flobble", "gibble", ...]
def messageIndex = (vars.getIteration() -1) % (messageIdList.size() -1)
println "iteration ${vars.iteration}, size ${messageIdList.size()}, index: ${messageIndex}"
vars.put("messageId", messageIdList[messageIndex]);
messageIndex++
This appears to do what I want, even when run in a Thread Group with multiple threads.
I'm not sure exactly what the vars.getIteration() represents, and I'm not clear about the precise lifetime / scope of the variables. But it'll do for now.
Any better answers will cheerfully accepted, marked and upvoted.

Hiding parameter prevents other parameters from refreshing

I have the first parameter (client drop down) that passes an ID to the second parameter to be used in the 3rd and 4th parameter. During testing all the parameters are set to visible to verify what's being passed. As soon as I hide that second parameter the rest don't refresh. Again, visible, correct info flowing through, hidden, DOA. I have this set up exactly like this on at least a dozen other reports with no issue. I'm not real sure where this going wrong since I can see the correct value being passed while visible?
I was able to duplicate your issue. While I can't find a reference to support my supposition, I believe that hidden parameters (even those with a default value) are not evaluated until rendering, which would explain this behavior. Data sets that drive parameter values are run prior to the report being rendered.
As you're talking about having trouble with dependencies between the parameter values, I assume you're using queries to drive your available values for parameters three and four. If this is the case, then simply embed the logic that relates the value(s) of parameter 1 to the value(s) of parameter 3 into the query that drives parameter 3's available values. By cutting out Parameter 2 and incorporating it into the query logic for parameter 3, you will work around this issue.
If you're having trouble accomplishing this, reply in a comment and I'll see if I can help. As it stands, I don't know enough about what you're trying to do to help any more.

Variable input for JMeter Load testing?

I need to load test a service I've developed but I need the data that I post to the web service to have some variance.
I've set up Thread with an Http Request and I've the parameter I need to set but I can't see how I'd go about changing the contents of the HTTP parameter from request to request.
Ideally I'd like to feed in a list of data Items and have JMeter iterate through them.
Prepare kind of csv-file with list of your test-params and use it to parametrize your test-samplers, using at least the following:
CSV Data Set Config
Explained example here, simple example here.
Jmeter functions: __CSVRead, __StringFromFile.
Variables From CSV sampler from jmeter-plugins.
One way would be to prepare a CSV file with all the values that you will need. There are a multitude of different ways to use it afterwards. Alies Belik's answer listed most of them. The drawback of the CSV approach, however, is that you need to generate the list of values, and in some tests you can't simply reuse it without cleaning up/reinitializing the back-end database.
Another option are the functions for generating random values, usually paired with "User Defined Variables" controller.
__Random for generating numbers in a given range.
__RandomString for generating random strings of a given length and containing a set of characters.
This is a powerful mechanism, but I find it somewhat cumbersome and clunky.
For simple variables, like generating username/password/e-mail combinations, I prefer and find it easier to use the Random Variable config element. It's available since Jmeter 2.3.3. You add it to your thread group and specify a variable to store the random value for each thread. You can later reference this variable in your HTTP sampler, in the GET/POST parameters of the request, by specifying the Value of the parameter to be testuser-${rnd} for username, testpass-${rnd} for password. Each thread will get a different value of ${rnd} so there is a small chance (but there is still a chance) that you will get duplicate values (users).
Besides the functions mentioned in #zorlem answer, You can also use:
__UUID for generating a pseudo random type 4 Universally Unique IDentifier, if you need to generate random & unique strings.

Resources