Wednesday, October 15, 2008

JMeter Properties Help, Tips, and Examples

Unlike User Defined Variables in JMeter, that seem to be a hack, Properties seem to be well handled and robust in use.

The first benefit of properties is they can be defined outside of your test plan. This allows you to feed parameters into a test plan. Common methods you can use (but not an all inclusive list) to define properties are

- Define them in a file which is read in when JMeter starts
- pass them on the command line to JMeter using -Jpropertyname=value
- in your test plan using __setProperty function

Unlike User Defined Variables, there is no separate controller for setting or manipulating Properties, so initially you may pass them over. Don't! You just need to get used to evaluating, computing, and setting them 'in line' with the rest of the test. You can also cheat and put functions in places like comments of test elements. So if you want to say use __setProperty and nothing else,, you can create a simple Test Action Sampler and put the function in the comment field of the sampler.

Properties have the benefit over Variables in that
- their scope is the entire test, not just a local thread
- they can be redefined during the test if needed
- their values can be fed into the test externally
- the _property function allows the use of default values

An example in some of my tests is I need to populate some variables for my simulation externally so they can be flexible and controlled by an external script. How many threads to use, how many loops, etc. However, I do not want to be burdened with defining all these parameters every time the test is ran, only if I want to change them.

The best way to do this is use the __property function with it's default value parameter. __P is a shorthand property function. So the Loop Count Parameter of my Loop Controller is defined as


If the property named p_ptploopcount is not defined, the test will loop once, and will not fail to execute because of a missing value in the Loop Controller. I don't have to worry about defining p_ptploopcount before using it either since I used the arguement that assigns a default value of 1 if it hasn't been defined. I can override the default value to be 10 on the command line by doing something like this

jmeter -t testplan.jmx -Jp_ptploopcount=10

This method allows for very flexible test plans that can be scaled up or down without modifying the test plan itself.

You can also pre-populate properties using a file. The gotcha to watch out for though is if you are using the jmeter GUI, the file is only loaded when jmeter is launched. So you can not change the values, and simply re-run the test without closing and reopening JMeter.

Make sure you remember to name your properties and variables accordingly so you don't forget how to reference them! Variables can be referenced simply by ${name} where properties must be referenced using the __P or __property function

I name all my properties with a p_ prefix to ensure I remember they are properties, and not user defined variables

No comments: