1
votes

I use groovy in my Jmeter BSF, and sometimes I have functions that are used frequently enough to be moved to some script which I than can use as a library. My approach was to create a file, say "library.groovy", and add there some function

public void function()
{
    println("hello!");
}

and then use the following code in my BSF script

import library.groovy;
function();

Both files lie in the same dir, but script refuses to locate library. I also tried to explicitly wrap this function into class but I took no effect as well.

Can anyone suggest a solution for this?

Update: I tried almost all possible solutions described in the internet. And everything that works in groovy console or Eclipse does not in Jmeter. Probably that is because of BSF. Anyone knows some workarounds?

3

3 Answers

3
votes

I just had this problem, and solved it in a way that seems, to me, nicer-looking. It is basically winstaan74's answer, but with the extra bits needed to make it work.

You have your function's groovy file, named say: MyJmeterFunctions.groovy:

package My.JmeterFunctions

public class MyHelloClass   {
    public void hello() {
        println("Hello!");
    }
}

Then you compile this from the terminal:

$groovyc -d myJmeterFunctions myJmeterFunctions.groovy

and turn it into a .jar inside the /lib folder of your jmeter install, with all the other .jar files that came with jmeter

$jar cvf /<>/apache-jmeter-2.8/lib/myJmeterFunctions.jar -C myJmeterFunctions .

Now, restart jmeter. It won't know about your new .jar until you do.

Lastly you have the script that you want to run the hello() function from, which your jmeter BSF assertion/listener/ whatever points to:

import My.JmeterFunctions.*

def my_hello_class_instance = new MyHelloClass();
my_hello_class_instance.hello();

And this is what worked for me. If you'd rather organize you .jar into a different folder than jmeter's /lib, I believe you can run jmeter using (from here): $jmeter -Jsearch_paths=/path/to/yourfunction.jar

But I haven't tried that myself.

1
votes

I ended up having 2 files like below: "MyHelloClass.groovy"

public class MyHelloClass   {
    public void hello() {
        println("Hello!");
    }
}

And "HelloScript.groovy"

try {
    ClassLoader parent = getClass().getClassLoader();
    GroovyClassLoader loader = new GroovyClassLoader(parent);
    Class groovyClass = loader.parseClass(new File("../GroovyScripts/MyHelloClass.groovy"));
    GroovyObject helloClass = (GroovyObject) groovyClass.newInstance();
    helloClass.hello();
}
catch (Throwable e) {
    println(e.toString());
}

Then I can run "HelloScript.groovy" in BSF from Jmeter.

0
votes

I think you'll need to wrap your helper methods in a class, and then import that class. So, your helper methods file should contain..

package library

class UsefulFunctions {
   static function() {
        println 'hello'
    }
}

And then in your test script, say

import static library.UsefulFunctions.*

function()

Now, this is just scratching the surface, but I hope it'd be enough to get you started.