JMeter Switch Controller - jmeter

How I can user variables in switch controller. I need this kind of logic:
ThreadGroup contains: counter (start with 1000 and ends with 1020, referense name = record_id), httpRequest(use record_id as a parameter), switch controller (switch value = ${record_id}) and inside switch controller I have size assertion named 1000. But this size assertion is't working. How I can make switch controller working with diffrent cases like 1000, 1001 etc (record_id(counter)).

The switch controller must contain samplers or controllers named 1000, 1001, etc. An assertion is not switchable so no point naming it 1000, you need to name the sample that the assertion applies to.

Related

Loop controller inside While controller in JMeter

I have a while controller that stops after running for 5 seconds.
This while controller works fine when inside it, there is one sampler or one HTTP request.
Now I want to have a loop controller inside this while controller. But now, the while controller doesn't stop after 5 seconds, and the script runs for the number specified in loop controller.
Is there any way my loop controller stop working when the while controller trigers in 5 seconds?
Here's the schematic of my test plan. I want that "search" request stops after 5 seconds (the condition inside while controller), no matter the specified number in loop controller.
PS. The code inside JSR223 Sampler1 calculates the maximum time:
max = ${__timeShift(,,PT5S,,)};
vars.putObject("max", max);
And this is the logic inside While Controller:
${__groovy( now = ${__time()}; max = vars.get("max") as long; now <= max,)}
Why would you need the Loop Controller if While Controller generates loops itself?
Don't inline JMeter Functions or Variables into JSR223 Test Elements or __groovy() function otherwise you might get unexpected behaviour.
If you want to limit While Controller's number of loops to some specific maximum value just include it into your condition clause.
In the JSR223 Sampler:
max = System.currentTimeMillis() + 5000L
In the While Controller:
${__groovy( now = System.currentTimeMillis(); max = vars.getObject("max"); now <= max && (vars.get('__jm__While Controller__idx') as int) < 10,)}
More information: Using the While Controller in JMeter

Jmeter script for different orderlines

I have question on jmeter
we have different types of order lines like (3 ,5, 15) orders.
i need to create a script for different order lines.
please find the my approach in loadrunner, how can we do the same in jmeter
In loadrunner--> I have created different action with switch case
Action name :
OrderLine(trans, int orderline)
{
switch case
case 3:
requests for 3 order lines
case 5:
requests for 5 order lines
case 15:
requests for 15 order lines; break;
}
in another action, i will call OrderLine transaction
Action_03Lines()
{
Orderlines(OrderLines_03,3);
Orderlines(OrderLines_03,3);
Orderlines(OrderLines_03,3);
}
Action_05Lines()
{
Orderlines(OrderLines_05,5);
Orderlines(OrderLines_05,5);
Orderlines(OrderLines_05,5);
Orderlines(OrderLines_05,5);
Orderlines(OrderLines_05,5);
}
and i will call the actions on %ile bases.. like Action_03lines will run for 90% and Action_05 & Action_15 will execute with 5% each.
Use Switch_Controller as parent of each request with relevant number in switch value
Switch Value
The number (or name) of the subordinate element to be invoked.
In JMeter you have:
Test Fragment - to hold the reusable code
Module Controller - to refer the reusable code blocks where required
Loop Controller or While Controller - to iterate the block of code for desired number of times or while certain condition is (not) met

how to Bypass the Sampler based on previous response value in jmeter?

I have caught up in a situation, where in i need to verify the response of the Previous Sampler for one of the value and if the Value for that is [], then i need to trigger the below request or else then switch to another Sampler.
Flow:
Check Response of Sampler for One of the attribute
IF(attribute value==[])
Execute the Sampler under IF Conditions.
ELSE
New Sampler
Sample Response:
{"id":8,"merchant_id":"39","title":"Shirts-XtraLarge","subtitle":null,"price":110,"description":null,"images":"image_thumbs":[[]],"options":[],"options_available":[],"custom_options":[]}
I need to check if the attribute custom_options is empty or not! If Empty do some actions and if not empty do some other action !
Need if condition to simulate this!
Help is useful!
A nice to have feature in JMeter would be Else statement, but until then you will have to use 2 If Controllers
If Controller allows the user to control whether the test elements below it (its children) are run or not.
Assuming you hold your attribute value using regex/json/css/other post processor extractor add two condition, first is positive and under it the Sampler:
${__groovy("${attributeValue}" == "[]")}
Second is negative and under it add the New Sampler
${__groovy("${attributeValue}" != "[]")}
__groovy is encourage to use over default Javascript
Checking this and using __jexl3 or __groovy function in Condition is advised for performances
Go for Switch Controller
Add JSR223 PostProcessor as a child of the request which returns your JSON
Put the following code into "Script" area:
def size = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..custom_options')[0].size()
if (size == 0) {
vars.put('size', 'empty')
} else {
vars.put('size', 'notempty')
}
Add Switch Controller to your Test Plan and use ${size} as "Switch Value"
Add Simple Controller as a child of the Switch Controller and give empty name to it. Put request(s) related to empty "custom_options" under that empty Simple Controller
Add another Simple Controller as a child of the Switch Controller and give notempty name to it. Put request(s) related to not empty "custom_options" under that notempty Simple Controller.
More information: Selection Statements in JMeter Made Easy

JMeter Nested Variable Reference with JDBC Resultset Variable and Counter Variable

I need to query a MYSQL Database for a list of siteIDs and siteURLs. I've specified these names in the JDBC Request's Variable Name field.
Then I created a ForEach Logic Controller to cycle through the siteURLs ${siteURL_1} till the last record from the result as such:
Input Variable Prefix: siteURL
Start Index: 0
End Index: 40
Output Variable Name: newSiteURL
Then I use this in the HTTP Request's Path field as:
${newSiteURL}
This works fine and the HTTP requests are going through.
Now, I want to name the HTTP Requests properly so that they are indexed better.
For that, I decided to use the siteID field from the result set.
To do that, I created a counter variable as such:
Start: 1
Increment: 1
Maximum: 40
Reference Name: siteIndex
Now, to get the siteID from the result show in the corresponding HTTP Request, I edited the Name field of the HTTP Request to this:
${siteID_"({siteIndex})"}
But my HTTP Requests in the View Results Tree still end up showing as:
${siteID_"({siteIndex})"}
${siteID_"({siteIndex})"}
${siteID_"({siteIndex})"}
${siteID_"({siteIndex})"}
${siteID_"({siteIndex})"}
...
And not the actual siteID for the corresponding siteURL in the HTTP Request like:
21231
12315
21654
64574
76876
...
You need to change this bit:
${siteID_"({siteIndex})"}
to
${__V(siteID_${siteIndex})}
Explanation:
As per __V function documentation
For example, if one has variables A1,A2 and N=1:
${A1} - works OK
${A${N}} - does not work (nested variable reference)
${__V(A${N})} - works OK. A${N} becomes A1, and the __V function returns the value of A1
See Using JMeter Functions post series for more examples on how to get things done with useful JMeter Functions.

Same random number and string for each thread

I need to generate the same JSON data twice for each thread for an HTTP request. I am having problem setting that up in JMeter.
My structure is:
Test Plan
- HTTP Header Manager
- Thread Group 1 users, 5 loop
- Random Variable,
- HTTP Request
I tried the combination of Per Thread User set to true and using seed for random function, but I can't achieve what I want. It keep on generating new number/string per loop.
Basically for each user, I want the exact same JSON request data.
I see three decisions for your case:
Use User Defined Variables before HTTP Request. Specify variables here. In this case it will be better to use __Random() function instead Random Variable.
Test Plan
- HTTP Header Manager
- Thread Group 1 users, 5 loop
- User Defined Variables, (Name:varName; Value:${__Random(1,100)})
- HTTP Request
Use Loop Controller (loop count=5) before HTTP Request (instead loop=5 in Thread Group)
I can suggest another solution which seems to me verbose and awkward but no other idea.
You create file using BSF (or BeanShell) PreProcessor and write value of random variable into file. Then read file before every request. In the next example I used Groovy and BSF PreProcessor.
import java.util.Random
def out= new File('File1.txt') // create file if it is not exists
if(!out.exists())
{
out.createNewFile()
Random rand = new Random()
int max = 10
def a = rand.nextInt(max+1)
out << a // write text to file
}
//then read value of generated variable
String fileContents = new File('File1.txt').text
//then put your variable into User defined Variable that I named HELLO
vars.putObject("HELLO",fileContents)
And in needed request use ${HELLO}

Resources