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
|
|
After calling that URL, all request times will be measured and informed on the application’s log, like this:
1
|
|
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
|
|
Enjoy :)