Praveen Nayak
Praveen Nayak

Reputation: 197

JMeter JSR223 script - how do we load multiple groovy files?

I want to structure my code well to use in JMeter, so I plan to have the groovy scripts in separate groovy files. The JSR223 script can load the script file and execute it. So far so good.

The groovy scripts will get complex, so I want to split the files into multiple files and use them. For e.g. have a file utils.groovy with content

public class SharedStore {
    // Initializes the store to store all information about all the data being worked upon
    def initializeStore() {
        HashMap allStore = new HashMap();
        // def props = org.apache.jmeter.util.JMeterUtils.getJMeterProperties()
        props.put("${storeHashSet}", allStore);
    }

This class will grow with more methods and will be used by all scripts invoked from JMeter. There will be other similar classes.

One JMeter sampler called InitializeEnvironment.groovy wants to use this method thus:

evaluate(new File("<<path>>/Utils.groovy"));  // This line is an attempt to include utils.groovy
var shr = new SharedStore().initializeStore();

The above attempt to include groovy file is based on this discussion - https://stackoverflow.com/a/9154553 (and I have tried other options from that discussion to no avail)

Using the above option throws the error:

Script70.groovy: 2: unable to resolve class SharedStore
 @ line 2, column 11.
   var shr = new SharedStore().initializeStore();

I would prefer to wrap things utility methods a class but I can live with having keep methods in global space. If I do manage to load the additional file like in this - https://stackoverflow.com/a/15904699 - suggestion, without using "class" and have groovy wrap it up in a class for me, props are not available in utils.groovy unless I include the line def props = org.apache.jmeter.util.JMeterUtils.getJMeterProperties() and even then ${storeHashSet} cannot be used

No such property: storeHashSet for class: SharedStore

I want to be able to decompose the scripts into more manageable files, and be able to access JMeter variables and structures in these files.

Any advise on how I can do that in JMeter?

Upvotes: 1

Views: 1118

Answers (1)

Dmitri T
Dmitri T

Reputation: 168147

The most straightforward solution would be:

  1. Compiling these individual files into classes
  2. Packaging them into .jar (this one you can skip)
  3. Putting the .jar or (class files if you skipped step 2) in the JMeter Classpath
  4. Using the .jar file in your JSR223 Test Elements

Upvotes: 2

Related Questions