Commit 5b89a956 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch 'ajk-alert-management-n-plus-ones' into 'master'

Improve GraphQL performance specs for alert management

See merge request gitlab-org/gitlab!50459
parents 430ebc13 01fdb668
...@@ -60,19 +60,35 @@ RSpec.describe 'getting Alert Management Alert Assignees' do ...@@ -60,19 +60,35 @@ RSpec.describe 'getting Alert Management Alert Assignees' do
expect(second_assignees).to be_empty expect(second_assignees).to be_empty
end end
it 'avoids N+1 queries' do describe 'performance' do
base_count = ActiveRecord::QueryRecorder.new do let(:first_n) { var('Int') }
post_graphql(query, current_user: current_user) let(:params) { { first: first_n } }
let(:limited_query) { with_signature([first_n], query) }
before do
create(:alert_management_alert, project: project, assignees: [current_user])
end
it 'can limit results' do
post_graphql(limited_query, current_user: current_user, variables: first_n.with(1))
expect(alerts.size).to eq 1
expect(alerts).not_to include(a_hash_including('iid' => first_alert.iid.to_s))
end end
# An N+1 would mean a new alert would increase the query count it 'can include all results' do
third_alert = create(:alert_management_alert, project: project, assignees: [current_user]) post_graphql(limited_query, current_user: current_user)
expect { post_graphql(query, current_user: current_user) }.not_to exceed_query_limit(base_count) expect(alerts.size).to be > 1
expect(alerts).to include(a_hash_including('iid' => first_alert.iid.to_s))
end
third_assignees = assignees[third_alert.iid.to_s] it 'avoids N+1 queries' do
base_count = ActiveRecord::QueryRecorder.new do
post_graphql(limited_query, current_user: current_user, variables: first_n.with(1))
end
expect(third_assignees.length).to eq(1) expect { post_graphql(limited_query, current_user: current_user) }.not_to exceed_query_limit(base_count)
expect(third_assignees.first).to include('username' => current_user.username) end
end end
end end
...@@ -65,16 +65,28 @@ RSpec.describe 'getting Alert Management Alert Notes' do ...@@ -65,16 +65,28 @@ RSpec.describe 'getting Alert Management Alert Notes' do
expect(second_notes_result).to be_empty expect(second_notes_result).to be_empty
end end
it 'avoids N+1 queries' do describe 'performance' do
base_count = ActiveRecord::QueryRecorder.new do let(:first_n) { var('Int') }
post_graphql(query, current_user: current_user) let(:params) { { first: first_n } }
before do
# An N+1 would mean a new alert would increase the query count
create(:alert_management_alert, project: project)
end end
# An N+1 would mean a new alert would increase the query count it 'avoids N+1 queries' do
create(:alert_management_alert, project: project) q = with_signature([first_n], query)
expect { post_graphql(query, current_user: current_user) }.not_to exceed_query_limit(base_count) base_count = ActiveRecord::QueryRecorder.new do
expect(alerts_result.length).to eq(3) post_graphql(q, current_user: current_user, variables: first_n.with(1))
expect(alerts_result.length).to eq(1)
end
expect do
post_graphql(q, current_user: current_user, variables: first_n.with(3))
expect(alerts_result.length).to eq(3)
end.not_to exceed_query_limit(base_count)
end
end end
context 'for non-system notes' do context 'for non-system notes' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment