Skip to content

Commit 0869490

Browse files
authored
capture the number of rows instantiated for active record (#585)
This is close to telling you the number of rows brought back from the database It doesn't show pluck results, so just need to keep that in mind. But it does help you understand why so much memory is being taken up from active record queries.
1 parent f564b12 commit 0869490

4 files changed

Lines changed: 23 additions & 1 deletion

File tree

lib/mini_profiler/profiling_methods.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def record_sql(query, elapsed_ms, params = nil)
1717
)
1818
end
1919

20+
def report_reader_duration(elapsed_ms, row_count = nil, class_name = nil)
21+
current&.current_timer&.report_reader_duration(elapsed_ms, row_count, class_name)
22+
end
23+
2024
def start_step(name)
2125
return unless current
2226
parent_timer = current.current_timer

lib/mini_profiler/timer_struct/request.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ def move_sql(sql, destination)
125125
end
126126
end
127127

128+
# please call SqlTiming#report_reader_duration instead
129+
def report_reader_duration(elapsed_ms, row_count = nil, class_name = nil)
130+
last_time = self[:sql_timings]&.last
131+
last_time&.report_reader_duration(elapsed_ms, row_count, class_name)
132+
end
133+
128134
def add_custom(type, elapsed_ms, page)
129135
TimerStruct::Custom.new(type, elapsed_ms, page, self).tap do |timer|
130136
timer[:parent_timing_id] = self[:id]

lib/mini_profiler/timer_struct/sql.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ def initialize(query, duration_ms, page, parent, params = nil, skip_backtrace =
5151
)
5252
end
5353

54-
def report_reader_duration(elapsed_ms)
54+
def report_reader_duration(elapsed_ms, row_count = nil, class_name = nil)
5555
return if @reported
5656
@reported = true
5757
self[:duration_milliseconds] += elapsed_ms
5858
@parent[:sql_timings_duration_milliseconds] += elapsed_ms
5959
@page[:duration_milliseconds_in_sql] += elapsed_ms
60+
self[:row_count] = self[:row_count].to_i + row_count if row_count
61+
self[:class_name] = class_name if class_name
6062
end
6163

6264
def trim_binds(binds)

lib/mini_profiler_rails/railtie.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ def self.initialize!(app)
114114
Rack::MiniProfiler.binds_to_params(payload[:binds])
115115
)
116116
end
117+
118+
subscribe("instantiation.active_record") do |name, start, finish, id, payload|
119+
next if !should_measure?
120+
121+
Rack::MiniProfiler.report_reader_duration(
122+
(finish - start) * 1000,
123+
payload[:record_count],
124+
payload[:class_name]
125+
)
126+
end
117127
end
118128
end
119129
@already_initialized = true

0 commit comments

Comments
 (0)