Class: RailsConsolePro::Services::ProfileCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_console_pro/services/profile_collector.rb

Overview

Collects metrics for profiling a block or relation execution

Constant Summary collapse

SQL_EVENT =
'sql.active_record'
INSTANTIATION_EVENT =
'instantiation.active_record'
CACHE_EVENTS =
%w[
  cache_read.active_support
  cache_generate.active_support
  cache_fetch_hit.active_support
  cache_write.active_support
].freeze
IGNORED_SQL_NAMES =
%w[SCHEMA CACHE EXPLAIN TRANSACTION].freeze
WRITE_SQL_REGEX =
/\A\s*(INSERT|UPDATE|DELETE|MERGE|REPLACE)\b/i.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = RailsConsolePro.config) ⇒ ProfileCollector

Returns a new instance of ProfileCollector.



20
21
22
# File 'lib/rails_console_pro/services/profile_collector.rb', line 20

def initialize(config = RailsConsolePro.config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/rails_console_pro/services/profile_collector.rb', line 18

def config
  @config
end

Instance Method Details

#profile(label: nil) ⇒ Object

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rails_console_pro/services/profile_collector.rb', line 24

def profile(label: nil)
  raise ArgumentError, 'ProfileCollector#profile requires a block' unless block_given?

  reset_state(label)
  subscribe!

  wall_start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  @started_at = Time.current

  begin
    @result = yield
  rescue => e
    @error = e
  ensure
    @finished_at = Time.current
    @duration_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - wall_start) * 1000.0).round(2)
    unsubscribe!
  end

  build_result
end