Class: ActiveRecordTracer::Reporter
- Inherits:
-
Object
- Object
- ActiveRecordTracer::Reporter
- Defined in:
- lib/active_record_tracer/reporter.rb
Class Attribute Summary collapse
-
.current ⇒ Object
Returns the value of attribute current.
Instance Method Summary collapse
-
#initialize(top: 50, backtrace_lines: 5, ignore_cached_queries: false, ignore_schema_queries: true) ⇒ Reporter
constructor
A new instance of Reporter.
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(top: 50, backtrace_lines: 5, ignore_cached_queries: false, ignore_schema_queries: true) ⇒ Reporter
Returns a new instance of Reporter.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/active_record_tracer/reporter.rb', line 10 def initialize(top: 50, backtrace_lines: 5, ignore_cached_queries: false, ignore_schema_queries: true) @top = top @backtrace_lines = backtrace_lines @ignore_cached_queries = ignore_cached_queries @ignore_schema_queries = ignore_schema_queries @total_runtime = 0.0 @queries_counts = Hash.new(0) @backtrace_queries = Hash.new { |h, k| h[k] = [] } @loaded_records = Hash.new(0) @loaded_records_backtraces = Hash.new(0) @subscriber1 = nil @subscriber2 = nil @backtraces_cache = {} end |
Class Attribute Details
.current ⇒ Object
Returns the value of attribute current.
7 8 9 |
# File 'lib/active_record_tracer/reporter.rb', line 7 def current @current end |
Instance Method Details
#start ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/active_record_tracer/reporter.rb', line 27 def start @subscriber1 = ActiveSupport::Notifications.monotonic_subscribe("sql.active_record") do |_name, start, finish, _id, payload| next if payload[:cached] && @ignore_cached_queries next if payload[:name] == "SCHEMA" && @ignore_schema_queries runtime = finish - start @total_runtime += runtime sql = payload[:sql].strip @queries_counts[sql] += 1 if @backtrace_lines && @backtrace_lines > 0 backtrace = query_backtrace(caller(1), @backtrace_lines) @backtrace_queries[backtrace] << sql end end @subscriber2 = ActiveSupport::Notifications.subscribe("instantiation.active_record") do |*, payload| record_count = payload[:record_count] # Active Record erroneously emits notifications even when # there are no records https://github.com/rails/rails/pull/52272. if record_count > 0 @loaded_records[payload[:class_name]] += record_count if @backtrace_lines && @backtrace_lines > 0 backtrace = query_backtrace(caller(1), @backtrace_lines) @loaded_records_backtraces[backtrace] += record_count end end end true end |
#stop ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/active_record_tracer/reporter.rb', line 61 def stop ActiveSupport::Notifications.unsubscribe(@subscriber1) ActiveSupport::Notifications.unsubscribe(@subscriber2) Report.new(@total_runtime, @queries_counts, @backtrace_queries, @loaded_records, @loaded_records_backtraces, top: @top) end |