Skip to content
This repository
Browse code

let automatic EXPLAIN ignore CACHE notifications

  • Loading branch information...
commit ee6e3c128b721570c8e59f507ea605cdd752cf7f 1 parent dd54137
Xavier Noria authored February 03, 2012
5  activerecord/lib/active_record/explain_subscriber.rb
@@ -11,7 +11,10 @@ def call(*args)
11 11
 
12 12
     # SCHEMA queries cannot be EXPLAINed, also we do not want to run EXPLAIN on
13 13
     # our own EXPLAINs now matter how loopingly beautiful that would be.
14  
-    IGNORED_PAYLOADS = %w(SCHEMA EXPLAIN)
  14
+    #
  15
+    # On the other hand, we want to monitor the performance of our real database
  16
+    # queries, not the performance of the access to the query cache.
  17
+    IGNORED_PAYLOADS = %w(SCHEMA EXPLAIN CACHE)
15 18
     def ignore_payload?(payload)
16 19
       payload[:exception] || IGNORED_PAYLOADS.include?(payload[:name])
17 20
     end
48  activerecord/test/cases/explain_subscriber_test.rb
... ...
@@ -0,0 +1,48 @@
  1
+require 'cases/helper'
  2
+
  3
+if ActiveRecord::Base.connection.supports_explain?
  4
+  class ExplainSubscriberTest < ActiveRecord::TestCase
  5
+    SUBSCRIBER = ActiveRecord::ExplainSubscriber.new
  6
+
  7
+    def test_collects_nothing_if_available_queries_for_explain_is_nil
  8
+      with_queries(nil) do
  9
+        SUBSCRIBER.call
  10
+        assert_nil Thread.current[:available_queries_for_explain]
  11
+      end
  12
+    end
  13
+
  14
+    def test_collects_nothing_if_the_payload_has_an_exception
  15
+      with_queries([]) do |queries|
  16
+        SUBSCRIBER.call(:exception => Exception.new)
  17
+        assert queries.empty?
  18
+      end
  19
+    end
  20
+
  21
+    def test_collects_nothing_for_ignored_payloads
  22
+      with_queries([]) do |queries|
  23
+        ActiveRecord::ExplainSubscriber::IGNORED_PAYLOADS.each do |ip|
  24
+          SUBSCRIBER.call(:name => ip)
  25
+        end
  26
+        assert queries.empty?
  27
+      end
  28
+    end
  29
+
  30
+    def test_collects_pairs_of_queries_and_binds
  31
+      sql   = 'select 1 from users'
  32
+      binds = [1, 2]
  33
+      with_queries([]) do |queries|
  34
+        SUBSCRIBER.call(:name => 'SQL', :sql => sql, :binds => binds)
  35
+        assert_equal 1, queries.size
  36
+        assert_equal sql, queries[0][0]
  37
+        assert_equal binds, queries[0][1]
  38
+      end
  39
+    end
  40
+
  41
+    def with_queries(queries)
  42
+      Thread.current[:available_queries_for_explain] = queries
  43
+      yield queries
  44
+    ensure
  45
+      Thread.current[:available_queries_for_explain] = nil
  46
+    end
  47
+  end
  48
+end

0 notes on commit ee6e3c1

Please sign in to comment.
Something went wrong with that request. Please try again.