Module: Rack::MiniProfiler::ProfilingMethods
- Included in:
- Rack::MiniProfiler
- Defined in:
- lib/mini_profiler/profiling_methods.rb
Instance Method Summary collapse
-
#counter(type, duration_ms = nil) ⇒ Object
Add a custom timing.
- #counter_method(klass, method, &blk) ⇒ Object
- #finish_step(obj) ⇒ Object
- #profile_method(klass, method, type = :profile, &blk) ⇒ Object
- #profile_singleton_method(klass, method, type = :profile, &blk) ⇒ Object
- #record_sql(query, elapsed_ms, params = nil, cached = nil) ⇒ Object
- #report_reader_duration(elapsed_ms, row_count = nil, class_name = nil) ⇒ Object
- #start_step(name) ⇒ Object
-
#step(name, opts = nil) ⇒ Object
perform a profiling step on given block.
- #uncounter_method(klass, method) ⇒ Object
- #unprofile_method(klass, method) ⇒ Object
- #unprofile_singleton_method(klass, method) ⇒ Object
Instance Method Details
#counter(type, duration_ms = nil) ⇒ Object
Add a custom timing. These are displayed similar to SQL/query time in columns expanding to the right.
type - String counter type. Each distinct type gets its own column. duration_ms - Duration of the call in ms. Either this or a block must be
given but not both.
When a block is given, calculate the duration by yielding to the block and keeping a record of its run time.
Returns the result of the block, or nil when no block is given.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mini_profiler/profiling_methods.rb', line 148 def counter(type, duration_ms = nil) result = nil if block_given? start = Process.clock_gettime(Process::CLOCK_MONOTONIC) result = yield duration_ms = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start).to_f * 1000 end return result if current.nil? || ! current.current_timer.add_custom(type, duration_ms, current.page_struct) result end |
#counter_method(klass, method, &blk) ⇒ Object
69 70 71 |
# File 'lib/mini_profiler/profiling_methods.rb', line 69 def counter_method(klass, method, &blk) self.profile_method(klass, method, :counter, &blk) end |
#finish_step(obj) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/mini_profiler/profiling_methods.rb', line 32 def finish_step(obj) return unless obj && current current_timer, parent_timer = obj current_timer.record_time current.current_timer = parent_timer end |
#profile_method(klass, method, type = :profile, &blk) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/mini_profiler/profiling_methods.rb', line 77 def profile_method(klass, method, type = :profile, &blk) default_name = type == :counter ? method.to_s : klass.to_s + " " + method.to_s clean = clean_method_name(method) with_profiling = ("#{clean}_with_mini_profiler").intern without_profiling = ("#{clean}_without_mini_profiler").intern if klass.send :method_defined?, with_profiling return # dont double profile end klass.send :alias_method, without_profiling, method klass.send :define_method, with_profiling do |*args, &orig| return self.send without_profiling, *args, &orig unless Rack::MiniProfiler.current name = default_name if blk name = if respond_to?(:instance_exec) instance_exec(*args, &blk) else # deprecated in Rails 4.x blk.bind(self).call(*args) end end parent_timer = Rack::MiniProfiler.current.current_timer if type == :counter start = Process.clock_gettime(Process::CLOCK_MONOTONIC) begin self.send without_profiling, *args, &orig ensure duration_ms = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start).to_f * 1000 parent_timer.add_custom(name, duration_ms, Rack::MiniProfiler.current.page_struct) end else Rack::MiniProfiler.current.current_timer = current_timer = parent_timer.add_child(name) begin self.send without_profiling, *args, &orig ensure current_timer.record_time Rack::MiniProfiler.current.current_timer = parent_timer end end end if klass.respond_to?(:ruby2_keywords, true) klass.send(:ruby2_keywords, with_profiling) end klass.send :alias_method, method, with_profiling end |
#profile_singleton_method(klass, method, type = :profile, &blk) ⇒ Object
129 130 131 |
# File 'lib/mini_profiler/profiling_methods.rb', line 129 def profile_singleton_method(klass, method, type = :profile, &blk) profile_method(klass.singleton_class, method, type, &blk) end |
#record_sql(query, elapsed_ms, params = nil, cached = nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/mini_profiler/profiling_methods.rb', line 7 def record_sql(query, elapsed_ms, params = nil, cached = nil) return unless current && current.current_timer c = current c.current_timer.add_sql( redact_sql_queries? ? nil : query, elapsed_ms, c.page_struct, redact_sql_queries? ? nil : params, c.skip_backtrace, c.full_backtrace, cached ) end |
#report_reader_duration(elapsed_ms, row_count = nil, class_name = nil) ⇒ Object
21 22 23 |
# File 'lib/mini_profiler/profiling_methods.rb', line 21 def report_reader_duration(elapsed_ms, row_count = nil, class_name = nil) current&.current_timer&.report_reader_duration(elapsed_ms, row_count, class_name) end |
#start_step(name) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/mini_profiler/profiling_methods.rb', line 25 def start_step(name) return unless current parent_timer = current.current_timer current.current_timer = current_timer = current.current_timer.add_child(name) [current_timer, parent_timer] end |
#step(name, opts = nil) ⇒ Object
perform a profiling step on given block
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mini_profiler/profiling_methods.rb', line 40 def step(name, opts = nil) if current parent_timer = current.current_timer current.current_timer = current_timer = current.current_timer.add_child(name) begin yield if block_given? ensure current_timer.record_time current.current_timer = parent_timer end else yield if block_given? end end |
#uncounter_method(klass, method) ⇒ Object
73 74 75 |
# File 'lib/mini_profiler/profiling_methods.rb', line 73 def uncounter_method(klass, method) self.unprofile_method(klass, method) end |
#unprofile_method(klass, method) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/mini_profiler/profiling_methods.rb', line 55 def unprofile_method(klass, method) clean = clean_method_name(method) with_profiling = ("#{clean}_with_mini_profiler").intern without_profiling = ("#{clean}_without_mini_profiler").intern if klass.send :method_defined?, with_profiling klass.send :alias_method, method, without_profiling klass.send :remove_method, with_profiling klass.send :remove_method, without_profiling end end |
#unprofile_singleton_method(klass, method) ⇒ Object
133 134 135 |
# File 'lib/mini_profiler/profiling_methods.rb', line 133 def unprofile_singleton_method(klass, method) unprofile_method(klass.singleton_class, method) end |