Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Ruby on Rails render times

I am working on an "optimization" on my application and I am trying to understand the output that rails (version 2.2.2) gives at the end of the render.

Here is the "old" way:

Rendered user/_old_log (25.7ms)
Completed in 466ms (View: 195, DB: 8) | 200 OK

And the "new" way:

Rendered user/_new_log (48.6ms)
Completed in 337ms (View: 192, DB: 33) | 200 OK

These queries were exactly the same, the difference is the old way is parsing log files while the new way is querying the database log table.

The actual speed of the page is not the issue (the user understands that this is a slow request) ... but I would like the page to respond as quickly as possible even though it is a "slow" page.

So, my question is, what do the numbers represent/mean? In other words, which way was the faster method and why?

like image 212
salt.racer Avatar asked Mar 26 '09 22:03

salt.racer


People also ask

What is render in ROR?

Rendering is the ultimate goal of your Ruby on Rails application. You render a view, usually . html. erb files, which contain a mix of HMTL & Ruby code. A view is what the user sees.

How can you tell Rails to render without a layout *?

By default, if you use the :plain option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option and use the . text. erb extension for the layout file.

What does render JSON do in Rails?

Rails has built-in support for converting objects to JSON and rendering that JSON back to the browser: render json: @product. You don't need to call to_json on the object that you want to render. If you use the :json option, render will automatically call to_json for you.

What is Local_assigns?

local_assigns is a Rails view helper method that you can check whether this partial has been provided with local variables or not. Here you render a partial with some values, the headline and person will become accessible with predefined value.


2 Answers

This:

Rendered user/_old_log (25.7ms)

is the time to render just the _old_log partial template, and comes from an ActiveSupport::Notification getting processed by ActionView::LogSubscriber

This:

Completed 200 OK in 466ms

Is the http status returned, as well as the total time for the entire request. It comes from ActionController::LogSubscriber.

Also, note those parenthetical items at the end:

(Views: 124.6ms | ActiveRecord: 10.8ms)

Those are the total times for rendering the entire view (partials & everything) and all database requests, respectively, and come from ActionController::LogSubscriber as well.

like image 139
Jordan Brough Avatar answered Oct 01 '22 15:10

Jordan Brough


Jordan's answer is correct. To paraphrase, the first number is the time the page took to load. The second is how long the view took to generate. The last number is how long it took for your database to handle all queries you sent to it.

You can also get an estimation of how long your Controller and Model code took by subtracting the last two numbers from the first number, but a better way would be to use the Benchmark.measure method (http://www.ruby-doc.org/stdlib/libdoc/benchmark/rdoc/classes/Benchmark.html).

Your new way appears to have improved because code in the Controller/Model completes faster.

like image 33
Gdeglin Avatar answered Oct 01 '22 14:10

Gdeglin