Class: AppProfiler::Backend::StackprofBackend

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

Constant Summary collapse

DEFAULTS =
{
  mode: :cpu,
  raw: true,
}.freeze
AVAILABLE_MODES =
[
  :wall,
  :cpu,
  :object,
].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/stackprof_backend.rb', line 19

def self.name
  :stackprof
end

Instance Method Details

#resultsObject



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

def results
  stackprof_profile = backend_results

  return unless stackprof_profile

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

#run(params = {}) ⇒ Object



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

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)


74
75
76
# File 'lib/app_profiler/backend/stackprof_backend.rb', line 74

def running?
  StackProf.running?
end

#start(params = {}) ⇒ Object



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

def start(params = {})
  # Do not start the profiler if StackProf was started somewhere else.
  return false if running?
  return false unless acquire_run_lock

  clear

  StackProf.start(**DEFAULTS, **params)
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 because StackProf#start returns a
  # boolean as well.
  false
end

#stopObject



55
56
57
58
59
# File 'lib/app_profiler/backend/stackprof_backend.rb', line 55

def stop
  StackProf.stop
ensure
  release_run_lock
end