1

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 ?

Ori Marko
  • 56,308
  • 23
  • 131
  • 233

1 Answers1

2

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.");
    }
aristotll
  • 8,694
  • 6
  • 33
  • 53
  • Thank you. But now I get all script engine support compilation – Ori Marko Aug 16 '17 at 08:48
  • 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. – Ori Marko Aug 16 '17 at 09:08
  • @user7294900 Similar result in my computer. From the Nashorn code, and actually it implements `compile` method. So Javascript not compilable is not correct now. – aristotll Aug 16 '17 at 09:12