Slow execution of inline groovy scripts

92 views
Skip to first unread message

Abre Chase

unread,
Mar 5, 2019, 1:07:12 PM3/5/19
to CAS Community
We have a service setup that prepends the user name with a numerical code.  We do this with an inline groovy script:

  "usernameAttributeProvider" : {
    "@class" : "org.apereo.cas.services.GroovyRegisteredServiceUsernameProvider",
    "groovyScript" : "groovy { return '20115-'+attributes['cn'][0] }"
  },

However, logins to this service are very slow and it appears that a lot of time is spend executing the groovy script.  From the debug logs, there is an almost 7 second gap between executing the script and producing the value:

2019-03-05 11:39:22,003 DEBUG [org.apereo.cas.services.GroovyRegisteredServiceUsernameProvider] - <Found groovy script to execute [groovy { return '20115-'+attributes['cn'][0] }]>
2019-03-05 11:39:22,003 DEBUG [org.apereo.cas.util.ScriptingUtils] - <Executing groovy script [return '20115-'+attributes['cn'][0] ] with variables [{attributes={cn=[admin], givenName=[Admin], mail=[admin@host.local], sn=[Admininstrator]}, id=admin@host.local, logger=org.apache.logging.slf4j.Log4jLogger@6534b155}]>
2019-03-05 11:39:28,706 DEBUG [org.apereo.cas.services.GroovyRegisteredServiceUsernameProvider] - <Found username [20115-admin] from script [groovy { return '20115-'+attributes['cn'][0] }]>
2019-03-05 11:39:28,706 DEBUG [org.apereo.cas.services.BaseRegisteredServiceUsernameAttributeProvider] - <Resolved username for [https://localhost] is [20115-admin]>

It looks like the slow part is inside ScriptingUtils.executeGroovyShellScript, most likely shell.evaluate.

public static <T> T executeGroovyShellScript(final String script,
                                                 final Map<String, Object> variables,
                                                 final Class<T> clazz) {
        try {
            final Binding binding = new Binding();
            final GroovyShell shell = new GroovyShell(binding);
            if (variables != null && !variables.isEmpty()) {
                variables.forEach(binding::setVariable);
            }
            if (!binding.hasVariable("logger")) {
                binding.setVariable("logger", LOGGER);
            }
            LOGGER.debug("Executing groovy script [{}] with variables [{}]", script, binding.getVariables());

            final Object result = shell.evaluate(script);
            if (result != null && !clazz.isAssignableFrom(result.getClass())) {
                throw new ClassCastException("Result [" + result
                    + " is of type " + result.getClass()
                    + " when we were expecting " + clazz);
            }
            return (T) result;

        } catch (final Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return null;
    }

I'm just curious if anyone else has run into slowness when executing inline scripts and if so, is there any way to speed it up short of modifying source code.

Abre

Abre Chase

unread,
Mar 5, 2019, 3:59:43 PM3/5/19
to CAS Community

I ran a single login through a profiler and it confirms that most of the time is spent evaluating the script.  That said, most of the time in evaluating is in class loading.  I'm still not sure how to speed this up but getting closer to identifying the root problem.


Screen Shot 2019-03-05 at 3.56.42 PM.png

Tepe, Dirk

unread,
Mar 5, 2019, 9:30:18 PM3/5/19
to cas-...@apereo.org
What version of CAS are you using? We arranged a fix to address groovy script loading in the 5.3.7 release. I suspect you are experiencing the same problem we observed.

-dirk

--
- Website: https://apereo.github.io/cas
- Gitter Chatroom: https://gitter.im/apereo/cas
- List Guidelines: https://goo.gl/1VRrw7
- Contributions: https://goo.gl/mh7qDG
---
You received this message because you are subscribed to the Google Groups "CAS Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cas-user+u...@apereo.org.
To view this discussion on the web visit https://groups.google.com/a/apereo.org/d/msgid/cas-user/8b9ff5cf-16bd-4345-be12-5b1a6a6688f7%40apereo.org.

Tepe, Dirk

unread,
Mar 5, 2019, 9:31:08 PM3/5/19
to cas-...@apereo.org
Sorry, I should clarify that we only arranged for the fix in the loading the MFA trigger script. The same solution could be applied to other uses of groovy scripts, but it is not global.

-dirk

Abre Chase

unread,
Mar 5, 2019, 9:44:51 PM3/5/19
to cas-...@apereo.org
Thanks for that tip.  I see the code commit for that change here:


That looks like it might solve the problem.  I'm on 5.3.2, so I'll try upgrading to see how it impacts the performance of the inline script.

Abre

You received this message because you are subscribed to a topic in the Google Groups "CAS Community" group.
To unsubscribe from this topic, visit https://groups.google.com/a/apereo.org/d/topic/cas-user/iNm7khTNLxo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cas-user+u...@apereo.org.
To view this discussion on the web visit https://groups.google.com/a/apereo.org/d/msgid/cas-user/CAJ%3D0EZzabP%3DYJHghCaDAzvuPu1inC%2BKMQnFm3thNiWaYCqzvVw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages