Question

I'm trying to display a server response time on the page similar to google's search time, something like "page loaded in about 1.3 seconds" or so.

What is the best way of achieving this? I currently have a MVC framework setup, and my initial approach was to store initial time in the controller, and pass it as the model into the view and it is up to view to calculate the time elapsed.

Somehow I feel there must be a better approach, that, for all requests, context might already have the information recorded, either the request start time, or the elapsed time.

Can someone please verify if my original thought was right? or there exists an already implemented solution?

Thanks,

Jason

Was it helpful?

Solution

If you wait all the way until your controller, you're potentially missing a lot of the "load time". You want to use a Filter to time the request from as early to as late in the process as possible. The Java EE Tutorial has more details on writing Filters. There's also another SO answer that deals with exactly this:

In spring MVC, where to start and end counter to test for speed execution time?

OTHER TIPS

You could use a servlet filter to store the start time in a request attribute for each request (or at least for each request to a page), and compute the elapsed time at the end of your view execution.

If you use a template engine like Tiles or SiteMesh, this elapsed time computation would be called in a single place: the page template.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top