Writing JMeter Functions in Groovy
Apache Groovy is a scripting language that has become the default option in JMeter JSR223 elements. To accommodate that, the Groovy library has been included by default in JMeter ever since version 3, and no manual steps need to be performed to use it. Previous JMeter versions required downloading Groovy and manually including it in JMeter libraries.
Groovy has become the recommended scripting language for JMeter because it’s easy to use, it is flexible and it is constantly being developed. In addition, Groovy can be fully integrated with Java, which provides many scripting capabilities via an expressive, concise and readable syntax. You can read more information about Groovy and why we recommend choosing it from this blog post Apache Groovy - Why and How You Should Use It.
Why is this the case? Because each Beanshell sampler has its own copy of the Java interpreter for each thread, which will be loaded in your system’s memory on each iteration of your test run, thus adding overhead. On the other hand, Groovy implements the Compilable interface, meaning that it can be executed repeatedly without recompilation. This behavior is further explained in the blog post Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For!.
Now we will explain how Groovy scripts can be developed and integrated in the relevant JMeter elements: the JSR223 Pre Processor, the JSR223 Post Processor and the JSR223 Sampler.
Groovy Scripts Structure
The following structure is recommended to follow programming good practices:
Import statements are included at the beginning of the script for any of the classes that will be used:
Import statements allow including specific members of a package in your script. In this example, the FileServer is referred in the script, and now all the functions implemented in it can be used.
Next, add variable definitions. This is where you should set the variables that will be used in the script. Some variables are used locally in the script while others are used to get JMeter Test plan defined variables values.
In order to set a Groovy script variable with a JMeter variable value, the vars.get() method must be used as followed:
def month = vars.get(“month”)
It is not necessary to declare the definition type for the variables.
Finally, write down the statements that develop your script. The following presents a summarized list of statements:
- if, if/else, switch - for decision making
- while, for, for-in - for loops
- break, continue - for loops control
- Arithmetic, relational, logical, bitwise and assignment operators
Now, let’s look at a short example of a Groovy script where the vars.put("variable", "value") method is used to set a value to a JMeter variable. Here, the variable value to be used on an assertion, is loaded from a file:
Using Groovy in JMeter
When it comes to improving JMeter’s functionality, Groovy has proven to be a powerful, and yet lightweight, language in terms of performance. We will now present examples where JMeter’s functionality is improved with Groovy.
In each of the following, JMeter JSR223 elements are used with Groovy scripting language, as shown below:
PreProcessor elements are executed before requests are sent in a test. Let’s look at an example with Groovy:
We will take a CSV file with transaction data, in the following format:
We will use Groovy to filter transactions for a specific month and year, which are defined as variables in the JMeter script. Each line of the input file will be read, and when a transaction match is found in the date fields, it will be written to an output file.
Groovy lets us easily work with files and filter lines based on specific criteria. This example focuses on reading each line, by using the the File.eachLine() function. Then, it uses String.split(), with a comma as a field separator, and finally String.endsWith(), to compare the date field.
Results are written into an output file, which is created if it does not exist yet. Lines are appended to the end by using the left shift operator (<<).
PostProcessor elements perform actions after the samplers are executed. In this example, HTTP response bodies and the thread name are saved in a response text file, by using the JMeter JSR223 PostProcessor element. Groovy is used to read the body and the thread name from the previous JMeter element.
This script generates the following output on the responseFile file (output truncated):
JMeter Sampler elements allow taking actions in any place in the test, without depending on a specific request.
In this example a JSR223 sampler will be used to send HTTP requests to either BlazeMeter or JMeter websites, based on the thread id. The thread id is the number of threads and its name, and the Thread class is used to get this information.
This sampler is placed right above the HTTP request in the JMeter tree, so the thread has the “server” variable properly set.
The log viewer panel will show:
How Groovy Helps Debugging
In most of the examples, the log.info() function is used to write relevant information into the log. This is a good practice for debugging purposes. The functions log.warn() and log.error() can also be used depending on the message to be logged. We will now illustrate how to improve your debugging by the use of these functions.
This example extends the Preprocessor one detailed previously, by adding a sentence if the file to be read in the filesystem does not exists. The following line is added at the end of the script:
In case the inputFile to be read is not accessible, an error message will be logged and the alert icon on the top right will be increased by one:
That’s it! You now understand the importance of Groovy functions in JMeter. We recommend adding JS223 elements in your scripts with Groovy to extend your JMeter functionalities. You can learn more Groovy functions here.
To learn more JMeter, check out our free advanced JMeter academy.