سؤال

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

هل كانت مفيدة؟

المحلول

I think there isn't good answer for that :)

But i solved the problem: I precompiled the expressions and if it was a simple init(something like dto.setA(1)) just execute it in java code.

Another way was to use separated cache for separated process and then delete it.

Its a very specifical way to resolve this problem but i works for me :)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top