This performance is unacceptable in the production environment, and there is no thread isolation. #120

Closed
opened 2024-11-28 04:15:45 +00:00 by p-moon · 0 comments
p-moon commented 2024-11-28 04:15:45 +00:00 (Migrated from github.com)

public class LuaEvalTest {


    private ScriptEngineManager scriptEngineManager = new ScriptEngineManager();

    @Test
    public void testScriptEngine() throws InterruptedException, ScriptException {
        DispatchContext dispatchContext = new DispatchContext(){};
        dispatchContext.setName("jack");
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("luaj");
        stopWatch.stop();
        log.info("getEngineByName cost {}ms", stopWatch.getTotalTimeMillis());

        String script = "result = dispatchContext:getName() == 'jack'";


        scriptEngine.put("dispatchContext", dispatchContext);
//        stopWatch = new StopWatch();
//        stopWatch.start();
//        // 大约 30毫秒
//        scriptEngine.eval(script);
//        scriptEngine.get("result");
//        stopWatch.stop();
//        log.info("scriptEngine eval cost:{}", stopWatch.getTotalTimeMillis());

        ScriptContext context = scriptEngine.getContext();
//        context.removeAttribute()

        // 试试编译后的
        stopWatch = new StopWatch();
        stopWatch.start();
        CompiledScript compiledScript = ((Compilable) scriptEngine).compile(script);
        stopWatch.stop();
        log.info("compiledScript compile script cost {}ms", stopWatch.getTotalTimeMillis());

        stopWatch = new StopWatch();
        stopWatch.start();
        compiledScript.eval(scriptEngine.getContext());
        stopWatch.stop();

        log.info("compiledScript eval cost:{}", stopWatch.getTotalTimeMillis());

        scriptEngine.getContext().setReader(null);
        scriptEngine.getContext().setWriter(null);
        scriptEngine.getContext().setErrorWriter(null);
        scriptEngine.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
        scriptEngine.setBindings(new SimpleBindings(), ScriptContext.GLOBAL_SCOPE);
        log.info("finished");

    }
}

result:

11:39:44.153 [main] INFO plus.jdk.smart.global.LuaEvalTest - getEngineByName cost 135ms
11:39:44.179 [main] INFO plus.jdk.smart.global.LuaEvalTest - compiledScript compile script cost 18ms
11:39:44.194 [main] INFO plus.jdk.smart.global.LuaEvalTest - compiledScript eval cost:15
11:39:47.692 [main] INFO plus.jdk.smart.global.LuaEvalTest - finished
```java public class LuaEvalTest { private ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); @Test public void testScriptEngine() throws InterruptedException, ScriptException { DispatchContext dispatchContext = new DispatchContext(){}; dispatchContext.setName("jack"); StopWatch stopWatch = new StopWatch(); stopWatch.start(); ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("luaj"); stopWatch.stop(); log.info("getEngineByName cost {}ms", stopWatch.getTotalTimeMillis()); String script = "result = dispatchContext:getName() == 'jack'"; scriptEngine.put("dispatchContext", dispatchContext); // stopWatch = new StopWatch(); // stopWatch.start(); // // 大约 30毫秒 // scriptEngine.eval(script); // scriptEngine.get("result"); // stopWatch.stop(); // log.info("scriptEngine eval cost:{}", stopWatch.getTotalTimeMillis()); ScriptContext context = scriptEngine.getContext(); // context.removeAttribute() // 试试编译后的 stopWatch = new StopWatch(); stopWatch.start(); CompiledScript compiledScript = ((Compilable) scriptEngine).compile(script); stopWatch.stop(); log.info("compiledScript compile script cost {}ms", stopWatch.getTotalTimeMillis()); stopWatch = new StopWatch(); stopWatch.start(); compiledScript.eval(scriptEngine.getContext()); stopWatch.stop(); log.info("compiledScript eval cost:{}", stopWatch.getTotalTimeMillis()); scriptEngine.getContext().setReader(null); scriptEngine.getContext().setWriter(null); scriptEngine.getContext().setErrorWriter(null); scriptEngine.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); scriptEngine.setBindings(new SimpleBindings(), ScriptContext.GLOBAL_SCOPE); log.info("finished"); } } ``` result: ``` 11:39:44.153 [main] INFO plus.jdk.smart.global.LuaEvalTest - getEngineByName cost 135ms 11:39:44.179 [main] INFO plus.jdk.smart.global.LuaEvalTest - compiledScript compile script cost 18ms 11:39:44.194 [main] INFO plus.jdk.smart.global.LuaEvalTest - compiledScript eval cost:15 11:39:47.692 [main] INFO plus.jdk.smart.global.LuaEvalTest - finished ```
open-autonomous-connection started working 2026-03-01 18:20:43 +00:00
open-autonomous-connection worked for 17 seconds 2026-03-01 18:21:00 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Total Time Spent: 17 seconds
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: open-autonomous-connection/luaj#120