We use Rhino engine to evaluate javascript in our javaEE application.(We use javascript as parametrization. Passing data between DTO -s and parameterization of condition etc).
We make some performance test and the result is very strange.
The javascript engine is very-very slow. And the duration increasing exponentially.
I think about changing the whole engine to Nashron but before that i try to improve what we have now.
Some code snippets about the way we use it(maybe the problem is here (?))
Every time i try to evaluate an expression i create a JavaScriptEval objects this way:
public void init() {
Context cc = Context.getCurrentContext();
if ( cc == null) {
cc = Context.enter();
}
this.ctx = cc;
this.scope =this.ctx.initStandardObjects(null);
this.wrapFactory = new WrapFactory();
}
After that we add objects- for example DTO -s- to the scope
private void putObject(String id, Object obj) {
Context ctx = setupContext();
if (obj != null) {
Scriptable paramWrapper = this.wrapFactory.wrapAsJavaObject(/*this.ctx*/ ctx, this.scope, obj, obj.getClass());
this.scope.put(id, this.scope, paramWrapper);
}
}
After that we evaluate our expressions this way:
public Object eval(Object scriptId, String source){
Object ret;
ret = ctx.evaluateString(this.scope,source, scriptId.toString(), 1, null);
return ret;
}
I tried using some kind of caching. I used compileString method and then put it in a cache. But the memory usage was extremly high in that way.
After that i try to limit the length of javascript source that i put in the cache. But the problem was the same with increasing concurrent usage the performance was horible.
Any idea?
All suggestions are welcome