In Jmeter vars vs props for while controller - jmeter

Before test, I added x variables to TestPlans,
After I run test as 10 thread 10 seconds ramp up
In while controller for condition that
${__javaScript("${x}" != "",)}
In beanshell:
vars.put("x","");
Normally, each thread occured 1 second, First thread changed x variable as "", therefore another threads don't meet if condition. why do threads get looped though ?
Note: Other threads did not start testing when using property instead of variable.

As per JMeter documentation:
Properties are not the same as variables. Variables are local to a thread; properties are common to all threads
so when you do vars.put("x",""); you change x variable value only for the current thread, other threads have their own values of the x variable.
If you want to amend the value so it would be visible for all threads - you will have to:
Change vars.put("x",""); to props.put("x","");
In the While Controller ${__javaScript("${__P(x,)}" != "",)}
Also: since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so it worth considering migrating (the same applies to __javaScript() function in the While Controller), more information: Beanshell vs. JSR223 vs. Java For JMeter: Complete Showdown

Related

JSR223 Preprocessor generating the same value when the thread runs in loop

Context: Want to generate unique and different value when the thread runs in loop.
Currently it is generating the same value.
Script Inside JSR223 Preprocessor,
String subscribeSchemaNamePreProcesser="agent-Perf-${__Random(1,99999,)}"
log.info("Schema Name --------------->"+subscribeSchemaNamePreProcesser)
vars.put("subscribeSchemaNamePreProcesser", subscribeSchemaNamePreProcesser)
When run with number of threads more than 1(i.e. 2 thread 1 loop), it is generating different value
When run with number of loops more than 1 (i.e. 1 thread 2 loop), it is generating the same value (want to fix it)
anything I missed
Don't inline JMeter Functions or Variables into Groovy scripts, they will be cached and only first occurrence will be used for subsequent calls.
You need to change this line:
String subscribeSchemaNamePreProcesser="agent-Perf-${__Random(1,99999,)}"
to this one:
String subscribeSchemaNamePreProcesser = "agent-Perf-" + org.apache.commons.lang3.RandomUtils.nextInt(1, 99999)
More information:
JSR223 Sampler
Apache Groovy - Why and How You Should Use It

How to save 10 different id from Jmeter response and use it in next 10 requests

I am currently running 10 different threads like this:
each of the thread response will provide a different id in the response and i want to save them and use it as a request in the next test case(10 threads) so there will be 10 ids and 10 threads and each thread will have a unique id as a request. This is what I am doing:
Here is the first request
This is how I am extracting the values
This is how i am using the final request however i am not able to get the desired results
UPDATE:
I tried Dmitri Answer but still no luck i am extracting the id by using this
I used __threadNum() function as the prefix or postfix for the property name like:
${__setProperty(loginassistant_${__threadNum},${loginassistant_},)}
and read it similarly:
${__P(loginassistant_${__threadNum},)}
but it is not working and it is setting the value as a static string(see screenshot below):
This is how and where i am defining my loginassistant using the simple controller:
You're using properties which are global, if you have more than one thread the next thread will overwrite the property defined by the previous thread so it could be mismatch.
If you don't need to pass values across thread groups - go for JMeter Variables instead, variables are local to the thread (virtual user) and the properties are global for the whole JMeter/JVM instance
If you do need to pass values across thread groups - either use __threadNum() function as the prefix or postfix for the property name like:
${__setProperty(loginassistant_${__threadNum},${loginassistant},)}
and read it similarly:
${__P(loginassistant_${__threadNum},)}
or go for Inter-Thread Communication Plugin

JMeter structure warmup

My project structure in JMeter is now:
But there are some limitations:
A lot of duplicate transaction/module controllers
The only once controller is not working with the UltimateThreadGroup
Its not possible to use the setUp thread, because the cache is not shared between threads
The time from the warmup is different, i cant use the offset in my Synthesis Report
What is a better structure to skip the warmup (first threadrun) from my results?
If you want to just remove 1st execution of each sampler for each thread (virtual user) you can play the following trick:
Add JSR223 PostProcessor as a child of the request you want to ignore (or according to JMeter Scoping Rules if you want to remove the first execution of other samplers)
Put the following code into "Script" area:
if (vars.getIteration() == 1) {
prev.setIgnore()
}
where:
vars - is a shorthand for JMeterVariables class and getIteration() function returns the current Thread Group loop number
prev - is a shorthand for the SampleResult class and setIgnore() function tells JMeter to discard the result

Jmeter property get/put issue

I have the following flow,
Thread Group 1
-JSR223 Sampler - props.put("x",10);
Thread Group 2
Number of threads(users) = ${__P("x")}
I am setting a property value which is not defined in any property file as 10. I am trying to use that value in the next thread group. But instead of ten threads, I get only 1 thread to start.
Documentation says ${__P()} would give 1 if the property is undefined. How can I set the THread Group 2's thread count to x value?
You need to pass that value from command line, when you run the test appended by -Jx.
So when you run your test, Pass value of X from command line
-JPropertyName=ProperyValue
This will pick up value as 10.
Setting property should be
props.put("x","10");
i.e. it wants value in String format, and function should be put, not set.
Thread Groups are being initialised during JMeter startup so you won't be able to set the number of threads in the Thread Group this way. You have 2 options:
Set the property value in user.properties file (lives in the "bin" folder of your JMeter installation) like
x=10
Pass the property value -J or -D command-line arguments like
jmeter -Jx=10 -n -t ....
jmeter -Dx=10 -n -t ...
The default value returned by the __P() function will never be 1 due to a bug in JMeter (you can report it via JMeter Issue Tracker if you want), its default value will be an empty string. If you want the __P() function to return 1 as default value you should be using it like ${__P(x,1)}
See Apache JMeter Properties Customization Guide guide for more information on working with JMeter Properties, ways of setting and overriding them, etc.
Although you cannot change the number of threads on the fly, you can control how often the samplers are executed via Constant Throughput Timer. This way you can use __P() function to set the desired throughput rate (in requests per minute) and you can change the associated property value on the fly even outside of the JMeter script.

Running a Beanshell pre-processor once in a test plan

I have a Bean shell preprocessor which ends up setting up some global variables like host name and the path according to the value that the user passes.
The variables that the bean shell sets would be used by all the thread group.
Currently i've placed my BS pre-processor outside a thread and it runs perfectly..
The issue is that it runs for every thread which isn't performance friendly.
I just want it to run once at the start of the Test plan to improve performance.
I tried to put it into a setUp thread but it doesn't work.
Is there something other that the BS-preprocessor that i can use which would be performance effective(which runs only once for the entire plan rather than for every thread).
You can put it under If Controller and use the following condition:
${__BeanShell(vars.getIteration() == 1)} && ${__threadNum} == 1
You can use setUp Thread Group which is designed for executing pre-test actions but in that case change all occurrences of vars.put() to props.put() as JMeter variables scope is limited to current thread group only and Properties are global for the whole JVM instance. See How to Use Variables in Different Thread Groups guide for additional information
JMeter SetUp thread group and TearDown thread group are meant for exactly this. These threads run at the start and the end of test plan. So, just add Setup thread group and add beanshells into. See links for more information:
http://jmeter.apache.org/usermanual/component_reference.html#setUp_Thread_Group
In the other hand, vars are limited for each thread groups. Use props to put a value which is shared with all thread groups.
This worked for me... Just configure a User Defined Variable called firstTimeIndicator and set the value to 1
String firstTimeIndicator = "";
firstTimeIndicator = vars.get("firstTimeIndicator");
if (firstTimeIndicator.equals("1")) {
log.info("The code inside the if statement executes once per test run...");
firstTimeIndicator = "0";
vars.put("firstTimeIndicator",firstTimeIndicator);
}
log.info("The code after the if statement executes once per thread");

Resources