Commit 20a08965 authored by Bob Van Landuyt's avatar Bob Van Landuyt

[WIP] improve number of queries when rendering a hierarchy

parent ea4e17e2
...@@ -38,7 +38,7 @@ class GroupChildrenFinder ...@@ -38,7 +38,7 @@ class GroupChildrenFinder
private private
def children def children
@children ||= subgroups + projects @children ||= subgroups.with_route.includes(:route, :parent) + projects.with_route.includes(:route, :namespace)
end end
def base_groups def base_groups
...@@ -64,7 +64,7 @@ class GroupChildrenFinder ...@@ -64,7 +64,7 @@ class GroupChildrenFinder
else else
base_groups base_groups
end end
groups.sort(params[:sort]).includes(:route) groups.sort(params[:sort])
end end
def base_projects def base_projects
...@@ -85,6 +85,6 @@ class GroupChildrenFinder ...@@ -85,6 +85,6 @@ class GroupChildrenFinder
else else
base_projects base_projects
end end
projects.sort(params[:sort]).includes(:route, :namespace) projects.sort(params[:sort])
end end
end end
...@@ -300,22 +300,28 @@ describe GroupsController do ...@@ -300,22 +300,28 @@ describe GroupsController do
let(:expected_queries_per_group) { 5 } let(:expected_queries_per_group) { 5 }
let(:expected_queries_per_project) { 0 } let(:expected_queries_per_project) { 0 }
before do
# Create the group before anything so it doesn't get tracked by the
# query recorder
group
end
def get_list def get_list
get :children, id: group.to_param, format: :json get :children, id: group.to_param, format: :json
end end
it 'queries the expected amount for a group row' do it 'queries the expected amount for a group row' do
control_count = ActiveRecord::QueryRecorder.new { get_list }.count control = ActiveRecord::QueryRecorder.new { get_list }
_new_group = create(:group, :public, parent: group) _new_group = create(:group, :public, parent: group)
expect { get_list }.not_to exceed_query_limit(control_count + expected_queries_per_group) expect { get_list }.not_to exceed_query_limit(control).with_threshold(expected_queries_per_group)
end end
it 'queries the expected amount for a project row' do it 'queries the expected amount for a project row' do
control_count = ActiveRecord::QueryRecorder.new { get_list }.count control = ActiveRecord::QueryRecorder.new { get_list }
_new_project = create(:project, :public, namespace: group) _new_project = create(:project, :public, namespace: group)
expect { get_list }.not_to exceed_query_limit(control_count + expected_queries_per_project) expect { get_list }.not_to exceed_query_limit(control).with_threshold(expected_queries_per_project)
end end
context 'when rendering hierarchies' do context 'when rendering hierarchies' do
...@@ -326,41 +332,42 @@ describe GroupsController do ...@@ -326,41 +332,42 @@ describe GroupsController do
it 'queries the expected amount when nested rows are rendered for a group' do it 'queries the expected amount when nested rows are rendered for a group' do
matched_group = create(:group, :public, parent: public_subgroup, name: 'filterme') matched_group = create(:group, :public, parent: public_subgroup, name: 'filterme')
control_count = ActiveRecord::QueryRecorder.new { get_filtered_list }.count control = ActiveRecord::QueryRecorder.new { get_filtered_list }
nested_group = create(:group, :public, parent: public_subgroup) nested_group = create(:group, :public, parent: public_subgroup)
matched_group.update!(parent: nested_group) matched_group.update!(parent: nested_group)
expect { get_filtered_list }.not_to exceed_query_limit(control_count + expected_queries_per_group) expect { get_filtered_list }.not_to exceed_query_limit(control).with_threshold(expected_queries_per_group)
end end
it 'queries the expected amount when a new group match is added' do it 'queries the expected amount when a new group match is added' do
create(:group, :public, parent: public_subgroup, name: 'filterme') create(:group, :public, parent: public_subgroup, name: 'filterme')
control_count = ActiveRecord::QueryRecorder.new { get_filtered_list }.count control = ActiveRecord::QueryRecorder.new { get_filtered_list }
create(:group, :public, parent: public_subgroup, name: 'filterme2') create(:group, :public, parent: public_subgroup, name: 'filterme2')
expect { get_filtered_list }.not_to exceed_query_limit(control_count + expected_queries_per_group) expect { get_filtered_list }.not_to exceed_query_limit(control).with_threshold(expected_queries_per_group)
end end
it 'queries the expected amount when nested rows are rendered for a project' do it 'queries the expected amount when nested rows are rendered for a project' do
matched_project = create(:project, :public, namespace: public_subgroup, name: 'filterme') matched_project = create(:project, :public, namespace: public_subgroup, name: 'filterme')
control_count = ActiveRecord::QueryRecorder.new { get_filtered_list }.count control = ActiveRecord::QueryRecorder.new { get_filtered_list }
nested_group = create(:group, :public, parent: public_subgroup) nested_group = create(:group, :public, parent: public_subgroup)
matched_project.update!(namespace: nested_group) matched_project.update!(namespace: nested_group)
expect { get_filtered_list }.not_to exceed_query_limit(control_count + expected_queries_per_group) expect { get_filtered_list }.not_to exceed_query_limit(control).with_threshold(expected_queries_per_group)
end end
it 'queries the expected amount when a new project match is added' do it 'queries the expected amount when a new project match is added' do
create(:project, :public, namespace: public_subgroup, name: 'filterme') create(:project, :public, namespace: public_subgroup, name: 'filterme')
control_count = ActiveRecord::QueryRecorder.new { get_filtered_list }.count control = ActiveRecord::QueryRecorder.new { get_filtered_list }
create(:project, :public, namespace: public_subgroup, name: 'filterme2') create(:project, :public, namespace: public_subgroup, name: 'filterme2')
expect { get_filtered_list }.not_to exceed_query_limit(control_count + expected_queries_per_project) expect { get_filtered_list }.not_to exceed_query_limit(control).with_threshold(expected_queries_per_project)
end end
end end
end end
......
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