palkan / n_plus_one_control

RSpec and Minitest matchers to prevent N+1 queries problem
MIT License
553 stars 20 forks source link

Make current scale factor available in test #36

Closed Earendil95 closed 4 years ago

Earendil95 commented 4 years ago

Hey Vladimir!

Thanks for the gem. I've found one potential performance improvement:

In some cases we are able to manage the number of records through parameters of the request, for example, when we have pagination:

get :index, params: { per_page: 10 }

or in GraphQL:

query {
  posts(limit: 10) { id }
}

In those cases we don't need to recreate records before each run of actual block and can manage it as in the example. For this purpose I've added scale method, which is an alias to current scale factor of executor. Supposed usage:

context "N+1", :n_plus_one do
  specify do
    expect { get :index, params: { per_page: scale } }.to perform_constant_number_of_queries
  end
end
Earendil95 commented 4 years ago

@palkan thanks for the review :) WDYT about current_scale? To point that it is current and at the same time not to make the name too long.

palkan commented 4 years ago

WDYT about current_scale

LGTM. Let's use it