user7294900 user7294900 - 1 year ago 121
Groovy Question

JMeter Script Engine which allow caching and compilation

JSR223 Sampler have a statement that Groovy is implementing

Compilable interface
different than other scripting languages and therefore is recommended


To benefit from caching and compilation, the language engine used for
scripting must implement JSR223 Compilable interface (Groovy is one of
these, java, beanshell and javascript are not)


I tried to check it using similar code in JSR223 Sampler.
I tried to check all available languages with Compilable:

import javax.script.Compilable;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
ScriptEngineManager mgr = new ScriptEngineManager();
engineFactories = mgr.getEngineFactories();
for (ScriptEngineFactory engineFactory : engineFactories) {

if (engineFactory instanceof Compilable) {
log.info(engineFactory.getEngineName() + " Script compilation is supported.");
} else {
log.info(engineFactory.getEngineName() + " Script compilation is not supported.");
}
}


My result is:

Oracle Nashorn Script compilation is not supported.
JEXL Engine Script compilation is not supported.
Groovy Scripting Engine Script compilation is not supported.
JEXL Engine Script compilation is not supported.
Velocity Script compilation is not supported.
BeanShell Engine Script compilation is not supported.


Meaning none support compilation,

EDIT 1
I change according to @aristotll the check and now it returns that all support compilation

final ScriptEngine engine = engineFactory.getScriptEngine();
if (engine instanceof Compilable) {


EDIT 2

I change according to @aristotll second edit

try {
((Compilable) engine).compile("");
log.info(engineFactory.getEngineName() + " Script compilation is supported.");
} catch (Error e) {
log.info(engineFactory.getEngineName() + " Script compilation is not supported.");


I'm getting interesting result: Nashorn and JEXL support it

Groovy Scripting Engine Script compilation is supported.
Oracle Nashorn Script compilation is supported.
JEXL Engine Script compilation is supported.
BeanShell Engine Script compilation is not supported.
JEXL Engine Script compilation is supported.


Am I checking something wrong? Do I need to import more jars to enable it?
How can I know if scripting engine uses caching and compilation? is JMeter's statement is wrong/outdated ?

Answer Source

You need to get ScriptEngine instance instead of ScriptEngineFactory

final ScriptEngine engine = engineFactory.getScriptEngine();
if (engine instanceof Compilable) {
...

Why all Compilable? Because these script engines may be compilable in the future. But currently not support, so they all implement this interface. You may try to compile the empty string:

  if (engine instanceof Compilable) {
        try {
            ((Compilable) engine).compile("");
        } catch (Error e) {
            System.out.println(engineName + " Script compilation is not supported.");
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        System.out.println(engineName + " Script compilation is supported.");
    } else {
        System.out.println(engineName + " Script compilation is not supported.");
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download