How to read JMeter Test Cmd Line Parameters from JSR223/Beanshell - jmeter

I have wrote a JMeter test and I want to run it in Command Line with some parameters, let's say ThreadNumber.
How do I read it in JSR223/BeanShell?

Send property in command line using -J which adds new property
-JthreadNum=100
Inside Thread Group use the value using __P function in Number of Users(threads) field
${__P(threadNum)}
simplified property function which is intended for use with properties defined on the command line.
Use props to get properry in JSR223/BeanShell
props.get("threadNum")
Note you may set ramp up same as thread number property
Start with Ramp-up = number of threads and adjust up or down as needed.

Related

JMeter update user defined variable at run time

I am trying to update the user defined variable set at a test plan level from a thread group. This is my scenario:
Test plan> user defined variable (variable name/value: fBurst=0)
Test plan> Threadgroup1 > Once only controller> JSR223 test plan (inside the test plan I have the following code)
log.info("fBurst user defined value : " + ${fBurst})
vars.put("fBurst", Integer.toString(111))
log.info("fBurst user defined value vars.put' : " + ${fBurst})
props.put("fBurst", 222)
log.info("fBurst user defined value props.put' : " + ${fBurst})
What I am trying to do here is to update the fBurst user defined value from inside the once only controller sampler and so far not been able to do it using the vars.put or props.put. Is there another way to do this?
Take a look at JSR223 Sampler documentation:
The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:
Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them.
Or Use Script Text and check Cache compiled script if available property.
When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.
So replace ${fBurst} with vars.get('fBurst") or props.get('fBurst') correspondingly and it should start working as expected
More information: Top 8 JMeter Java Classes You Should Be Using with Groovy

How to pass variable value in selenium-javascript

In script I used org.apache.commons.io.FileUtils.writeStringToFile(new java.io.File('result33.csv'), 'User,Items per page,Pagination / Activity,Time to load(ms)' + newline, 'UTF-8', true) now I want to pass user value and time to load value dynamically (Actually I calculated those value in my script) so how to pass this value in above function?
and can you please provide how to pass variable(which used in script) and its value in Flexible File Writer
You have WDS.vars shorthand which provides read/write access to JMeterVariables class instance so you can do:
var foo = WDS.vars.get('foo')
to read the foo variable value from JMeter Variables
or
WDS.vars.put('foo', 'bar')
to create a JMeter Variable called foo with the value of bar
In order to be able to use the variables in the Flexible File Writer you need to declare them via sample_variables property, i.e. add the next line to user.properties file (lives in "bin" folder of your JMeter Installation)
sample_variables=foo
and next time you start JMeter you will be able to refer the variable as variable#0
if you have 2 variables like:
sample_variables=foo,bar
the first one will be variable#0, the second one will be variable#1, etc.
More information: Using the JMeter Flexible File Writer Plugin

Pass DBID value twice in single request

I need to pass the DBID value two times in the single request, I have tried placing _counter function twice, but it is updating the counter value every time when it is hitting
<v13:taskIdentifier>${__evalVar(taskcounter)}</v13:taskIdentifier>
<v13:databaseId>${__evalVar(dbid_${__counter(TRUE,)})}</v13:databaseId>
</v12:uniqueTaskIdentifier>
<v14:executionTime>
<v15:value>${__time(YYYY-MM-dd)}T13:00:00</v15:value>
</v14:executionTime>
<v14:resourceLocation>
<v16:identifierLocation>
<v17:type>Unique Task Identifier</v17:type>
<v17:identifier>${__evalVar(dbid_${__counter(TRUE,)})}${__evalVar(taskcounter)}</v17:identifier>
Is there any other option to get the same counter value second time ?
I suggest you save the value in User Parameters which will be a child of the request
allows you to specify a series of values for any User Variable. For each thread, the variable will be assigned one of the values from the series in sequence. If there are more threads than values, the values get re-used.
Define parameter value as ${__counter(TRUE,)} and for example name c
Then use in XML ${c} instead
<v13:taskIdentifier>${__evalVar(taskcounter)}</v13:taskIdentifier>
<v13:databaseId>${__evalVar(dbid_${c})}</v13:databaseId>
</v12:uniqueTaskIdentifier>
<v14:executionTime>
<v15:value>${__time(YYYY-MM-dd)}T13:00:00</v15:value>
</v14:executionTime>
<v14:resourceLocation>
<v16:identifierLocation>
<v17:type>Unique Task Identifier</v17:type>
<v17:identifier>${__evalVar(dbid_${c})}${__evalVar(taskcounter)}</v17:identifier>
Your __counter() function generates an incremented value each time it's being called
The options are in:
Switch to Counter configuration element
Amend your current setup as follows:
1st occurrence: ${__evalVar(dbid_${__counter(TRUE,dbid)})}
this will store the generated counter number into ${dbid} JMeter Variable
2nd, 3rd, etc. occurrences: ${__evalVar(dbid_${dbid})}
More information: How to Use a Counter in a JMeter Test

How to parametrize number of threads in JMeter 5.2.1

I have a JMeter 5.2.1 project where in the SetUp thread I generate properties with names like ThreadGroupName1-NumberOfThreads, ThreadGroupName2-NumberOfThreads, etc. with values representing integers.
Now, I wish to access these properties in thread groups named ThreadGroupName1, ThreadGroupName2, etc. to parametrize the number of threads. I tried something like ${__jexl3(props.get(threadName + "-NumberOfThreads"))} but it fails as threadName evaluates to standardjmeterengine.
Also, I tried to use ctx but ctx.getThread() and ctx.getThreadGroup() but they evaluate to null.
So far what 'works' for me is ${__jexl3(props.get("ThreadGroupName1-NumberOfThreads"))} but I want it be parametrized by the name of the thread group.
Is it possible to do this?
Is this threadName returning standardjmeterengine a bug?
Update: In fact, the easiest 'solution' that provides the parametrized number of threads there is ${__P(ThreadGroupName1-NumberOfThreads)} and what I want is to generate this key ThreadGroupName1-NumberOfThreads to be something like ${MyCurrentGroupName}-NumberOfThreads, effectively providing a way to have an abstract method like
int GetNumberOfThreads(string threadGroupName)
{
return properties.get(threadGroupName + "-NumberOfThreads";
}
Similarly, I wish to use this patter in Constant Throughput Timer as well with another prefix like -Rpm.
I don't think you can use any JMeter Function in the "Number of Threads" field of the Thread Group so this is not something you can do via UI. If you believe this is something everyone needs you can consider raising an enhancement request
As a workaround you can
Set the number of threads to 1
Add If Controller to the Thread Group and use the following __groovy() function as the condition:
${__groovy(ctx.getThreadNum() == 0 && vars.getIteration() == 1,)}
Add JSR223 Sampler as a child of the If Controller and put the following code into "Script" area:
SampleResult.setIgnore()
2.upto(props.get(ctx.getThreadGroup().getName() + '-NumberOfThreads') as int, { ctx.getThreadGroup().addNewThread(0, ctx.getEngine()) })
This way each thread Group will normally start with 1 thread, however this thread will read the property you defined earlier and add as many threads as needed.
You can use JMeterContext's getThreadNum to get thread number (increment, because it starts with 0)
${__jexl3(props.get("ThreadGroupName"))}${__jexl3((ctx.getThreadNum()+1) + "-NumberOfThreads" )}

How to create simple counter using Beanshell?

I'm trying to create a simple counter that will print the iteration number to the log.
the problem is that I didn't find a way to initialize the int value of i to 0.
if I'll do it inside the Beanshell script it will keep initializing, I need it to run only once at the beginning of the test.
My code:
int i=0;
log.info(string.valueOf(i));
i=i+1;
Add Once Only Controller, under it JSR223 Sampler with the initialization
vars.putObject("i", 0);
Then you can increment it after it (not under the Controller) with other JSR223 Sampler:
myI = vars.getObject("i")
log.info(String.valueOf(myI));
vars.putObject("i", ((Integer)myI+1));
It is recommended to avoid scripting where possible, and if you cannot live without scripting you should be using the most performing option which is JSR223 Test Elements and Groovy language.
Particularly your case can be implemented without any scripting, you can use the following JMeter Functions:
__log() - which prints an arbitrary message to jmeter.log file
__iterationNum() - which returns the number of current iteration
So if you use the statement like: ${__log(Current iteration is: ${__iterationNum},,,)} JMeter will return it where the function is called and additionally print the corresponding message to the log file.
Demo:
You can install __iterationNum() function as a part of Custom JMeter Functions bundle using JMeter Plugins Manager

Resources