Probability Distribution Code in JMeter

Skewness:

“In probability theory and statistics, skewness is a measure of the asymmetry of the probability distribution of a real-valued random variable about its mean. The skewness value can be positive or negative, or undefined.”  (source wikipedia)

 

Exponential:

“In probability theory and statistics, the exponential distribution (also known as negative exponential distribution) is the probability distribution that describes the time between events in a Poisson point process, i.e. a process in which events occur continuously and independently at a constant average rate.” (source wikipedia)

 

When designing application simulation model for performance testing you will come across scenarios that will require you to use different probability distribution to emulate correct production behavior. For example, average # of items per order or think time between pages.

 

The code below is an example of how you can create a skewed or exponential distribution in JMeter.

 

Skewed distribution code

min = VALUE; //update this value to minimum value expected in the distribution
max = VALUE; //update this value to maximum value expected in the distribution
bias = VALUE; //update this to a value against which  the distribution should be biased toward
influence = 1; //[0.0, 1.0] – 1 means 100% influence
rnd = Math.random()*(max-min)+min;
mix = Math.random()*influence;
result = rnd *(1 – mix) + bias * mix;

 

NOTE: The above code is from stackoverflow and I don’t remember the link to it. If you do, please let me know you I can refer to it.

 

Exponential distribution code

Avg = VALUE; //update this value to reflect mean value for the distribution

MIN = VALUE; //update this value to minimum value expected in the distribution

result = (long)(-(double)Avg*Math.log(Math.random()))+MIN;

 

Example (Exponential distribution):
MIN = 1;
Avg = 2.5;
result = (long)(-(double)Avg*Math.log(Math.random()))+MIN;
If the above code is executed for 200 iterations/thread, it will generate the values depicted in the histogram below. More iterations executed, better the distribution will be. For testing, two threads were used.

1-Exp-300x187
NOTE: If you want to have a hard max boundary, add an if condition in the code to check against the MAX value.

 

Example (Skewed distribution):

min = 1;

max = 10;
bias = 3;
influence = 1;
rnd = Math.random()*(max-min)+min;
mix = Math.random()*influence;
result = rnd *(1 – mix) + bias * mix;

If above code is executed for 200 iterations/thread, it will generate values depicted in the histogram below. More iterations executed, better the distribution will look be. For testing, two threads were used.

1-skew-300x197

 

Use beanshell sampler to generate the value and save it in a variable. Pass variable into the loop controller to control it. Below is the code in beanshell sampler.

 

1-Exp-dis-300x183

1-skew-dis-300x247

 

NOTE:

1: Make sure you run a few tests to get the distribution right to reflect what is happening in production.

2: If you have a better code to generate probability distribution be it exponential or any other kind, I would love to  know.

3: Use JSR223 sampler than beanshell sampler. I have noticed that beanshell sampler throughput is less compared to JSR223 sampler.