Class: Hone::Profilers::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/hone/profilers/factory.rb

Constant Summary collapse

DETECTORS =
[
  [Vernier, ->(data) { data.key?("threads") }],
  [StackProf, ->(data) { data.key?("frames") || data.key?("methods") }]
].freeze

Class Method Summary collapse

Class Method Details

.create(profile_path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hone/profilers/factory.rb', line 13

def self.create(profile_path)
  return nil if profile_path.nil?

  data = JSON.parse(File.read(profile_path))
  profiler_class = detect_profiler(data)

  raise Hone::Error, "Unknown profile format in #{profile_path}" unless profiler_class

  profiler_class.new(profile_path)
rescue JSON::ParserError => e
  raise Hone::Error, "Invalid profile JSON: #{e.message}"
end

.detect_profiler(data) ⇒ Object



26
27
28
# File 'lib/hone/profilers/factory.rb', line 26

def self.detect_profiler(data)
  DETECTORS.find { |_, detector| detector.call(data) }&.first
end