Profiling web requests in your Grails application

| Comments

Here’s a simple filter I’ve been using to help me detect points of improvement in my application:

class UtilFilters {

    def filters = {

        profiler(controller: '*', action: '*') {
            before = {
                request._timeBeforeRequest = System.currentTimeMillis()
            }

            after = {
                request._timeAfterRequest = System.currentTimeMillis()
            }

            afterView = {
                if (params.showTime) {
                    session._showTime = params.showTime == "on"
                }
                if (session._showTime) {
                    def actionDuration = request._timeAfterRequest - request._timeBeforeRequest
                    def viewDuration = System.currentTimeMillis() - request._timeAfterRequest
                    log.debug("Request duration for (${controllerName}/${actionName}): ${actionDuration}ms/${viewDuration}ms")
                }
            }
        }
    }

}

To use it, put this class in the grails-app/conf folder of your project. To activate the profile, call any URL of your application with the showTime=on parameter, like this:

1
http://localhost:8080/my-grails-app/book/list?showTime=on

After calling that URL, all request times will be measured and informed on the application’s log, like this:

1
2010-12-21 12:02:31,698 [http-8080-5] DEBUG filters.UtilFilters  - Request duration for (book/list): 20ms/50ms

The first time informed (20ms) is the time spent executing the action (list in this case) and the second (50ms) is the time spent rendering the view (list.gsp).

To turn off the profiler, call any URL with showTime=off:

1
http://localhost:8080/my-grails-app?showTime=on

Enjoy :)

Comments