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, run_lock

Class Method Details

.nameObject



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

def name
  :vernier
end

Instance Method Details

#resultsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/app_profiler/backend/vernier_backend.rb', line 57

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

#running?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/app_profiler/backend/vernier_backend.rb', line 79

def running?
  @collector != nil
end

#start(params = {}) ⇒ Object



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

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



47
48
49
50
51
52
53
54
55
# File 'lib/app_profiler/backend/vernier_backend.rb', line 47

def stop
  return false unless running?

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