From ed41888ba4ebd885bf56ebf11f43150a857c8e23 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 3 Mar 2021 13:21:58 +0100 Subject: [PATCH] Set up Bullet for development and testing This will log any N+1 it finds, pointing to the line causing it and a way to solve it, aka. which `#includes` to add. Like so ``` web_1 | GET /admin/order_cycles.json?ams_prefix=index&q%5Borders_close_at_gt%5D=Sun+Jan+31+2021+00:00:00+GMT%2B0100+(Central+European+Standard+Time) web_1 | USE eager loading detected web_1 | OrderCycle => [:coordinator] web_1 | Add to your query: .includes([:coordinator]) web_1 | Call stack web_1 | /usr/src/app/app/serializers/api/admin/index_order_cycle_serializer.rb:41:in `coordinator' web_1 | /usr/src/app/app/controllers/spree/admin/base_controller.rb:98:in `render_as_json' web_1 | /usr/src/app/app/controllers/admin/order_cycles_controller.rb:17:in `block (2 levels) in index' web_1 | /usr/src/app/app/controllers/admin/order_cycles_controller.rb:14:in `index' web_1 | bin/rails:4:in `require' web_1 | bin/rails:4:in `
' ``` We gave a try at Bullet long ago and abandoned it because it's not a silver bullet (pun intended) due to false positives. However, it's pretty clear that this won't happen often; we have endless N+1 still to fix. I recently experienced how, relying on Bullet made it just extra 30s to fix additional N+1s other than the one I was fixing. Usually, finding the culprit line takes me more of 30min. --- Gemfile | 1 + Gemfile.lock | 5 +++++ Gemfile_next.lock | 5 +++++ config/initializers/bullet.rb | 9 +++++++++ 4 files changed, 20 insertions(+) create mode 100644 config/initializers/bullet.rb diff --git a/Gemfile b/Gemfile index a83014ef03..3fa5c78ca4 100644 --- a/Gemfile +++ b/Gemfile @@ -144,6 +144,7 @@ group :test, :development do # Pretty printed test output gem 'atomic' gem 'awesome_print' + gem 'bullet' gem 'capybara' gem 'database_cleaner', require: false gem "factory_bot_rails", '5.2.0', require: false diff --git a/Gemfile.lock b/Gemfile.lock index b3a698156b..29a1e3b326 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -124,6 +124,9 @@ GEM bugsnag (6.19.0) concurrent-ruby (~> 1.0) builder (3.2.4) + bullet (6.1.4) + activesupport (>= 3.0.0) + uniform_notifier (~> 1.11) byebug (11.1.3) cancancan (1.7.1) capybara (3.32.2) @@ -700,6 +703,7 @@ GEM unicorn-worker-killer (0.4.4) get_process_mem (~> 0) unicorn (>= 4, < 6) + uniform_notifier (1.14.1) warden (1.2.7) rack (>= 1.0) webdrivers (4.6.0) @@ -742,6 +746,7 @@ DEPENDENCIES awesome_print aws-sdk (= 1.67.0) bugsnag + bullet byebug cancancan (~> 1.7.0) capybara diff --git a/Gemfile_next.lock b/Gemfile_next.lock index 3f9065b3b1..cd7a1c4a9f 100644 --- a/Gemfile_next.lock +++ b/Gemfile_next.lock @@ -123,6 +123,9 @@ GEM bugsnag (6.18.0) concurrent-ruby (~> 1.0) builder (3.2.4) + bullet (6.1.4) + activesupport (>= 3.0.0) + uniform_notifier (~> 1.11) byebug (11.0.1) cancancan (1.7.1) capybara (3.15.0) @@ -524,6 +527,7 @@ GEM unicorn-worker-killer (0.4.4) get_process_mem (~> 0) unicorn (>= 4, < 6) + uniform_notifier (1.14.1) warden (1.2.9) rack (>= 2.0.9) webdrivers (4.2.0) @@ -568,6 +572,7 @@ DEPENDENCIES awesome_print aws-sdk (= 1.67.0) bugsnag + bullet byebug cancancan (~> 1.7.0) capybara diff --git a/config/initializers/bullet.rb b/config/initializers/bullet.rb new file mode 100644 index 0000000000..89ba05a6c3 --- /dev/null +++ b/config/initializers/bullet.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +if defined?(Bullet) + Rails.application.config.after_initialize do + Bullet.enable = true + Bullet.bullet_logger = true + Bullet.rails_logger = true + end +end