Control Jmeter test properties with Simplified Property Function ( __P ) in command line

Hi .. how's it going? I thought of sharing something which I found out recently while I was generating a performance script in Jmeter with Continuous Integration using Jenkins.

As you know we use the JMeter GUI mode to PLAN and Non-GUI mode to RUN JMeter scripts. As we design different tests with different thread groups, to execute our performance test plans we need to assign different values to Threads, Ramp-Up Time, Loops mainly.



My main problem was the need to change this value from time to time. If we use the GUI mode to change it and run, It'll work. But It takes some additional amount of time to do that.

So the solution I found is with __P which is the simplified property function of JMeter. What you have to do is really simple. With __P you can assign a variable in your JMeter script and pass it in your command line with -J argument. For example, if your defined variable was 'Threads' and if you add-JThreads=5 in you JMeter command. The variable Threads will be assigned with the number 5.

In __P the syntax is ${__P(VariableName, DefaultValue)} or ${__P(VariableName)} ( If you did not assign a Default value It'll take '1' as the default value ). Best thing is you can have it anywhere in your script. ( AWESOME NEWS right? 👍 )

So I'll let you know the way I used this concept in my performance script.

Let's say we have two thread groups named as Thread Group 1 and Thread Group 2. ( I'll give you an example about controlling the number of threads you can use the same methods, anywhere you want )
First, let's handle the Default Value.

Go to your Test Plan and define a variable name.
Name: DefaultThreads ( Use any name you like )

Then Have your Variable value as,
Value: ${__P(DT,1)}


Now in your command line if you run
jmeter -JDT=2 -n -t<ScriptLocation>/test.jmx

The variable you defined as DefaultThreads will be assigned with the value 2. Got it?

Not let's move on to our Thread Group 1.
In the threads under Thread properties, I'm going to do something similar to what I did earlier.
There, For thread count, I'm going to use: ${__P(Group1,${DefaultThreads})}

And in Thread Group 2 it'll be: ${__P(Group2,${DefaultThreads})}




Well, That's it. we did it.

No all you need to do is run this in your command line.
Let's say you need both the Thread Groups to have 2 threads each and run. So what you need to do is set the DefaultThreads count as 10.

The code will look like
jmeter -JDT=10 -n -t <Script Location>//<Name of the script>.jmx

Since we have 2 thread groups, It should start 20 threads.


Now let's think of a scenario where we need all the other thread groups to have a thread count of 1 but group 1 needs 10 threads.

The code will look like
jmeter -JDT=1 -JGroup1=10 -n -t <Script Location>//<Name of the script>.jmx

Since we have 2 thread groups It should start 11 threads

Now Finally if you want all the threads to run with 10 threads but stop thread group 2 from running, all you need to do is set the thread count of that group 2 to 0

The code will look like
jmeter -JDT=10 -JGroup2=0 -n -t <Script Location>//<Name of the script>.jmx

Since we have 2 thread groups It should start 10 threads


So That's all about it :D
Now once you configured the Jenkins or your favourite CI environment. You can control all your thread groups, ramp-up times, loops, etc. with just a single line of code. ( Some times it'll be a loooong ling but still single 😁 )


Happy Performance Scripting Folks

Muditha Perera
Senior QA Engineer
Intervest Software Technologies pvt. Ltd.

Comments

Popular posts from this blog

QA Engineering Performance Analysis: Don’t underestimate the power of the 99th percentile

Test Automation With Robot Framework

Test Automation With Cucumber