Pass Variable inside the another variable in Jmeter - performance

I Have a scenario like need to pass a random variable example ${__Random(1,25,)} in inside one more variable like
Globel variable:
Test = TestResults
${__Random(1,25,)}
Sample Request Data:
${Test(randome variable)}, ${Test(randome variable)},${Test(randome variable)},-------- ${Test(randome variable)}
Sample expected response:
TestResults1,TestResults10,TestResults5,----------- TestResults20
How should I pass this scenario?

Use the V function
${__V(TestResults${__Random(1,25,)})}
V (variable) function returns the result of evaluating a variable name expression. This can be used to evaluate nested variable references
You can replace TestResults with ${yourVarName} if you want to dynamically use additional variable

Related

How can I use __chooseRandom with a URL parameter?

Context: I want to use the __chooseRandom function to embed a randomly chosen value in a URI path.
Problem: I can't figure out how to use the __chooseRandom function properly. With the below example, the URL I get is 'https://google.com/search?q=&iter=${__chooseRandom(1111,2222,iter)}' instead of https://google.com/search?q=1111 or https://google.com/search?q=2222
Question: How should I modify the JMeter script so the request path gets a value from chooseRandom instead of directly embedding the chooseRandom string?
Not Working Example:
If I got your problem properly you are trying to generate a random value at the end of that URL from a set of variables (numbers).
You can try to follow these steps:
add User Defined Variables -> Right click on Test Plan -> Add -> select Config Element -> User Defined Variables
Add following variables (Change values and variables names depending on your needs)
Name: Value:
num1 111
num2 222
num3 333
num4 444
Then all you have to do now is to use this ${__V(num${__Random(1,4,)})}
This function will actually get the value of each variable name that starts with "num" and ends with a random value between 1 and 4 (value of num1 = 111 , the value of num3 = 333 ...)
PS: I tried using this as a JSON parameter
Body Data:
{
"iter" : ${__V(num${__Random(1,6,)})}
}
It looks like that URL path is being evaluated before request parameter.
You need to call the __chooseRandom() function either somewhere before the HTTP Request sampler or get rid of this ${iter} variable and use __chooseRandom() function directly in the request path.
/search?q=${__chooseRandom(1111,2222,iter)}
Demo:
More information:
__chooseRandom() function documentation
How to Use the Custom JMeter Functions Plugin

How to read values from a database like a csv file in jmeter

I would like to read values from a select query and use the values sequentially in a API call.
Since the database values get stored as var_1, var_2 ... var_N
So how do i increment the number for the variable?
I used a counter and pre processor to increment the number in variable
vars.put ("email", "email"+"_"+vars.get("counter"))
But the final variable is not getting replaced by the value from the select query
eg select query result from a debug sampler
email_1=hata.pd.h13u#yopmail.com
email_2=hata.pd.h13u#yopmail.com
email_3=hataiot.test13#mailinator.com
email_4=hataiot.test12#mailinator.com
--combining the variable and counter:
vars.put ("email", "email"+"_"+vars.get("counter"))
--using the variable in the API post body
{
"username":"${email}",
"password":"test1234"
}
Actual result:
POST data:
{
"username":"email_1",
"password":"test1234"
}
Expected result:
{
"username":"hata.pd.h13u#yopmail.com",
"password":"test1234"
}
TIA
${__V(email_${counter})}
Try this one, see also the documentation: http://jmeter.apache.org/usermanual/functions.html#what_can_do
Note that variables cannot currently be nested; i.e. ${Var${N}} does not work. The __V (variable) function can be used to do this: ${__V(Var${N})}. You can also use ${__BeanShell(vars.get("Var${N}")}.
Have you considered using ForEach Controller? It's super-handy for iterating the variables from extractors or JDBC test elements.
If you still want to continue with the current approach you need to change this line:
vars.put ("email", "email"+"_"+vars.get("counter"))
to this one:
vars.put ("email", vars.get("email"+"_"+vars.get("counter")))
Because you're putting into email variable stuff like email_1, email_2, etc. instead of actual ${email_1} variable value.
References:
JMeter Functions and Variables
How to Retrieve Database Data for API Testing with JMeter

__V for property variable in jmeter

I have JMX structure such as,
ThreadGroup-1
Set propertyVariable-Content_0,Content_1,Content_3 etc
ThreadGroup-2
LoopController
Counter-counterNum
HTTPRequest--use PropertyValue as ${__V(__P(Content)_${CounterNum})}
above variable is not fetching value.
Got stuck on how to use __V for this case as it has property variable.
can someone explain me how to use __V when we have property variable.
You don't need __V() function here, you can access property value using __P() function directly like:
${__P(Content_${CounterNum},)}
Demo:
In case of malfunction double check that you're really setting the properties values using Debug Sampler and View Results Tree listener combination
you need to use this
${__V(MainVriableName_${CounterNo})}
1) Add Loop Controller
2) Under Loop Counter -> Add Counter (Starting value= 1, Increment = 1, Maximun value = ${MainVriableName_matchNr} and Exported Variable Name = CounterNo
3) After in the below Request(s) use ${__V(MainVriableName_${CounterNo})}
this works fine.

Go capturing template variables and names

I am experimenting with Go's templates.
I am curious: is it possible to do the following: {{$myVariable := randomStringFunc | saveFunc}}
Where:
$myVariable - randomly chosen variable name
randomStringFunc - function that generates random strings
TRICKY: saveFunc - a function that saves the variable name and its value
I have looked into capture-or-assign-golang-template-output-to-variable but I am not sure how and if it can help me achieve my goal.
EDIT
In the end I would like to have a mapping between the variable name that is defined in the template and the value that is assigned to it:
var variableMapping map[string]string
After template execution the content of variableMapping should be something like:
{
"$myVariable:"randomString1",
"$anotherVariable":"5",
"$thirdVariableInMyTemplate":"false"
}

Passing two variables in a ForEach controller in jmeter

I am running a ForEach controller in which I want the controller to run the service underneath it for changes in sets of latitudes and longitudes. example-
Input variable prefix: latitude
Output variable name: Latitude
I want to run the controller for changes in both "latitude" and "longitude". I tried doing this-
Input variable prefix:latitude, longitude
but it does not work. Is there any other way to pass two variables in ForEach controller?
Unfortunately you cannot do it using ForEach Controller, but you can work it around using __V() and __counter() function combination.
For example you have 4 JMeter Variables:
latitude_1=40.7128° N
longitude_1=74.0059° W
latitude_2=32.0853° N
longitude_2=34.7818° E
And you want to iterate them both using ForEach Controller. In that case the relevant configuration would be:
Input variable prefix: latitude
Output variable name: anything meaningful, i.e. current_latitude
You can refer matching longitude value using the following expression:
${__V(longitude_${__counter(,)})}
Demo:
See Here’s What to Do to Combine Multiple JMeter Variables for detailed explanation of where did the above expression come from.
I encountered same problem and google a lot to find the best possible solution but
did not get success. I am sharing my work-around (and I am not saying it's a great solution ) -
Mention your Input variable name (ex - inputVar) as well as Output variable name (outputVar) on ForEach Controller
(lets say http://website.com/folder_A/folder_B/123_latitude/456_longitude) and you like to take care these two : latitue and longtitude
Use regular expression extractor :
Name of created variable : inputVar
Expression : http://website.com/folder_A/folder_B/(.*)
Template : $1$
match No : -1 // (yes -1 , this will give you complete group)
Handle with regular expression (just search it out )
so for above example : that would be -
http://website.com/folder_A/folder_B/${outputVar}
http://website.com/folder_A/folder_B/${outputVar} URL will be able to print / give you latitue and longtitude in your response code

Resources