Useful Groovy scripts for Home tasks and for using in common life.

Using Groovy scripts you can upgrade your scenarios and make them extremely flexible.

The most simply Groovy script is Percentage distribution script.
As you know our users do not move over the ‘app’ one-by-one from one page to another. Taking your own experience into consideration you can see, that you are not going to visit ALL the page on this or that site. You, probably, will ‘select’ your favorite pages and will visit them more often than another pages.

So, our user are distributed inside our ‘app’ with some proportion. We need to find out this proportion and configure it using Groovy script.

We will get more relevant results in such case. Example of code could be found in “script_additional.jmx” file or in roll-over section.

The other way of Groovy scripts using – is Data preparation.
From time-to-time we need to make our Data a little bit flexible or we need to take some ‘specific’ data.

In this case we also can use Groovy scripting. As an example of preparing some ‘specific’ data, you can see “script_additional.jmx” file –> ‘Thread Group – Operating with some file’ or find it inside the roll-over section.

For some-how data randomization, also see “script_additional.jmx” file –> ‘Thread Group – Randomizing Data’ or… you know what to do… See above.

// Groovy script for jMeter
// Random selection of Step with percentage distribution

Random rnd = new Random()

// Defining Percentage List

def percents = [15, 20, 10, 5, 50]

// Generating Random number
def rand = rnd.nextInt(100)

int n = 0
// Variable that will be used in scenario
def group_number

// Checking that number belongs to some group
for (i in 0..percents.size()-1) {
n += percents[i]

if (rand <= n) {
// Variable name that will be used in Switch Controller
// Value must be converted to String
vars.put(“group_number”, i.toString())
break
}
// As a result “group_number” will be simple number, example: “rand number occures to be 40. So we will have “group_number” = 2 (according to list of values we have in this code).”
}

import org.apache.jmeter.services.FileServer;

// Define number of lines to be generated
def number_of_lines = 3;

// Define random line-number from witch to start taking lines.
// In ranges from 0 to x – “number_of_lines” will be taken one-by-one from Generated “random_file_line”
def random_file_line = new java.util.Random().nextInt(12);

// Taking file from the same Folder where current *.jmx file executed from.
String file_path_name = FileServer.getFileServer().getBaseDir() + “/users_data_file.txt”;

// Reading the file.
def file_data = new File(file_path_name);
def file_line = file_data.readLines();

// Defining list of Data to be used in request
def data_list = [];

// Preparing Data for sending
for (i in 1..number_of_lines) {
String user_name = file_line[random_file_line].split(“\t”)[1];
String user_age = file_line[random_file_line].split(“\t”)[2];
random_file_line++;
data_list.add(“{\”userName\”: \”${user_name}\”, \”userAge\”: \”${user_age}\”}”);
}

// Assigning value to variable, that will be used in scenario
vars.put(“preparedData”, data_list.toString());

// Generating max number of symbols from 50 to 1000.
def max_symbols = Math.abs(new Random().nextInt() % (1000 – 50) ) + 50

// Define alphabet to use in generation
String alphabet = ( (‘A’..’N’) + (‘ ‘) + (‘P’..’Z’) + (‘ ‘) + (‘a’..’k’) + (‘ ‘) + (‘m’..’z’) + (‘ ‘) + (‘0’..’9′) + (‘ ‘)).join()

// Generating random text
def rand_text = new Random().with {
(1..max_symbols).collect{alphabet[nextInt(alphabet.length())]}.join()
}

def rand_year = Math.abs(new Random().nextInt() % (2020 – 2010) + 2010 )
def rand_month = Math.abs(new Random().nextInt() % (12 – 10) + 10 )
def rand_day = Math.abs(new Random().nextInt() % (28 – 10) + 10 )
def rand_date = rand_year + ‘-‘ + rand_month + ‘-‘ + rand_day + ‘ ‘ + ’00:01’

// Getting the loop number and comparison
// “loopCounter” is variable name we provide in ‘Counter’ element
int counter = vars.get(“loopCounter”) as Integer

vars.put(“body_random”, rand_text)
vars.put(“title_random”, rand_text.substring(0, 30))
vars.put(“date_random”, rand_date)