-2

We are executing very simple Javascripts using Java for certain validation. The Javascript engine is Nashorn. Using a single thread and max memory of 1GB, we were able to run 40,000 script executions per minute. We can achieve more speed by running multiple threads but the target machines where the program will eventually be running may not be multi-core machines.

As suggested, posting snippet of the code.

public class RuleExecutor {

private ScriptEngineManager manager;
private ScriptEngine engine;

public RuleExecutor() {
    this.manager = new ScriptEngineManager();
    this.engine = manager.getEngineByName("JavaScript");
}

public Object execute(String[] inputRow, Rule rule) {
    Object output = new String("");
    Bindings bindings = engine.createBindings(); 
    //code to bind the variables
    ...
    ......      
    bindings.put(.....)
    engine.eval(rule.getExpr(), bindings);
    output = bindings.get(rule.getTarget().trim());

    return output;
} 
}

Only one instance of the above class exists throughout the program. The method execute() is called multiple times.

Used JDK 8 (build 1.8.0_101-b13)

How to speed it up?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sourajit Basak
  • 693
  • 5
  • 15
  • There's no way to answer this question, as written. It either degenerates into "how to generally optimize things" (and we don't even know the basics, like whether the code is heavy terms of memory, raw processor speed or IO) or will give you random advice (which might help or might make things worse). – fdreger Dec 07 '18 at 09:15

1 Answers1

2

Use a profiler e.g. FlightRecorder or VisuamVM or YourKit or JProfiler, to find out where it is spending the most time, and optimise it.

Convert the expensive bits into Java. e.g. if a piece of code is inefficient in Javascript, write it in Java instead and call it from JS.

Use a decent machine for the task you need. Sometimes hardware is much cheaper than developer time.

These days, even the cheapest mobile phones have multiple cores. Some embedded devices only have 1 core, but these usually can contact a server via blue tooth or wifi to do the expensive processing.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I added a comment to the main question. Your only question-specific (Nashorn / Java) advice could degrade the performance as well as help it. Which makes it non-useful for people who come here looking for Nashorn advice. – fdreger Dec 07 '18 at 09:18
  • @fdreger should we downgrade to JDK 7. Ref https://stackoverflow.com/questions/33596023/major-performance-issues-with-java-8-scriptengine-compared-to-java-7 – Sourajit Basak Dec 07 '18 at 14:20
  • @SourajitBasak: just do random things - change settings, downgrade, upgrade, modify your script .Maybe something helps. _Unless_ you make the question more precise, there is absolutely no way to help you in a non-random manner. – fdreger Dec 07 '18 at 14:26
  • @SourajitBasak (which is why I downvoted it and voted for closing) – fdreger Dec 07 '18 at 14:27
  • @PeterLawrey Can i have your email? – Pie Dec 08 '18 at 05:39
  • @Pie the best free place to ask questions is on StackOverflow. – Peter Lawrey Dec 09 '18 at 12:36