I am having a Jmeter script which is used to hit two IP addresses/ server names. currently I use two Jmeter instances for hitting the two server names. Is there any way to do this in single Jmeter instance, like parameterization of Server names/IP addresses in HTTP Request Default?
Interesting. I never tried before, but it seems the dynamic values, which you define in request defaults are always re-evaluated.
So you can define it as a javascript random and two strings depending on random value:
${__javaScript((Math.random()<0.5)?'ALMA':'KORTE')}
Screenshots of the plan and the results:
Generally its still nicer to put the names into a csv file, and to use CSV config element, and to say, that the CSV has a rollover. Quote from best practices:
16.5 User variables Some test plans need to use different values for different users/threads. For example, you might want to test a
sequence that requires a unique login for each user. This is easy to
achieve with the facilities provided by JMeter.
For example:
Create a text file containing the user names and passwords, separated
by commas. Put this in the same directory as your test plan. Add a CSV
DataSet configuration element to the test plan. Name the variables
USER and PASS. Replace the login name with ${USER} and the password
with ${PASS} on the appropriate samplers The CSV Data Set element will
read a new line for each thread.
Although I like Gábor Lipták answer I'm gonna offer the alternative for fun.
Let's say you want to perform your test on both DEV and PROD environments(imaginary environment). Where DEV connects to host1, PROD connects to host2 and endpoints being the same /myserviceendpoint
So start by adding User Defined Variables to your thread group. Let's add two variables :
ENVIRONMENT -> ${__P(environment,host1)} and
ENDPOINT_PATH -> /myserviceendpoint
Add Http Request sampler and inside Server Name or IP set it's value to ${ENVIRONMENT} and other ports etc. change accordingly.
So now your default environment is DEV. If you want to change your environment to PROD add BSF Preprocessor and change the ENVIRONMENT variable to PROD instance.
vars.put("ENVIRONMENT", "host2");
So you can disable/enable this BSF Post Processor in your test to toggle between DEV/ PROD. This is for GUI Mode.
This will come handy when you have big test and you're running jmeter with no GUI mode. This part is cool ${__P(environment,host1)} this is where if you pass no parameters via command line DEV value will be used otherwise you can inject value to overwrite DEV environment i.e. (see more here):
jmeter -n -t yourtest.jmx -l testresults.xml -Jenvironment=host2 //running `PROD`
jmeter -n -t yourtest.jmx -l testresults.xml //running `DEV`
Here is how that looks like screenshots (test plan) :
Results (with some added samples just for clarity sake) :
So you toggle environments or running with no gui and inject via command line. You can also do this for other attributes as well i.e port/endpoint etc.
Related
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.
The test scripts on GUI works perfect but not in the non-GUI mode.Image showing Terminal test execution
Explanation:
I have 3 thread groups in the test plan, where the first Thread Group creates 3 .csv files and are expected to feed the data in the CSV files into the second Thread Group.
For few test runs, Non-GUI mode worked great but then, the tests Intermittently like this [Terminal showing test run], doesn't create the CSV files at all and also,
sometimes, third Thread Group doesn't execute at all.
The problem I noticed is:
During the NOn-GUI tests the files are not been creating, so that's the reason the next Thread groups weren't able to pick up and use the variables inside CSV file and also
The second One could be: I'm saving folder and file paths in the
${__setProperty(prop_folder_Path,${File_Path})} and getting the prop.
${__property(prop_folder_Path)} in another Thread Group -> Sometimes this property function Doesn't work and files are saving in /bin Directory
Is there a way to use ${__property(prop_folder_Path)} value in BeanShell POStProcessor ?
I believe whatever works in the GUI should work same in the Non-GUI too, AM I right?
I Just noticed that output files are creating in /bin folderas shown here while running the tests in NOn-GUI
Any suggestions to fix this. Thank you
My expectation is that your ".csv" files creation fails somewhere somehow therefore 2nd thread group is not able to operate due to missing files.
JMeter doesn't have any build-in functionality to write something into the file so I think you implemented some custom logic using i.e. JSR223 Scripting which doesn't work. The reasons could be in:
If you're running JMeter from another folder the .csv files can be created in a different location, try using full paths just in case.
Non-GUI mode tends to be faster than GUI mode so it might be the case of multithreading issue i.e. when multiple threads are trying to write data into the same file concurrently and clash or produce not well-formed data.
In both cases the answer will live in jmeter.log file, check it for any suspicious entries and fix the causes.
In general using files to pass data between thread groups is not the best idea, I would recommend doing it in-memory instead, for example:
In 1st Thread Group use __setProperty() function to convert your data which you store in CSV file into JMeter Properties like:
${__setProperty(foo,bar,)}
In 2nd Thread Group use __P() function to read the data like:
${__P(foo,)}
More information: Knit One Pearl Two: How to Use Variables in Different Thread Groups
I am trying to build a jmeter testplan, where all the test values are sent from a csv datafile.I want to add assertions(provided in the datafile) to my HTTP Request at runtime and execute the test. The reason behind doing this is to keep the plan flexible according to the number of assertions. In my case, the assertions are getting added at the runtime; however they fail to get executed. May I know what should be done to get the components added and executed in the same flow?
For example: A part of plan looks like:
XYZ
--HTTP Sampler
-- Response Assertion1
-- Response Assertion2
-- JSON Extractor
where XYZ -->keyword based transaction controller(reusable component)
Everytime I have a request of type XYZ, this chunk of components will get executed. In my case, I do not want to place anything such as Assertions, pre/post processors, extractors in the test plan already. I want to generate these components at run time and execute them (as per my test requisites).
Issue: The problem here is that I cant load the components programmatically and execute them in the same flow. The reason being, the compiler does not know beforehand what all components it needs to execute, so it bypasses the newly added components.
So, I need some alternative solution to execute this.
You can add Response Assertion (or multiple) with Pattern to test filled with a variable as ${testAssert1} and set the variable by default as empty, for example
Put in User Defined Variables name testAssert1 with empty value.
Your assertion(s) will pass until you on run time set the variable with a different value, for example using User Parameters Pre Processor.
I would like to have my tests parametrized so I could run the very same test with different number of users. I know that I could define some user-variable in my test plan, but I would like a solution which would be more friendly for a regular user (who doesn't know JMeter at all).
I've tried using CSV configuration but it appears that I cannot inject (${numberOfUsers}) into thread group. Is there any workaround? Maybe I could pass number of users when running my test from command line?
Ok, turned out to be really easy-peasy. Instead of acutal users number I set something like this ${__property(myPropertyName)} in my thead group in JMeter. Then I run my tests like :
jmeter -t path/to/test.jmx -JmyPropertyName=10
And the test group would have 10 user.
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)