Erik Sapir
Erik Sapir

Reputation: 24767

Grails: Printing how long each request took

I would like to print for each request in my controllers, when the request started, when it ended and how long was the process. Do i have to add this code to each request separately or is there any generic way to do that?

Upvotes: 0

Views: 474

Answers (2)

Joshua Moore
Joshua Moore

Reputation: 24776

Besides the wide variety of plugins that exist (Profiler for example) a very simple solution is to use a filter. This blog post walks you through the process of setting up a filter that will log the start and end times for each controller action.

A very simple example using a Grails filter would be:

class PerfFilter {
  def filters = {
    profiler(controller: '*', action: '*') {
      before {
        request._startTime = System.currentTimeMillis()
        log.trace("Beginning ${controllerName} ${actionName}")
      }
      after {
        log.trace("Finished ${controllerName} ${actionName}")
        log.trace("Total time to execute was: ${System.currentTimeMillis() - request._startTime}")
      }
    }
  }
}

If you need something more robust for overall performance monitoring I highly recommend the Javamelody plugin for Grails.

Upvotes: 5

Orubel
Orubel

Reputation: 334

apache AB helps immensely. You can do an ab test with one concurrency and one request to just show the time if need be. I'd also suggest a tool called 'New Relic'. Great for testing and shows time for request and bottlenecks.

Upvotes: 0

Related Questions