Milind_open_source
Milind_open_source

Reputation: 151

providing Dynamic IP addresses in Jmeter

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?

Upvotes: 2

Views: 3981

Answers (2)

ant
ant

Reputation: 22948

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) :

enter image description here

Results (with some added samples just for clarity sake) :

enter image description here

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.

Upvotes: 3

Gábor Lipták
Gábor Lipták

Reputation: 9776

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:

Http request defaults with JS function HTTP request sampler One "ALMA" result One "KORTE" result

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.

Upvotes: 3

Related Questions