JMeter: the same UUID between several samplers - random

How can a Jmeter thread group with several HTTP requests uses the same UUID value per thread?
I've tried to create 'user defined variables' element with ${__UUID} and java preprocessor with vars.put("uuid", UUID.randomUUID().toString()); (independently)
but every http request in the same thread using its own UUID.

Move the java PreProcessor as the child of the first HTTP Request(start session) and then call it as ${uuid} in all requests to get only one UUID value.
Another option is without code: Add User Parameters as PreProcessor of the first HTTP Request(start session) with Name as uuid2 and Value ${__UUID()} and you can use ${uuid2} to get only one UUID value.

JMeter Variables are local and individual for each thread (see ThreadLocal concept for details) so in order to create a random GUID structure you need:
Execute this function only once (optional, but nice to have). For instance it can be done using If Controller with condition set to something like:
${__groovy(ctx.getThreadNum() == 0 && vars.getIteration() == 1)}
Make function execution result available to all JMeter Threads. There are multiple ways of doing it, the easiest is converting it to JMeter Property via setProperty() function
Refer to the generated GUID using __P() function as ${__P(uuid,)} where required
This way you will be able to access the same GUID value from different threads or even thread groups.
Demo:

you cannot reuse the generated ${__UUID}, but you can always generate it via JavaScript function (it allows to reuse the result):
${__javaScript(function s4(){return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);} s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();, var_for_reuse)}

Related

Increment time value for 2 fields for every Thread and loop in jmeter

I have to Schedule a Meeting, In the particular post request body data I have to give from and to time. Every time the From & To time hours be different and time should not overlap.
For this requirement I tried the below code using JSR223 Sampler, But the issue I'm facing here is that only one time gets incremented and for every thread and loop .The value is same and it is not incrementing. Every Thread the time value should be incremented. Please let me know how I achieve it , as below code is returning same value for each Thread
def now = new Date()
log.info('Before: ' + now.format('HH:mm'))
use(groovy.time.TimeCategory) {
def nowPlus60Mins = now + 60.minutes
def nowPlus15Mins = nowPlus60Mins + 15.minutes
log.info('After: ' + nowPlus60Mins.format('HH:mm'))
log.info('End: ' + nowPlus15Mins.format('HH:mm'))
vars.put("AfterTime",nowPlus60Mins.format('HH:mm'));
vars.put("EndTime",nowPlus15Mins.format('HH:mm'));
if you want to affect all thread you must use JMeter properties, represented in script as props:
props.put("AfterTime",nowPlus60Mins.format('HH:mm'));
props.put("EndTime",nowPlus15Mins.format('HH:mm'));
To get the property value outside JSR223 Sampler using __P function as ${__P(AfterTime,)}
In JSR223 get property with props.get("EndTime")
If you run more than 1 iteration in 1 minute - it's absolutely expected that you will get the same generated offsets because given your SimpleDateFormat setting the value will update every minute.
Also you don't need any scripting, you can achieve the same using __timeShift() function directly in your request body:
plus 60 minutes: ${__timeShift(HH:mm,,PT60M,,)}
plus 15 minutes: ${__timeShift(HH:mm,,PT15M,,)}
More information: Creating Dates in JMeter Using the TimeShift Function

${__fifoPop(sync_tokensqo, gotTokenq_2)} empty the stack, what we supposed to do when we need same token for other request in Jmeter

I summarized my code as given below
User Login -100 times
Jsr233 post-processor put 100 tokens.
While loop
{counter
user login-100 times
Jsr 223 preprocessor -pop 100 tokens
}
On loop count 1, its pops all token, nothing left for count 2. How o handle this?
Just don't use the Inter-Thread Communication Plugin
If Login and While Loop are in the same Thread Group - use vars shorthand for the JMeterVariables class instance like:
vars.put('gotTokenq_2', 'token_value_here') // store the variable
If Login and While Loop are in different Thread Groups - use props shorthand instead:
to set the value:
props.put('token_' + ctx.getThreadNum(), 'token value here')
to read the value:
vars.put('gotTokenq_2', props.get('token_' + ctx.getThreadNum()))
In both cases you wil be able to access the token value as ${gotTokenq_2} where required
More information on these vars, props, and ctx shortcuts: Top 8 JMeter Java Classes You Should Be Using with Groovy

JMeter-For each implementation

I have an API which takes multiple IDs as a parameter:
http://pqa-volpqa.unknown.com:8080/items/batch/?Ids=00000017072571,00000017072588,00000017072595,00000019786230,00000019987460,00000019988238,00000019988283,00000019990170,00000020015206,00000020015213
Now these IDs are mention in a CSV file like below:
00000017072571
00000017072588
00000017072595
00000019786230
~~
~~
~~
00000020015213
How can I implement this?
If they are as a single string in CSV file you can just use __StringFromFile() or __FileToString() function to read them directly into request parameter
If they are each one a separate line it is still possible with __groovy() function like:
${__groovy(def line = new File('/path/to/your/file.csv').getText().replaceAll(System.getProperty('line.separator')\,'\,'),)}
See Apache JMeter Functions - An Introduction to learn more about JMeter Functions concept.
You can add a Loop controller with Loop count = number of IDs you want to add. Then add a Counter inside the loop controller with the below configurations:
Start: 1
Increment: 1
Reference Name: Counter
Check the boxes for Track counter independently for each user and Reset counter on each thread group iteration
Add a CSV Data Set Config after the counter with the below configurations:
Filename : The full name to your file // if the csv file is not in the same folder with your jmx file you should enter the full path to your CSV file.
Variable Names : id
Finally, add a BeanShell Sampler after the csv data set config with the below code in the code area:
String id = vars.get("id");
int Counter = Integer.parseInt(vars.get("Counter"));
if (Counter == 1){
vars.put("IDs", id);
}
else{
vars.put("IDs", vars.get("IDs") + "," + id);
}
All of the above should be before your API, now you can use your API as below:
http://pqa-volpqa.unknown.com:8080/items/batch/?Ids=${IDs}

jmeter unique id per thread for http request

My jmeter tests make a http request which contains a unique id.
http://myserver.com/{uniqueId}/
I want to set the base number (say 35000) and increment for each thread, for example my ids would be 35001, 35002, 35003 ...
http://myserver.com/{base + count}
I see functions for __threadnum and __counter but how would I:
set it in a variable so that I can substitute it in my url
add this count to my base value
I would simply go with Beanshell pre processor.
int threadNo = ctx.getThreadNum()+1; //to get the thread number in beanshell
int base = 35000;
int uniqueId = threadNo + base;
vars.put("uniqueId", Integer.toString(uniqueId));
Now your HTTP requests as given below should have the value substituted.
http://myserver.com/${uniqueId}/
To set a variable per thread you can use User Defined Variables.
To sum, you can use functions:
__intSum
__longSum
Or use a JSR223 PRE Processor or JSR223 POST Processor + Groovy and do it in Groovy.

How to deal with randomly generated post request parameters in Jmeter Scripts?

I am writing jmeter scripts for my web-based application. I am using firefox-firebug to watch POST request parameters. I could successfully write login page scripts because it had only "username" and "password" parameters.
But, after logging into the web application, I realized that, there are randomly generated required parameters which are sent along with the post request.
So, I am trying to find out the way to deal with these parameters.
Please let me know, if you have dealt with this situation.
Example: These are my post request parameters:
externalId=971&submit.go=Go&submit.go=&013f57c77c2a%3A6eed%3A1b320be7=105f230e-9f86-40f8-9473-215975812128
Where **013f57c77c2a%3A6eed%3A1b320be7** parameter and it's value are generated differently each time.
I don't know how to define this parameter.
I found an answer. You can use List Extractor(Regular Expression Extractor).
You can define any pattern as per your criteria.
for example regex patter is : input type="hidden" name="([^"]+?)" value="([^"]+?)"
Step2) Use Beanshell pre processor with this script.
log.info("=====================");
count = Integer.valueOf (vars.getObject("hiddenList_matchNr") ) ;
log.info("Number of hidden fields in previous sampler: " + count);
for (i=1; i <= count; i++) {
paramName = vars.getObject("hiddenList_"+ i + "_g1");
paramVal = vars.getObject("hiddenList_"+ i + "_g2");
log.info("Adding request parameter: " + paramName + " = " + paramVal);
sampler.addArgument(paramName, paramVal);
}
log.info("=====================");

Resources