Part 1: log, OUT, vars, props
Part 2: SampleResult, prev, sampler, ctx


In this post, I will continue to introduce to you next variables which we can use in the JSR223 with Groovy script.

When using this feature, ensure your script code does not use JMeter variables directly in script code as caching would only cache first replacement. Instead use script parameters.

Checked Cache compiled script if available is recommended

1. Parameters

List of parameters to be passed to the script file or the script. You can access the values entered here in the JSR223 Sampler/Post-PreProcessor by using the following pre-defined variables:

  • Parameters: returns all the value which was defined in the Parameters field
  • args: returns array of parameters
  • args.length: returns the number of value in the Parameters field
  • args[n]: with n=0,1,2… it returns the value in the corresponding position

Note: Multiple values must be separated by spaces.

Example 1: (Download here) I defined 3 parameters

jmetervn jsr223-groovy part3

and access it via the script

log.info("Number of parameters: " + args.length);
log.info("Parameters array: " + args);
log.info("All parameters: " + Parameters);
log.info("First parameter: " + args[0]);
log.info("Second parameter: " + args[1]);
log.info("Third parameter: " + args[2]);
log.info("This is the post for " + args[0] + " " + args[1] + " " + args[2]);

Let see how it works

jsr223-groovy-3-sample-1.png

2. Script File

Name of a file to be used as a JSR223 script. It will override the script. So if you’re using the script file, everything in the script area will be ignored.

Example 2: (Download here) if a relative file path is used, then it will be relative to directory referenced by user.dir System property, and usually, it’s $JMETER_HOME\bin directory.

I create a simple groovy script file sum.groovy (download here) with the content like below

def sum_two_number = { a,b -> a + b }

log.info("result of 2 + 3 = " + sum_two_number(2,3))

Then I save this file into JMETER_HOME\bin folder.

Go back to the JSR223 Sampler/Pre-PostProcessor, input sum.groovy into File Name field of Script File. Please not that: the script file is now under $JMETER_HOME\bin directory, the you just input the file name is enough.

I also put the command log.info into Script field to prove it will be ignored.

jsr223-groovy-3-sample-2.png

Example 3: (Download here) It’s better if the script file is located in separate folder and relates to the script file. And the following is the way how to do it

The folder structure looks like

jsr223-groovy-3-sample-3-1.png

Prepare: The groovy script file has content:

def multiply_two_number = { a,b -> a * b }

log.info("result of 2 * 3 = " + multiply_two_number(2,3))

Step 1: Create in UDVs a variable to store the directory of test plan,

Name Value
PATH  ${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}

Note: the value should not break down the line, it’s in one line.

jsr223-groovy-3-sample-3-2.png

Step2: In JSR223 Sampler/Post-PreProcessor, input the path to script folder and script name into the File Name

jsr223-groovy-3-sample-3-3.png

Note: ${__BeanShell(File.separator,)} will return the / in Unix (Mac, Linux, …) and \ in Windows. So you can consider it as:
– Windows: ${PATH}\scripts\multiply.groovy
– Unix: ${PATH}/scripts/multiply.groovy

So using ${__BeanShell(File.separator,)}, my example can run normally in any OS.

Step 3: Run and observe the result

jsr223-groovy-3-sample-3-4.png

Conclude: With the Script File, we can reduce the duplicate code in our test plan, just write the common function somewhere (should be related to the text plan .jmx file), and refer to each JSR223 element. Somedays, if you want to upgrade or optimize your script, just edit only 1 file is enough. It helps you to save much effort.

3. Using Parameters and Script File together

In section 1 and 2, we all know how the Parameters and Script File work. And now, I will show you how to use them together. Imagine, we define a script, and every time we use it, we have some other different parameter to process. So this is the best way to do this.

Example 4: (Download here) I have the requirement:

  • Input into Parameters field with format
    [operation] [number1] [number2] [number3] ...
  • The [operation] is sum or multiply
  • The number1 is Integer format
  • If the operation is sum , then sum all the numbers. And print the result
  • If the operation is multiply, then multiply all the numbers. And print the result

I will do the following steps:

Step 1: Prepare the operation.groovy file with the content:

//check if the Parameters is null
def operation = Parameters ? args[0].toLowerCase() : 'null parameters';

//check the operator
if (operation != 'sum' && operation != 'multiply') {
 SampleResult.setSuccessful(false);
 SampleResult.setResponseCode("500");
 SampleResult.setResponseMessage("Missing or incorrect the args operation. Expected 'sum' or 'multiply'. But found '" + operation + "'");
 SampleResult.setResponseData("");
 log.info("Missing or incorrect the args operation. Expected 'sum' or 'multiply'. But found '" + operation + "'");
 return true;
}

log.info("The operations is: " + operation);

//check the list of number
for (i = 1; i < args.length; i++) {
 if(!args[i].isInteger()) {
 SampleResult.setSuccessful(false);
 SampleResult.setResponseCode("500");
 SampleResult.setResponseMessage("There is an incorrect number in the list: " + args[i]);
 SampleResult.setResponseData("");
 log.info("There is an incorrect number in the list: " + args[i]);
 return true;
 }
}

//calculate in case of sum
if (operation == 'sum') {
 long sum = 0;
 for (i = 1; i < args.length; i++) {
 sum += args[i].toInteger();
 }
 log.info("sum = " + sum)
}

//calculate in case of multiply
if (operation == 'multiply') {
 long multiply = (args.length > 1) ? 1 : 0;
 for (i = 1; i < args.length; i++) {
 multiply *= args[i].toInteger();
 }
 log.info("multiply: " + multiply)
}

Step 2: Save operation.groovy file to the script folder, and should be related to the script .jmx file like:

jsr223-groovy-3-sample-4-1

Step 3: Go back JSR223 Sampler/Post-PreProcessor, and call the script as the way I did in Example 3.

jsr223-groovy-3-sample-4-2.png

Step 4: Define the Parameters and try running the test

Case 1: sum 1 3 5 7 9 10 20

And the result is: sum = 55

jsr223-groovy-3-sample-4-3.png

Case 2: multiply 5 10 15 20

And the result is: multiply = 15000

jsr223-groovy-3-sample-4-4.png

Case 3: test 1 2 3 4 5

jsr223-groovy-3-sample-4-5.png

And there are still some other cases, and you can try it by yourself

Case 4: 2 4 6
Case 5: input nothing into Parameters field
Case 6: sum 1 10 a 11


Example 1: Download here
Example 2: Download .jmx here, the sum.groovy here
Example 3: Download here
Example 4: Download here

All in one: Download here


Part 1: log, OUT, vars, props
Part 2: SampleResult, prev, sampler, ctx

Advertisements