Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide initial implementation #1

Closed
DmitryTsepelev opened this issue Feb 18, 2022 · 2 comments · Fixed by #4
Closed

Provide initial implementation #1

DmitryTsepelev opened this issue Feb 18, 2022 · 2 comments · Fixed by #4

Comments

@DmitryTsepelev
Copy link
Owner

DmitryTsepelev commented Feb 18, 2022

More details on Cult.

We need to implement a module that patches controller actions to measure database and HTTP response payload:

 class ReportController < ApplicationController
   include IoToResponsePayloadRatio::Controller

   def monthly_transaction_sum
     sum = Transactions.where(date: Date.current.all_month).sum(&:amount)
     render json: { sum: sum }
   end
 end

It can help find possible memory bloats earlier. These measurements can be sent to Rails logs or published as notification events. For instance, log could look like this:

Completed 200 OK in 1232ms (ActiveRecord: 400ms, DB Payload: 10kB, Body: 1kB, Allocations: 213223)
@prog-supdex
Copy link

Hello, @DmitryTsepelev!
I am now working on this and have several questions)

What Do I need to measure? Allocated memory of executed query and builded ActiveRecord::Relation or just calculate fetched data from database using bytesize (PG::Result#tuples or ActiveRecord::Result#rows)?

Just, if we execute

Transaction.select(:amount).sum(&:amount)

we will get all instances of Transaction, not only field amount.

and

Transaction.pluck(:amount).sum

will get less memory, than first, but same data fetched

Currently, i am measuring spent memory for this and also calculates bytes of fetched data. And I don`t know, which way is right

Second question. Should I calculate response body only when using render json? Or Have I also think about rendering view case?

@DmitryTsepelev
Copy link
Owner Author

Hi @prog-supdex!

will get less memory, than first, but same data fetched

That's right, and Transaction.sum(:amount) will get the same data fetched with even less amount of memory needed, and that's what I'd like to measure 🙂My assumption was that when we load a lot of data from the source but return way less data to the client there is a chance we can move these calculations outside our app, and the gem can help detect such things.

Should I calculate response body only when using render json?

My idea was to start with something simple, so JSON would be enough. The task is a huge enough already 🙂You can also omit some data sources and focus on one or two (e.g., only DB).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants