Performance issue for groovy scripts
Description
Environment
Attachments
- 30 Aug 2017, 08:09 AM
Activity
Luis Amesty February 15, 2018 at 2:08 PM
I have a Plugin using javascript engine.
When :
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine interprete = manager.getEngineByName("javascript");
interprete.put("CN", CND);
i've got a "java.lang.NullPointerException"
It worked fine before Patch.
How do you think i have to carry out this on my plugin ?
Regards Luis Amesty
ERP OpenSource Italia September 12, 2017 at 7:08 AM
This should work with bsh and jython, since they are provided by org.adempiere.base, but groovy is just a jar packed like a plugin afaik.
For the first two the problem is irrelevant: both the cache and the engine are in the same plugin.
An hypothesis could be:
create a plugin hosting script-related features (And the cache)
create a plugin for every script engine.
Regards,
Silvano
Hiep Lq September 8, 2017 at 1:55 PM
how about if do it like before
1. plugin provide script engine will fire a "install script engine" event when it it's deploy
2. every plugin provide script engine will listen "install script engine" event, if it have lower version it should clear cache by itself.
3. every plugin provide script engine have to remove it from cache when uninstall.
ERP OpenSource Italia September 8, 2017 at 1:42 PM
I have tried a different solution checking the classloader of the engine and using the cached one if they come from the same classloader, but it requires creating an instance of an engine and throwing it away in nearly all case. This should work based on the assumption that every plugin has a different classloader, so if the classloader is different, its a newly-deployed plugin.
In the end i opted for the first solution because i didnt like the idea of creating many isntances just to be thrown away to support an extremely rare situation.
ERP OpenSource Italia September 8, 2017 at 1:38 PM
The fact that the engines are cached: once one is cached, even if you deploy a new version of groovy plugin (for exampe) the engine is still cached so the old one is used. The new one will be ignored.
Groovy script are very slow due to the fact that every invocation resutl in a new script engine being created (overhead could be related to the initializazione of the engine). For example invoking a simple script callout required about 5 seconds,most of them before the first real instructions being invoked.
Caching the script engine removes that overhead, with no impact on other supported script engines (beanshell, jythons is not working anyway) and may be useful for future engines (javascript ?).
The proposed solution is aggressive in caching engines, with the side effect of making it impossibile to update the script engine while the server is running. I have opted for that solutions to reduce the remove the overhead of creating a engine just for checking purpose, and then throwing it away.