【问题标题】:Groovy: best/performant way of loading dynamic scriptsGroovy:加载动态脚本的最佳/高性能方式
【发布时间】:2008-12-12 08:07:34
【问题描述】:

我正在实现一个程序,该程序利用 Groovy 作为脚本引擎,为用户提供了自动化任务的可能性。因此脚本必须按需加载,我使用以下代码实现:

// short version, try/catch and error handling skipped
String[] roots = new String[] { "data" };
Binding binding = new Binding();
GroovyScriptEngine gse = new GroovyScriptEngine(roots);

binding.setVariable("control", this.getControl());
// .. several others

gse.run(scriptName, binding); // where scriptName is provided through user selection

我注意到在第一次运行时整个块的执行需要 400 到 800 毫秒,但在我的笔记本上稍后运行时会下降到 200 到 400 毫秒。

一开始,每个事件都会初始化一个新的 GroovyScriptEngine,然后结束。与此同时,我为整个程序使用了一个 GSE 实例,尽管最初的问题尚未解决:

有没有办法预编译 groovy 脚本,即在 GSE 启动时? groovyc 不是一个选项,因为这需要“外部”程序调用。

非常感谢!

【问题讨论】:

    标签: performance groovy


    【解决方案1】:

    我正在查看同样的问题并且有以下问题:

    • 创建一个查看共享目录(在您的示例中为“数据”)的 GSE
    • 单个 GSE 监视并预编译目录中的项目,以便加快速度(不确定,但 GSE 似乎有一些在实例创建之间徘徊的静态内容。

    如果您的脚本没有太大变化,那么预编译它们是最好的方法,并且在注册更改时重新编译。

    希望对您有所帮助。

    【讨论】:

    • 除了使用 groovyc 之外,我找不到预编译 groovy 脚本的方法,但是如果 GroovyScriptEngine 仅作为 *.class 可用(至少不使用所描述的代码),则 GroovyScriptEngine 不会选择正确的脚本在最初的问题中)。还有其他方法可以达到同样的效果吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 2011-10-23
    • 2012-02-18
    • 1970-01-01
    • 2022-08-13
    • 2012-11-05
    • 1970-01-01
    相关资源
    最近更新 更多