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



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

def name
  :vernier
end

Instance Method Details

#resultsObject



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

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



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

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)


93
94
95
# File 'lib/app_profiler/backend/vernier_backend.rb', line 93

def running?
  @collector != nil
end

#start(params = {}) ⇒ Object



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

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



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

def stop
  return false unless running?

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