之前的项目里面大量使用了Nashorn引擎,目的是很多需要动态执行的代码放到了JavaScript里面,这样在用户那边比较好调试。但是因为性能的问题遇到了几个坑,最后经过一段时间,最终解决了,形成了这篇文章。
使用预编译JS来加速
在实际的应用中,基本上JS是不变的,变的只是调用的参数。于是乎可以通过预编辑,来进行加速。这样可以省略每次编译的耗时。代码如下。
String script = " var x = a + 1; " +
" var y = x * 2 + m; " +
" var z = y * 3 - n; " +
" z;" +
final CompiledScript compiled = ((Compilable)scriptEngine).compile(script);
调用的时候,可以直接
try {
Bindings bindings = new SimpleBindings();
bindings.put("a", v);
bindings.put("m", v + 1);
bindings.put("n", v + 2) ;
o = (Double)compiled.eval(bindings);
} catch (ScriptException e) {
e.printStackTrace();
}
预编辑JS遇到的问题
这样的写法是没有问题的,但是在压力比较大的时候,CPU比较高,还是比较慢。经过跟踪,发现大量的时间浪费在JDK初始化Nashorn的执行上下文了。
经过查看JDK的源代码,Nashorn里面其实提供了共享上下文的方法。修改代码为:
NashornScriptEngineFactory factory = null;
for (ScriptEngineFactory f : sm.getEngineFactories()) {
if (f.getEngineName().equalsIgnoreCase("Oracle Nashorn")) {
factory = (NashornScriptEngineFactory)f;
break;
}
}
String[] stringArray = new String[]{"-doe", "--global-per-engine"};
ScriptEngine scriptEngine = factory.getScriptEngine(stringArray);
final CompiledScript compiled = ((Compilable)scriptEngine).compile(script);
主要的是初始化参数中的--global-per-engine这个参数。
但是这样写,在并发执行的时候,会遇到问题。测试代码如下:
public class TestScript1 {
static ExecutorService pool = Executors.newCachedThreadPool();
static String script = " var x = a + 1; " +
" var y = x * 2 + m; " +
" var z = y * 3 - n; " +
" z;";
public static void main(String[] argu) throws ScriptException {
ScriptEngineManager sm = new ScriptEngineManager();
NashornScriptEngineFactory factory = null;
for (ScriptEngineFactory f : sm.getEngineFactories()) {
if (f.getEngineName().equalsIgnoreCase("Oracle Nashorn")) {
factory = (NashornScriptEngineFactory)f;
break;
}
}
String[] stringArray = new String[]{"-doe", "--global-per-engine"};
ScriptEngine scriptEngine = factory.getScriptEngine(stringArray);
final CompiledScript compiled = ((Compilable)scriptEngine).compile(script);
long l0 = System.currentTimeMillis();
AtomicInteger counter = new AtomicInteger();
for (int i=0; i<1024; i++) {
final int v = i;
pool.submit(() -> {
Bindings bindings = new SimpleBindings();
bindings.put("a", v);
bindings.put("m", v + 1);
bindings.put("n", v + 2) ;
int expected = getExpected(v, v + 1, v + 2);
Double o = null;
try {
o = (Double)compiled.eval(bindings);
if (o.intValue() == (expected)) {
// System.out.println("OK " + o);
counter.incrementAndGet();
} else {
System.err.println("FAILED!!!" + o + " " + (v + 1));
}
} catch (ScriptException e) {
e.printStackTrace();
}
});
}
pool.shutdown();
try {
boolean r = pool.awaitTermination(5, TimeUnit.SECONDS);
System.out.println("Terminated " + r);
} catch (InterruptedException e) {
e.printStackTrace();
}
long l1 = System.currentTimeMillis();
System.out.println(l1 - l0 + " ms");
System.out.println("Total OK = " + counter.intValue());
}
private static int getExpected(int a, int m, int n) {
int x = a + 1;
int y = x * 2 + m;
int z = y * 3 - n;
return z;
}
}
在上面的代码中,同时启动1024个线程进行模拟计算,同时和期望的值进行比较(一个JS版本,一个Java版本)。最后发现结果大部分都错了。为什么?
原因是,测试的JS是上下文相关的。
观察下JS,其中有三个上下文中的变量:a, m, n.
var x = a + 1;
var y = x * 2 + m;
var z = y * 3 - n;
这三个变量是通过Java代码中的
Bindings bindings = new SimpleBindings();
bindings.put("a", v);
bindings.put("m", v + 1);
bindings.put("n", v + 2) ;
来设置的。那么自然的,多线程执行的时候,后面的调用就有可能覆盖前面的调用。所以JS在执行的过程中,值乱套了。
解决并发的问题
经过尝试,上面并发的问题可以用下面的方式来解决。
首先,将值包装在一个参数对象中。
Map<String, Object> parameters = new HashMap<>();
parameters.put("b", v);
parameters.put("c", v + 1);
parameters.put("d", v + 2) ;
bindings.put("parameters", parameters);
其次,也是最重要的,要保证JS的代码是上下文无关代码(只和调用参数相关,和全局变量无关)
static String script = "function xx(a, m, n) { " +
" var x = a + 1; " +
" var y = x * 2 + m; " +
" var z = y * 3 - n; " +
" return z;" +
"} " +
"xx(parameters.b, parameters.c, parameters.d);";
如上,这段Script的结果只和调用时传入的参数有关,和其它的无关。
整个测试程序如下:
public class TestPoolledScript {
static ExecutorService pool = Executors.newCachedThreadPool();
static String script = "function xx(a, m, n) { " +
" var x = a + 1; java.lang.Thread.sleep(2); " +
" var y = x * 2 + m; java.lang.Thread.sleep(3);" +
" var z = y * 3 - n; java.lang.Thread.sleep(1);" +
" return z;" +
"} " +
"xx(parameters.b, parameters.c, parameters.d);";
public static void main(String[] argu) throws ScriptException {
ScriptEngineManager sm = new ScriptEngineManager();
NashornScriptEngineFactory factory = null;
for (ScriptEngineFactory f : sm.getEngineFactories()) {
if (f.getEngineName().equalsIgnoreCase("Oracle Nashorn")) {
factory = (NashornScriptEngineFactory)f;
break;
}
}
String[] stringArray = new String[]{"-doe", "--global-per-engine"};
ScriptEngine scriptEngine = factory.getScriptEngine(stringArray);
final CompiledScript compiled = ((Compilable)scriptEngine).compile(script);
long l0 = System.currentTimeMillis();
AtomicInteger counter = new AtomicInteger();
for (int i=0; i<1024; i++) {
final int v = i;
pool.submit(() -> {
Bindings bindings = new SimpleBindings();
Map<String, Object> parameters = new HashMap<>();
parameters.put("b", v);
parameters.put("c", v + 1);
parameters.put("d", v + 2) ;
bindings.put("parameters", parameters);
int expected = getExpected(v, v + 1, v + 2);
Double o = null;
try {
o = (Double)compiled.eval(bindings);
if (o.intValue() == (expected)) {
// System.out.println("OK " + o);
counter.incrementAndGet();
} else {
System.err.println("FAILED!!!" + o + " " + (v + 1));
}
} catch (ScriptException e) {
e.printStackTrace();
}
});
}
pool.shutdown();
try {
boolean r = pool.awaitTermination(5, TimeUnit.SECONDS);
System.out.println("Terminated " + r);
} catch (InterruptedException e) {
e.printStackTrace();
}
long l1 = System.currentTimeMillis();
System.out.println(l1 - l0 + " ms");
System.out.println("Total OK = " + counter.intValue());
}
private static int getExpected(int a, int m, int n) {
int x = a + 1;
int y = x * 2 + m;
int z = y * 3 - n;
return z;
}
}
这样的结果是完全正确的。
在我的机器上执行效率是之前的写法(没有加--global-per-engine)的四到五倍。
没有加--global-per-engine 1024个线程并发大概是2秒钟。
加上之后大概是500毫秒。
总结
最后,我们可以通过下面的方式来提高Nashorn执行JS的性能:
- 使用预编译;
- 使用--global-per-engine参数,同时修改JS代码,做到上下文无关。