Can I use a JMeter Duration Assertion with a variable? - jmeter

I'm trying to assert the response time of several pages but introducing a threshold, let's say:
http://xxxx/login typical takes: 500ms
http://xxxx/user typical takes: 1000ms
...
So I want to write assertions checking the expected value + threshold, i.e. something like:
http://xxxx/login: Assert < 500 * (1 + ${threshold})
http://xxxx/user: Assert < 1000 * (1 + ${threshold})
...
For sure I can make the calculation and set the value, but would be nice if I can use a variable.
Would this be possible ?

Sure you can, you can perform whatever calculation you want using i.e. __groovy() function which allows executing arbitrary Groovy code.
In your case the code would be:
500 * (1 + (vars.get('threshold') as int))
and the __groovy() function syntax:
${__groovy(500 * (1 + (vars.get('threshold') as int)),)}
You can put the function directly to your Duration Assertion:
and the function will be evaluated in the runtime and applied to the Duration Assertion:
In the above example vars stands for JMeterVariables class instance, it provides read/write access to all JMeter Variables in the current virtual user context, see JavaDoc for available functions and Top 8 JMeter Java Classes You Should Be Using with Groovy for more information on this and other JMeter API shorthands available for JSR223 Test Elements and __groovy() function

Related

How to pass ${__threadNum} value in selenium javascript?

I used Webdriver sampler and write selenium -Javascript. Now in my script for achieving some result we need total number of users so I want to know how add ${__threadNum} value in my script?
or is there any way when multiple users run and some operations execute for only one user?
Either put the ${__threadNum} function to the "Parameters" section and refer it in the script as WDS.args[0] where required
var threadNum = WDS.args[0]
Or use WDS.ctx shorthand for JMeterContext class, there you will have access to getThreadNum() function. It will return zero-based number of thread so to get the same value you will need to increment it by 1
var threadNum = (WDS.ctx.getThreadNum() + 1)
Demo:
More information: The WebDriver Sampler: Your Top 10 Questions Answered article for more information if needed

Jmeter how to find the difference in two time which is stored in string

I have below values
String CurrentTime1=${__groovy(${__groovy(Date.parse('yyyy-MM-dd hh:mm:ss.SSS','${Modified_date_1}').getTime(),)},)}
log.info("Current time1 ----> "+CurrentTime1);
String beforetime=vars.get("beforetime");
log.info(" after time -->"+beforetime);
Result:
Current time1 ----> 1611495406402
after time -->1611495402100
As there are stored in string, i am not able to get the difference between these values. Can you please help.
I need to get the difference between current time and before time which are dynamic values.
If you want to do this in Groovy - you need to convert the string to Long like:
def before = vars.get('beforetime') as long
def current = vars.get('CurrentTime1') as long
def delta = current - before
def deltaAsString = delta as String
If you prefer using JMeter Functions - you can go for __longSum()
${__longSum(${CurrentTime1},-${beforetime},)}
More information on this and other JMeter Functions: How to Use JMeter Functions - Part III
But don't mix both approaches like inlining JMeter functions in Groovy code, as per JSR223 Sampler documentation:
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.

Jmeter $__Random() with user defined variables

I have an issue I have been struggeling with for some time.
In my script I'm calculating a max and min value. I save it using:
int minWaitPregancy_acutall=0.2*maxWaitPregancyWait;
int maxWaitPregancy_acutall=0.8*maxWaitPregancyWait;
vars.putObject("minWaitPregancy_acutall", minWaitPregancy_acutall);
vars.putObject("maxWaitPregancy_acutall", maxWaitPregancy_acutall);
So far so good, it is saved and I can see it using the debugger.
Now I would like to use it in say a "Uniform Random timer".
I set the constant delay to 0 and in the Random Delay Maximum I try thing like:
${__Random(${__eval(vars.getObject("minWaitPregancy_acutall"))},${__eval(vars.get("maxWaitPregancy_acutall"))})}
For some reason that (and other variations, like skipping __eval) doesn't work I get variations of:
"java.lang.NumberFormatException: For input string: "vars.getObject("minWaitPregancy_acutall")""
So I guess I don't understand how to retrieve and use the data from the user defined variables. Any help??
As per Elements of Test Plan -> Execution Order
0. Configuration elements
1. Pre-Processors
2. Timers
3. Sampler
4. Post-Processors (unless SampleResult is null)
5. Assertions (unless SampleResult is null)
6. Listeners (unless SampleResult is null)
As per User Defined Variables documentation:
The User Defined Variables element lets you define an initial set of variables, just as in the Test Plan.
Note that all the UDV elements in a test plan - no matter where they are - are processed at the start.
It looks like you're using the wrong test element, if you want to calculate a random variable and save it you need to use a PreProcessor, for example User Parameters

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