1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/panda26-gitlab

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Клонировать/Скачать
query_recorder.md 1.9 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
James Edwards-Jones Отправлено 09.03.2017 17:54 4dc99f3

QueryRecorder

QueryRecorder is a tool for detecting the N+1 queries problem from tests.

Implemented in spec/support/query_recorder.rb via 9c623e3e

As a rule, merge requests should not increase query counts. If you find yourself adding something like .includes(:author, :assignee) to avoid having N+1 queries, consider using QueryRecorder to enforce this with a test. Without this, a new feature which causes an additional model to be accessed will silently reintroduce the problem.

How it works

This style of test works by counting the number of SQL queries executed by ActiveRecord. First a control count is taken, then you add new records to the database and rerun the count. If the number of queries has significantly increased then an N+1 queries problem exists.

it "avoids N+1 database queries" do
  control_count = ActiveRecord::QueryRecorder.new { visit_some_page }.count
  create_list(:issue, 5)
  expect { visit_some_page }.not_to exceed_query_limit(control_count)
end

As an example you might create 5 issues in between counts, which would cause the query count to increase by 5 if an N+1 problem exists.

Note: In some cases the query count might change slightly between runs for unrelated reasons. In this case you might need to test exceed_query_limit(control_count + acceptable_change), but this should be avoided if possible.

See also

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/panda26-gitlab.git
git@api.gitlife.ru:oschina-mirror/panda26-gitlab.git
oschina-mirror
panda26-gitlab
panda26-gitlab
master