Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The compilation performance is poor in the case of high concurrency #149

Open
darkCity123 opened this issue Nov 26, 2023 · 3 comments
Open
Labels

Comments

@darkCity123
Copy link

darkCity123 commented Nov 26, 2023

I used jmeter to perform a performance pressure test on the sandbox. When the number of concurrent requests is 50, the compile execution time increases with the increase of time. The compile time can even reach 50s. Can you confirm whether the problem is caused by my use or a possible bug? Thank you very much!
The sandbox version is 0.3.0. The test code is as follows:

@RequestMapping(path = "/sync", method = RequestMethod.POST)
public BaseResponse createDataSource() throws ScriptException {
    String scriptContent = "function excute(data) {\r\n   var reqBody = {\r\n    \"isIncrement\":\"false\",\r\n    \"cursor\":1,\r\n    \"count\": 1\r\n    };\r\n    return reqBody;\r\n}";
    NashornSandbox nashornSandbox = createNashornSandbox();
    nashornSandbox.setMaxPreparedStatements(2000);
    long compileTime = System.currentTimeMillis();
    CompiledScript compiledScript = nashornSandbox.compile(scriptContent);
    long endCompileTime = System.currentTimeMillis();
    System.out.println("current compile time is: " + (endCompileTime - compileTime));

    long startEvalTime = System.currentTimeMillis();
    nashornSandbox.eval(compiledScript);
    long endEvalTime = System.currentTimeMillis();
    System.out.println("current eval time is: " + (endEvalTime - startEvalTime));
    BaseResponse response = new BaseResponse();
    response.setCode("1111");
    response.setMessage("current eval time is :" + (endEvalTime - startEvalTime) + "  compile time is: " + (endCompileTime - compileTime));
    return response;
}
@mxro
Copy link
Collaborator

mxro commented Dec 2, 2023

Hi! Thanks for raising this issue!

For reference, here is the code that gets executed when precompiling a script:

https://github.com/javadelight/delight-nashorn-sandbox/blob/master/src/main/java/delight/nashornsandbox/internal/NashornSandboxImpl.java#L512

Is the issue the same when calling eval directly with the script as string (e.g. not to precompile it)?

Could you try to wrap the execution in a FixedThreadPool or the like to limit concurrency - e.g. to 10 parallel executions - that may execute faster:

ExecutorService executorService = Executors.newFixedThreadPool(10);

@mxro mxro added the question label Dec 2, 2023
@darkCity123
Copy link
Author

Thank you for your reply! Actually, the problem I am facing now is that the sandbox compile method is very slow under high concurrency. I tried to analyze it further and found that the beautifyJs(final String js) method is very time-consuming when there are 100 concurrent requests. I am still trying to analyze it, but I have not made any more progress.

@mxro
Copy link
Collaborator

mxro commented Dec 9, 2023

Did you try limiting concurrency with the fixed thread pool?

Is the problem one of overall load or just concurrency? If the latter is the case, using the fixed thread pool could be an option.

Maybe the web server you use also has a possibility to limit concurrent requests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants