Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

java - 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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.");
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...