Apache Groovy – Why should you use Groovy in Jmeter?

Tram Ho

Jmeter is a very powerful API testing tool.

It has enough Test elements to perform most test cases, even a complex scenario. However, in certain test cases it is not possible to perform by using only the default Jmeter elements and functions.

Hence, it is possible to write scripts with Jmeter and Java API methods to bypass these limitations. This leads to the question of which language is appropriate?

Why use Groovy

Apache Groovy is an object-oriented programming language that can be used for the Java platform.

Groovy is one of the languages ​​that can implement Compliable Interface, so it’s great for doing complex calculations because the script will only need to compile once for all streams and loops, we will No need to waste resources for this anymore.

It has outstanding features such as:

1. Development and support

Groovy language is constantly being developed, maintained and supported.

2. Java compatibility

  • 99% if the java code is valid, then the Groovy code is also valid.
  • If the code was developed in Java 6,7 ​​or 8 it will be considered valid Groovy code. For example if you use Beanshell then you are limited to Java 5 language level, there will be some of the following limitations:
    • There is no binary literals
    • There is no string in the “switch” statement.
    • Without Genrics
    • There are no try-with-sources
    • There is no handling of many exceptions

3. Performance

Starting with version 2, the scripts can be statically compiled providing almost Java code performance.

Let’s compare the performance of Groovy scripts with Beanshell scripts by doing something very basic, such as printing Jmeter test start time (making relevant Jmeter TESTSTART.MS into jmeter.log file).

At first glance, Beanshell is faster (1ms response time for Beanshell Sampler compared to 5ms response time for JSR223 Sampler). This is due to the Groovy scripting task size. Since the size of the groovy-all-2.4.7 library is close to 7 MB, it takes more time to load it. So if you use the script for something very soft and is called once then you should still use Beanshell.

Now let’s test something “heavier”, such as compute the 36th number in the Fibonacci sequence and print it into the jmeter.log file using Beanshell and Groovy.

The above code is used in both Beanshell and JSR223 Samplers

As you can see, JSR223 Samplers computes and prints out in 192ms, while Beanshell Samplers takes 36 seconds to do the same thing. So if you use scripts to load or repeatedly call with multiple threads then the obvious choice is Goovy.

4. Additional features

a. Easy file writing and reading

  • Assuming you want to instantiate a file object, you can write to it as simple as this:

    myFile << "Hello from Groovy"

  • Or reading the file only requires access to the .text property of the file object:

    def fileContent = myFile.text

  • Put everything together like the image below:

b. Working with collections is easy and fast

props.each { k, v -> log.info( "${k}:${v}") }

And here is the output:

c. Execute external commands

With Groovy, you can forget about OS Process Sampler, Run operating commands and executable files just need:

"your_command".execute()

If you need output – then simply add .text at the end of the statement:

"your_command".execute().text

Below is an example of executing the jmeter -v command and printing the output to the jmeter.log file:

References: https://www.blazemeter.com/blog/groovy-new-black?utm_source=blog&utm_medium=BM_blog&utm_campaign=the-groovy-templates-cheat-sheet-for-jmeter

Share the news now

Source : Viblo