From 84242714b11acc2011392cb53539546a0f22d482 Mon Sep 17 00:00:00 2001 From: Larry Gebhardt Date: Fri, 19 Jul 2019 10:51:40 -0400 Subject: [PATCH] Warn on performance issues --- lib/jsonapi/active_relation_resource.rb | 4 ++++ lib/jsonapi/configuration.rb | 4 ++++ test/controllers/controller_test.rb | 23 +++++++++++++++++++++++ test/fixtures/active_record.rb | 10 +++++++++- 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/jsonapi/active_relation_resource.rb b/lib/jsonapi/active_relation_resource.rb index 39846b9f6..5f800de50 100644 --- a/lib/jsonapi/active_relation_resource.rb +++ b/lib/jsonapi/active_relation_resource.rb @@ -181,6 +181,10 @@ def find_fragments(filters, options = {}) end end + if JSONAPI.configuration.warn_on_performance_issues && (rows.length > fragments.length) + warn "Performance issue detected: `#{self.name.to_s}.records` returned non-normalized results in `#{self.name.to_s}.find_fragments`." + end + fragments end diff --git a/lib/jsonapi/configuration.rb b/lib/jsonapi/configuration.rb index 5cf33e845..a91c453da 100644 --- a/lib/jsonapi/configuration.rb +++ b/lib/jsonapi/configuration.rb @@ -10,6 +10,7 @@ class Configuration :raise_if_parameters_not_allowed, :warn_on_route_setup_issues, :warn_on_missing_routes, + :warn_on_performance_issues, :default_allow_include_to_one, :default_allow_include_to_many, :allow_sort, @@ -60,6 +61,7 @@ def initialize self.warn_on_route_setup_issues = true self.warn_on_missing_routes = true + self.warn_on_performance_issues = true # :none, :offset, :paged, or a custom paginator name self.default_paginator = :none @@ -272,6 +274,8 @@ def allow_include=(allow_include) attr_writer :warn_on_missing_routes + attr_writer :warn_on_performance_issues + attr_writer :use_relationship_reflection attr_writer :resource_cache diff --git a/test/controllers/controller_test.rb b/test/controllers/controller_test.rb index fcc9a9819..7d266792f 100644 --- a/test/controllers/controller_test.rb +++ b/test/controllers/controller_test.rb @@ -3625,6 +3625,29 @@ def test_book_comments_exclude_unapproved_context_based end end +class Api::V4::PostsControllerTest < ActionController::TestCase + def test_warn_on_joined_to_many + original_config = JSONAPI.configuration.dup + + JSONAPI.configuration.warn_on_performance_issues = true + _out, err = capture_subprocess_io do + get :index, params: {fields: {posts: 'id,title'}} + assert_response :success + end + assert_equal(err, "Performance issue detected: `Api::V4::PostResource.records` returned non-normalized results in `Api::V4::PostResource.find_fragments`.\n") + + JSONAPI.configuration.warn_on_performance_issues = false + _out, err = capture_subprocess_io do + get :index, params: {fields: {posts: 'id,title'}} + assert_response :success + end + assert_empty err + + ensure + JSONAPI.configuration = original_config + end +end + class Api::V4::BooksControllerTest < ActionController::TestCase def setup JSONAPI.configuration.json_key_format = :camelized_key diff --git a/test/fixtures/active_record.rb b/test/fixtures/active_record.rb index 602d2825b..68bb69fc3 100644 --- a/test/fixtures/active_record.rb +++ b/test/fixtures/active_record.rb @@ -1989,7 +1989,15 @@ class PreferencesResource < PreferencesResource; end module Api module V4 - class PostResource < PostResource; end + class PostResource < PostResource + class << self + def records(options = {}) + # Sets up a performance issue for testing + super(options).joins(:comments) + end + end + end + class PersonResource < PersonResource; end class ExpenseEntryResource < ExpenseEntryResource; end class IsoCurrencyResource < IsoCurrencyResource