JMeter Thread Groups - Is it possible to make the same instance of one behave as a time based or an iteration based element dynamically at runtime? - jmeter

I am creating a JMeter Test Harness that will be run from Jenkins using "Build with Parameters' to set runtime parameters that are passed into JMeter as Properties using -J attributes.
End users will be given the choice of running tests either based on the number of iterations or a period of time.
I can easily interrogate a passed in property value to determine if an 'iteration' or 'time' based test type and in order to try this I have added variables (which may or may not be have values depending) for ALL of the Thread Group 'Thread Properties' fields as can be seen below. Each variable will have a value of either 0 or 1+ depending upon the Jenkins parameters passed in at runtime.
enter image description here
As I do not want to create 2 Thread Groups (one for iteration based, and the other time based) then use logic controllers or similar to control what way to test, is there a dynamic way to switch the Thread Group 'Thread Properties" behaviour at run time using the singular Thread Group?
enter image description here
Any help in making 1 thing do 2 things in my little world of JMeter will be greatly appreciate!!

Not really, with "vanilla" JMeter you can:
Set number of iterations to -1 for "duration" based test
Set duration to a very long value (a couple of years) for the "iteration" based test
Other options are in:
JMeter .jmx scripts are basically XML files and XML is a subset of text so you can use a text editor like sed to substitute values directly in the .jmx file
Taurus automation framework has a nice feature of modifying an existing JMeter test before launching, it allows overriding absolutely any property of absolutely any element.

Related

JMeter - How can you see/manipulate a Thread Group's - 'Thread Properties' values

I am developing a Test Harness, for which some on here have assisted, so great thanks to you all.
But as part of my Test Harness journey (and stupidity along it) I wanted to know what exactly was being sent when a Thread Group is started, specifically, the ThreadGroup's 'Thread Properties' values for the:
Loop Count: Infinite checkbox
Specify Thread lifetime checkbox
Is it possible to get and manipulate the values of the fields via say JSR223 or Beanshell elements as I would like to manipulate them at runtime?
As of JMeter 5.5 there is no possibility to change these parameters in the runtime
The options are in:
Use JMeter Properties to pass the values from file or command-line arguments when you start the test
Basically the same as point 1 but if you need i.e. more complex calculations you can amend the properties somewhere in setUp Thread Group using suitable JSR223 Test Element (using Beanshell is not generally recommended)
It's also possible to set the "Loop Count" to "Infinite" and "Thread lifetime" to something very big and stop the test using JSR223 Test Element or Flow Control Action sampler if it helps

JMeter: How to assign weights/frequencies/smapleRates to ThreadGroups/"TestSets" dynamically from CSV file

I would like to create a JMeter test setup with important sampling and test variation parameters entirely controlled by csv-Files (i.e. not modifying the JMX-File). It should be run with maven.
Ideas is as follows:
sources.csv contains
sampleRate;file
40;samplesForController1.csv
30;samplesForController2.csv
5;samplesForController3.csv
...
SampleRate should determine, how often a certain number of tests (defined by parameters in the respective file) should be executed (relative to others).
How could this be achieved? Im asking for the first step here (make sure the files/testSamples are sampled/executed given the indicated sampleRate) as I think I can solve the second part (dealing with parameters in samplesForController1.csv etc.) by myself.
P.S.
I'm struggling with the options presented here: In Jmeter, I have to divide number of thread into the multiple http requests in different percentage but have to keep sequence remain same since
afaics, thread groups cannot be created on-the-fly/dynamically/progammatically
apparently, Throughput Controller needs to know its child element(s probablities) upfront (i.e. not created dynamically), otherweise, sampling is very odd (I could not get it working maintaining the desired sampleRate)
I did not try to integrate jmeter-plugins in the maven build thus far as my impression is, the available plugins/controllers also needs to know their child element upfront
It is possible to create thread groups programmatically, check out:
Five Ways To Launch a JMeter Test without Using the JMeter GUI
jmeter-from-code example project
jmeter-java-dsl project
You can use Switch Controller and use a function like __groovy() to generate the child element index, example implementation can be found in Running JMeter Samplers with Defined Percentage Probability article
It's not a problem to use JMeter Plugins with Maven, see Adding jar's to the /lib/ext directory documentation section for example setup

JMeter: thread switching between variables for preset time periods throughout a test run

I have a script with a once only loop where a thread uses reg expression extractor to get a variable into an array. After the once only loop, the thread selects a random instance of that variable and then continues with that instance of the variable for the entire test run.
It would be more realistic for the script to do the following:
Jmeter thread uses variable_1 for x seconds, switch to variable_2 for x seconds, switch to variable_3 for x seconds
Variables_1,2,3,x coming from the reg ex array should be selected randomly
x seconds could be 300, 600, 1200 and selected randomly
Is this something that can be accomplished with JMeter controllers/functions or would it require something along the lines of custom beanshell code?
It can be done using ForEach Controller & Runtime Controller.
For Each controller is to iterate the variables one by one.
Under Runtime controller, you keep all your samplers to be executed for the duration.
Test Plan:
For Each Controller config:
Runtime Controller config:
I would recommend reconsidering your approach as this way you test won't be repeatable. The whole idea of testing is that when you run the test 2 or more times - you should get the same results, elsewise if you discover a product issue you won't be able to reproduce it to ensure that it is fixed.
Coming back to your question: yes it can be done via JMeter Test Elements without having to go into scripting. Take a look at __chooseRandom() function available via JMeter Plugins project. The easiest way of installing JMeter Plugins and keeping them up-to-date is using JMeter Plugins Manager

Is it possible to create a User Defined Variable in JMeter using an expression?

I've been trying to use JMeter to create some automated Performance Tests and I'm setting up a job in Jenkins so that people can run it and view the results there.
I'm trying to add a few parameters to the job that correspond to the options available in the Thread Group. They are:
Number of Users
Ramp-up Period
Loop count
Some people don't quite understand the concept of the 'Ramp-up Period' so instead I wanted to make refer to it as 'Delay' and use it to control how long each thread will wait before starting the test. This would mean that the 'Ramp-up Period' should be equal to (The Number of Users x Delay).
The command I'm using to run the test is below:
jmeter -n -t <myscript>.jmx -l results.jtl -j jmeter.log -JUSERS=10 -JDELAY=1 -JLOOPS=1
and the variables look like this:
USERS = ${__P(USERS)}
RAMPUP = ${USERS}*${__P(DELAY)}
LOOPS = ${__P(LOOPS)}
But unfortunately the value of the RAMPUP variable was not what I expected. It ended up being "${USERS}*1"
Is there a way to do this in JMeter?
JMeter variables and properties are Strings, you can not apply arithmetic operations to them. The behavior you're getting is absolutely expected. Also there is no possibility to dynamically change properties during runtime, you'll have to calculate ramp-up prior to passing it to JMeter.
You can add BSF PreProcessor before you test plan, calculate there values for user defined variables and put values into variables using putObject(String key,Object value)

What is purpose to use setup threadgroup and teardown threagroup in Jmeter? Please explain same with example

What is purpose to use setup threadgroup and teardown threagroup in Jmeter? Please explain same with example.
I know why we use thread groups and also aware of fact that setup is for pre activities like creating user and monitoring purpose but not sure with an incident where can i use it. same with tear down.
It sounds like you have pretty much figured it out already, but let me give you a few examples of when I've used it.
setup:
Get a large data set from a database into a jmeter variable for use during the test.
Get and log the version number from the system under test:s version number.
Run a javascript to set jmeter properties based on more simple input parameters/properties. Lets say you want to configure the selection of target host a simple true/false value, but in your test you need to expand it to different strings, and you dont want to have logic spread out all over your test plan.
teardown:
Never used it, but I guess it is mainly useful for cleaning up your system (e.g. deleting users that were created during the test)
correct me as i'm probably wrong, but a setUp thread cannot be used to store variables for use on the test threads (that i can see). any variables that i use in the setUp are never available. however, i found that if i use a beanshell and convert the variable in the setUp thread to a property like this
${__setProperty(userToken, ${userToken})};
then on each test thread i either use the property directly like:
${__property(userToken)}
or at the top of my thread i convert the property back into a variable like:
vars.put("userToken","${__property(userToken)}")
however, this seems a bit long winded and it would be great if there was a way to set up variables in the setUP to be used on every thread.
A special type of ThreadGroup that can be utilized to perform Pre-Test/ Post-Test Actions.The behavior of these threads is exactly like a normal Thread Group element.
The difference is that these type of threads execute before/after the test has finished executing its regular Thread Groups.enter image description here

Resources