允许缓存和编译的 JMeter 脚本引擎 [英] JMeter Script Engine which allow caching and compilation

查看:16
本文介绍了允许缓存和编译的 JMeter 脚本引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSR223 Sampler 声明 Groovy 正在实现 Compilable界面不同于其他脚本语言,因此推荐

JSR223 Sampler have a statement that Groovy is implementing Compilable interface different than other scripting languages and therefore is recommended

为了从缓存和编译中受益,语言引擎用于脚本编写必须实现 JSR223 Compilable 接口(Groovy 是其中之一)这些,java、beanshell 和 javascript 不是)

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)

我尝试使用 JSR223 Sampler 中的 类似代码来检查它.我尝试使用 Compilable 检查所有可用语言:

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

我的结果是:

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.

含义支持编译,

编辑 1我根据@aristotll 更改检查,现在它返回所有支持编译

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) {

编辑 2

我根据@aristotll 第二次编辑而改变

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

我得到了有趣的结果:Nashorn 和 JEXL 支持它

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.

我检查错了吗?我需要导入更多 jars 才能启用它吗?如何知道脚本引擎是否使用缓存和编译?JMeter 的说法是错误的/过时的吗?

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 ?

推荐答案

您需要获得 ScriptEngine 实例而不是 ScriptEngineFactory

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

<小时>

为什么都是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.");
    }

这篇关于允许缓存和编译的 JMeter 脚本引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆