Working with TOML in Groovy 4.0 in JMeter

Reading data or configurations from files (e.g. CSV, TXT, and more) or building file data structures in JSON, YAML or XML is ubiquitous in performance test scripting. In this blog post, we are going to see about working with TOML file in JMeter using the incubating feature of Groovy 4.0.0 using JSR223 Sampler.

What is TOML?

Tom’s Obvious, Minimal Language is a config file format for humans. TOML files are easy to read and parse using various languages. A TOML file typically consists of key value pairs. But TOML supports the following multiple native types:

  • Key/Value Pairs
  • Arrays
  • Tables
  • Inline tables
  • Arrays of tables
  • Integers & Floats
  • Booleans
  • Dates & Times, with optional offsets

Groovy 4.0 Features

Recently, Groovy community released Groovy 4.0 with multiple new features, enhancements, and incubating features. Here are the release note details.

Switch expressions, Sealed types, Built-in Type Checkers, Built-in Macro methods, and more are the new features.

Features like TOML support, Records and record-like classes, JavaShell, POJO annotation, Groovy Contracts, GINQ (Groovy-Integrated Query) are the incubating features.

Working with TOML in JMeter using Groovy 4.0

The most recent version of JMeter is 5.4.3 which is shipped with Groovy 3.0.7. Below are the groovy jars in JMeter 5.4.3.

Groovy in JMeter 5.4.3
Groovy in JMeter 5.4.3

JMeter 5.4.3 also ships with Jackson jars (high-performance JSON processor for Java) as follows.

Jackson in JMeter 5.4.3
Jackson in JMeter 5.4.3

Upgrading to Groovy 4.0 in JMeter

Before you start upgrading, there are a couple of important points to note. TOML support in Groovy 4.0 is an incubating feature. So, I do not recommend using it in production yet. Always take a back up before you proceed.

Create a backup folder in JMETER_HOME\lib folder and move all the files related to Groovy and Jackson jars.

Head to the Groovy site to download the Groovy package. Extract it and navigate to lib folder. Copy the following Groovy jars and paste it into JMETER_HOME\lib folder.

Groovy 4.0 in JMeter 5.4.3
Groovy 4.0 in JMeter 5.4.3

Similarly, copy and paste the Jackson jars.

Jackson in JMeter 5.4.3
Jackson in JMeter 5.4.3

TOML Support in JMeter 5.4.3 using Groovy 4.0

The next step is to validate the TOML support in Groovy 4.0 in JMeter using JSR223 Sampler. Launch JMeter 5.4.3, create a new Thread Group and add a JSR223 Sampler.

TomlSlurper

TomlSlurper is equivalent to JsonSlurper. TomlSlurper helps to parse text or read TOML content and convert it into Groovy objects.

Below is the sample TOML file which we are going to parse it using TomlSlurper.

Copy and paste the below code and save it as *.toml.

# Sample TOML File
title = "TOML in JMeter"

[author]
name = "NaveenKumar Namachivayam"
blog = "https://qainsights.com"

[environment]
name = "stage"
enabled = true
ports = [ 8000, 8001, 8002 ]
workload = { rps = 80, thinktime = 5 }

[instance-a]
ip = "10.0.0.1"
role = "frontend"

[instance-b]
ip = "10.0.0.2"
role = "appserver"

[instance-c]
ip = "10.0.0.3"
role = "database"

In the JSR223 Sampler, copy and paste the below code.

import groovy.toml.TomlSlurper

// Reading and parsing TOML file
def tomlConfig = new File("C:\\Tools\\apache-jmeter-5.4.3\\bin\\sample.toml")
def ts = new TomlSlurper()
def rt = ts.parse(tomlConfig)

// Reading all the key, value pairs from TOML
for (def item in rt){
    log.info("TOML key, value: " + item.toString())
}

// Reading title, author, and blog from TOML
log.info("The Title is: " + rt.title.toString())
log.info("Author: " + rt.author.name.toString())
log.info("Blog: " + rt.author.blog.toString())

// Reading all the environment key, value pairs from TOML
for(def item in rt.environment){
    log.info("Environment: " + item.toString())
}

The first step is to verify the Groovy version in the JSR223 Sampler. If you are not getting Groovy 4.0 in Script Language as shown below, verify the Groovy and Jackson jars again.

Groovy 4.0 in JMeter 5.4.3
Groovy 4.0 in JMeter 5.4.3

Now, the next step is code walk through. To parse TOML, import TomlSlurper using import groovy.toml.TomlSlurper.

To parse the TOML file, map the TOML path and define a variable to use TomlSluper object. By using the parse method, it is possible to read the content.

The subsequent code is to read and print the various sections from the TOML file.

Click on the Run button and open Log Viewer to view the output.


2022-02-27 17:39:59,797 INFO o.a.j.p.j.s.J.JSR223 Sampler: TOML key, value: title=TOML in JMeter 2022-02-27 17:39:59,797 INFO o.a.j.p.j.s.J.JSR223 Sampler: TOML key, value: author={name=NaveenKumar Namachivayam, blog=https://qainsights.com} 2022-02-27 17:39:59,797 INFO o.a.j.p.j.s.J.JSR223 Sampler: TOML key, value: environment={name=stage, enabled=true, ports=[8000, 8001, 8002], workload={rps=80, thinktime=5}} 2022-02-27 17:39:59,797 INFO o.a.j.p.j.s.J.JSR223 Sampler: TOML key, value: instance-a={ip=10.0.0.1, role=frontend} 2022-02-27 17:39:59,797 INFO o.a.j.p.j.s.J.JSR223 Sampler: TOML key, value: instance-b={ip=10.0.0.2, role=appserver} 2022-02-27 17:39:59,798 INFO o.a.j.p.j.s.J.JSR223 Sampler: TOML key, value: instance-c={ip=10.0.0.3, role=database} 2022-02-27 17:39:59,799 INFO o.a.j.p.j.s.J.JSR223 Sampler: The Title is: TOML in JMeter 2022-02-27 17:39:59,801 INFO o.a.j.p.j.s.J.JSR223 Sampler: Author: NaveenKumar Namachivayam 2022-02-27 17:39:59,803 INFO o.a.j.p.j.s.J.JSR223 Sampler: Blog: https://qainsights.com 2022-02-27 17:39:59,807 INFO o.a.j.p.j.s.J.JSR223 Sampler: Environment: name=stage 2022-02-27 17:39:59,807 INFO o.a.j.p.j.s.J.JSR223 Sampler: Environment: enabled=true 2022-02-27 17:39:59,807 INFO o.a.j.p.j.s.J.JSR223 Sampler: Environment: ports=[8000, 8001, 8002] 2022-02-27 17:39:59,807 INFO o.a.j.p.j.s.J.JSR223 Sampler: Environment: workload={rps=80, thinktime=5}

TomlBuilder

TomlBuilder helps to build the TOML using the DSL.

Add another JSR223 Sampler in JMeter and add the below code.

import groovy.toml.TomlBuilder

// Building TOML record
def builder = new TomlBuilder()
builder.records {
    movie {
        name            'Valimai'
        year            2022
        actor           'Ajith Kumar'
        language        'Tamil'
        genre           'action', 'crime', 'thriller'
        imdb_rating     7.8
    }
}

// Creating toml file
def n = new File("output.toml")

// Reading key, value pairs from builder and writing to new.toml
for (def data in builder){
    log.info(data.toString())
    n.write(data.toString())
}

Run the test plan again to see the below output. In the current directory, output.toml will be generated.

records.movie.name = 'Valimai'
records.movie.year = 2022
records.movie.actor = 'Ajith Kumar'
records.movie.language = 'Tamil'
records.movie.genre = ['action', 'crime', 'thriller']
records.movie.imdb_rating = 7.8

By leveraging TomlBuilder() object, and by using the builder variable, it is easy to build the records.

To assert, you can use assert keyword to validate the TOML.

Closing Words

If your project heavily relies on TOML config format and you are using JMeter for performance testing and engineering, then Groovy 4.0 features will be helpful and a great time-saver.

Since TOML support is in incubation, do not implement it for production usage. But, you can be proactive and implement it in your local. JMeter 5.5 will be released soon. I hope Groovy 4.0 will be shipped.

Working with TOML in Groovy 4.0 in JMeter

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join the fastest growing Performance Engineers club at ClubhouseSHOW ME HOW
+ +
Share via
Copy link