why I can save a groovy closure to JMeter's exposed props Hashtable but not a groovy function?

190 views Asked by At

Question

Anyone know why I can save a groovy closure to JMeter's exposed props Hashtable but not a groovy function?

Minimal example

In a test plan, in a thread group, in a JSR223 sampler with script language Groovy 3.0.3, I have the following minimal failing example (fails both when pasted into the "script" box in the Jmeter GUI or referenced from the file name "test.groovy"):

def bern = { p -> Math.random() < p }

props.put("bern", bern)
closure = props.get("bern")

log.warn("props closure call: " + closure(1.0))

def bernoulli (double p) {
    Math.random() < p
}

props.put("bernoulli", bernoulli)
funct = props.get(bernoulli)

log.warn("props function call: " + funct(1.0))

Here's my expected output (in the JMeter log viewer)

props closure call: true
props function call: true

Here's the log output I'm getting

props closure call: true
...text
Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptExecution: 
groovy.lang.MissingPropertyException: No such property bernoulli for class:
 Script<jmeter generated groovy script number> ...

Related

1

There are 1 answers

2
daggett On BEST ANSWER

You could store a function into props with the following syntax:

// def props = [:]
def bernoulli (double p) {
    Math.random() < p
}

props.put("bernoulli", this.&bernoulli)

funct = props.get("bernoulli")
println funct(1.0)

The expression this.&bernoulli just converts the bernoulli function to a closure using the method pointer operator .&.