JMeter脚本引擎,允许缓存和编译 [英] JMeter Script Engine which allow caching and compilation
问题描述
JSR223 Sampler 有一个声明,Groovy正在执行 Compilable interface
与其他脚本语言不同,因此建议使用
$ b
为了从缓存和编译中受益,用于
脚本的语言引擎必须实现JSR223 Compilable接口(Groovy是
中的一个,java,beanshell和javascript不是)
我试图使用类似的代码进行检查在JSR223采样器。
我尝试使用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()+支持脚本编译。
} else {
log.info(engineFactory.getEngineName()+不支持脚本编译);
}
}
我的结果是:
不支持Oracle Nashorn脚本编译。
不支持JEXL引擎脚本编译。
Groovy脚本引擎不支持脚本编译。
不支持JEXL引擎脚本编译。
不支持Velocity Script编译。
不支持BeanShell引擎脚本编译。
含义 none 支持编译,
编辑1
我根据@aristotll检查进行更改,现在它返回全部支持编译
final ScriptEngine engine = engineFactory.getScriptEngine();
if(engine instanceof Compilable){
编辑2
我根据@aristotll第二次修改进行更改
try {
((Compilable)引擎).compile();
log.info(engineFactory.getEngineName()+支持脚本编译。);
} catch(Error e){
log.info(engineFactory.getEngineName()+不支持脚本编译);
我得到有趣的结果:Nashorn和JEXL支持它
支持Groovy脚本引擎脚本编译。
支持Oracle Nashorn脚本编译。
支持JEXL引擎脚本编译。
不支持BeanShell引擎脚本编译。
支持JEXL引擎脚本编译。
我检查错了吗?我是否需要导入更多的罐子来启用它?
如何知道脚本引擎是否使用缓存和编译?是JMeter的声明是错误的/过时的?
您需要获取 ScriptEngine
实例而不是 ScriptEngineFactory
最终ScriptEngine engine = engineFactory.getScriptEngine();
if(engine instanceof Compilable){
...
为什么所有 Compilable
?因为这些脚本引擎将来可能会编译。但目前不支持,所以他们都实现了这个接口。
您可以尝试编译空字符串:
if(引擎instanceof Compilable){
try {
((Compilable)引擎).compile();
} catch(Error e){
System.out.println(engineName +Script compilation is not supported。);
} catch(ScriptException e){
e.printStackTrace();
}
System.out.println(engineName +支持脚本编译);
} else {
System.out.println(engineName +不支持脚本编译。);
}
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 ?
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.");
}
这篇关于JMeter脚本引擎,允许缓存和编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!