@Test public void testLoadInitFileBenchmark () throws CallException, ScriptLoadException { JSRhinoScriptEngine engine = new JSRhinoScriptEngine(); long startTime = System.currentTimeMillis(); long startTimeNs = System.nanoTime(); engine.loadFile(new File(FilePath.parse("../data/init/scripts/init.js"))); long endTimeNs = System.nanoTime(); long endTime = System.currentTimeMillis(); long timeDiffNs = endTimeNs - startTimeNs; long timeDiff = endTime - startTime; System.err.println("execute init.js took " + timeDiff + "ms (" + timeDiffNs + "ns)."); for (int i = 0; i < 10; i++) { startTime = System.currentTimeMillis(); startTimeNs = System.nanoTime(); engine.loadFile(new File(FilePath.parse("../data/init/scripts/init.js"))); endTimeNs = System.nanoTime(); endTime = System.currentTimeMillis(); timeDiffNs = endTimeNs - startTimeNs; timeDiff = endTime - startTime; System.err.println("[" + i + "]: execute pre-compiled init.js took " + timeDiff + "ms (" + timeDiffNs + "ns)."); } ScriptEngine.cleanUp(); }
@Override public void loadFile(File file) throws ScriptLoadException { Objects.requireNonNull(file); if (!file.exists()) { throw new ScriptLoadException("js script file doesn't exists: " + file.getAbsolutePath()); } ExceptionUtils.throwSLEOnException(SCRIPTS_TAG, "Cannot read script file: " + file.getAbsolutePath() + ", IOException: ", () -> { File relFile = FileUtils.getRelativeFile(file, new File(".")); String scriptName = relFile.getPath().replace("\\", "/"); if (!this.scripts.containsKey(scriptName)) { String content = FileUtils.readFile(file.getAbsolutePath(), StandardCharsets.UTF_8); this.compile(scriptName, content); } this.execScript(scriptName); }); }
JSRhinoScriptEngine implements IScriptEngine { @Override public void loadFile(File file) throws ScriptLoadException { Objects.requireNonNull(file); if (!file.exists()) { throw new ScriptLoadException("js script file doesn't exists: " + file.getAbsolutePath()); } ExceptionUtils.throwSLEOnException(SCRIPTS_TAG, "Cannot read script file: " + file.getAbsolutePath() + ", IOException: ", () -> { File relFile = FileUtils.getRelativeFile(file, new File(".")); String scriptName = relFile.getPath().replace("\\", "/"); if (!this.scripts.containsKey(scriptName)) { String content = FileUtils.readFile(file.getAbsolutePath(), StandardCharsets.UTF_8); this.compile(scriptName, content); } this.execScript(scriptName); }); } }
JSRhinoScriptEngine implements IScriptEngine { @Override public void loadFile(File file) throws ScriptLoadException { Objects.requireNonNull(file); if (!file.exists()) { throw new ScriptLoadException("js script file doesn't exists: " + file.getAbsolutePath()); } ExceptionUtils.throwSLEOnException(SCRIPTS_TAG, "Cannot read script file: " + file.getAbsolutePath() + ", IOException: ", () -> { File relFile = FileUtils.getRelativeFile(file, new File(".")); String scriptName = relFile.getPath().replace("\\", "/"); if (!this.scripts.containsKey(scriptName)) { String content = FileUtils.readFile(file.getAbsolutePath(), StandardCharsets.UTF_8); this.compile(scriptName, content); } this.execScript(scriptName); }); } JSRhinoScriptEngine(); }
JSRhinoScriptEngine implements IScriptEngine { @Override public void loadFile(File file) throws ScriptLoadException { Objects.requireNonNull(file); if (!file.exists()) { throw new ScriptLoadException("js script file doesn't exists: " + file.getAbsolutePath()); } ExceptionUtils.throwSLEOnException(SCRIPTS_TAG, "Cannot read script file: " + file.getAbsolutePath() + ", IOException: ", () -> { File relFile = FileUtils.getRelativeFile(file, new File(".")); String scriptName = relFile.getPath().replace("\\", "/"); if (!this.scripts.containsKey(scriptName)) { String content = FileUtils.readFile(file.getAbsolutePath(), StandardCharsets.UTF_8); this.compile(scriptName, content); } this.execScript(scriptName); }); } JSRhinoScriptEngine(); @Override void compile(String scriptName, String programStr); @Override void loadFile(File file); @Override Object execFunc(String funcName, Object... args); @Override Object execFunc(String funcName); @Override Object execScript(String scriptName, Object... args); @Override void shutdown(); }
JSRhinoScriptEngine implements IScriptEngine { @Override public void loadFile(File file) throws ScriptLoadException { Objects.requireNonNull(file); if (!file.exists()) { throw new ScriptLoadException("js script file doesn't exists: " + file.getAbsolutePath()); } ExceptionUtils.throwSLEOnException(SCRIPTS_TAG, "Cannot read script file: " + file.getAbsolutePath() + ", IOException: ", () -> { File relFile = FileUtils.getRelativeFile(file, new File(".")); String scriptName = relFile.getPath().replace("\\", "/"); if (!this.scripts.containsKey(scriptName)) { String content = FileUtils.readFile(file.getAbsolutePath(), StandardCharsets.UTF_8); this.compile(scriptName, content); } this.execScript(scriptName); }); } JSRhinoScriptEngine(); @Override void compile(String scriptName, String programStr); @Override void loadFile(File file); @Override Object execFunc(String funcName, Object... args); @Override Object execFunc(String funcName); @Override Object execScript(String scriptName, Object... args); @Override void shutdown();  }