Class: AppProfiler::Backend::VernierBackend

Inherits:
BaseBackend
  • Object
show all
Defined in:
lib/app_profiler/backend/vernier_backend.rb

Constant Summary collapse

DEFAULTS =
{
  mode: :wall,
}.freeze
AVAILABLE_MODES =
[
  :wall,
  :retained,
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseBackend

run_lock

Class Method Details

.nameObject



18
19
20
# File 'lib/app_profiler/backend/vernier_backend.rb', line 18

def self.name
  :vernier
end

Instance Method Details

#resultsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/app_profiler/backend/vernier_backend.rb', line 69

def results
  vernier_profile = backend_results
  clear

  return unless vernier_profile

  # HACK: - "data" is private, but we want to avoid serializing to JSON then
  # parsing back from JSON by just directly getting the hash
  data = ::Vernier::Output::Firefox.new(vernier_profile).send(:data)
  data[:meta][:mode] = @mode # TODO: https://github.com/jhawthorn/vernier/issues/30
  data[:meta].merge!(@metadata) if @metadata
  @mode = nil
  @metadata = nil

  BaseProfile.from_vernier(data)
rescue => error
  AppProfiler.logger.info(
    "[Profiler] failed to obtain the profile error_class=#{error.class} error_message=#{error.message}"
  )
  nil
end

#run(params = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/app_profiler/backend/vernier_backend.rb', line 22

def run(params = {})
  started = start(params)

  yield

  return unless started

  stop
  results
ensure
  # Only stop the profiler if profiling was started in this context.
  stop if started
end

#running?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/app_profiler/backend/vernier_backend.rb', line 91

def running?
  @collector != nil
end

#start(params = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/app_profiler/backend/vernier_backend.rb', line 36

def start(params = {})
  # Do not start the profiler if we already have a collector started somewhere else.
  return false if running?
  return false unless acquire_run_lock

  @mode = params.delete(:mode) || DEFAULTS[:mode]
  raise ArgumentError unless AVAILABLE_MODES.include?(@mode)

  @metadata = params.delete(:metadata)
  clear

  @collector ||= ::Vernier::Collector.new(@mode, **params)
  @collector.start
rescue => error
  AppProfiler.logger.info(
    "[Profiler] failed to start the profiler error_class=#{error.class} error_message=#{error.message}"
  )
  release_run_lock
  # This is a boolean instead of nil to be consistent with the stackprof backend behaviour
  # boolean as well.
  false
end

#stopObject



59
60
61
62
63
64
65
66
67
# File 'lib/app_profiler/backend/vernier_backend.rb', line 59

def stop
  return false unless running?

  @results = @collector&.stop
  @collector = nil
  !@results.nil?
ensure
  release_run_lock
end