I know from the server log (or the console output), that the time to show the page can be seen. But what about just for product manager to see real time how much time it takes to render a page? (also for easy to see for the developer)
I tried using at the very beginning of the controller's action
@time_start = Time.now
and at the end of view application layout, show
Time.now - @time_start
actually, it is kind of close... about 3% to 5% off only. But is there a way to show as close as possible, or even somehow show what the server log is showing?
For example, is there a @time_start that Rails keep as the very beginning even before it does any routing? Maybe a timestamp the moment 开发者_开发技巧the request is received by the server?
First off, I suggest checking out an enterprise Rails benchmarking solution, such as New Relic (it's free in development mode!).
However if you want to do it for yourself, I would suggest tapping into the Middleware and time it there.
This should get you started:
http://asciicasts.com/episodes/151-rack-middleware
New relic has a developer mode that will give you insight to the time it takes to render a page
- http://support.newrelic.com/kb/docs/developer-mode
- https://github.com/newrelic/rpm
Unfortunately, this impossible to do in a single pass - by definition! You can't ever render the full amount of time it's taken to render a page to the page itself, because that page, obviously, is still getting rendered.
If you want to be super-precise, you'll probably have to cache the full render time (in the session, maybe?) and fetch it with JS. Or you could show it on the next page, but that might be confusing.
If you want to come as close as you can, but don't want to bother with the above, then I'd recommend putting your <%= @start_time - Time.now %>
line towards the end of your layout - things like the content_for helper imply that the layout is actually rendered after the page template.
Hope this helps!
精彩评论