Class: Stackflame

Inherits:
Object
  • Object
show all
Defined in:
lib/stackflame.rb,
lib/stackflame/version.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  interval: 1000,
  mode:     :cpu,
  raw:      true,
}.freeze
VERSION =
"0.2.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.profile(options = {}, &block) ⇒ Object



12
13
14
15
16
# File 'lib/stackflame.rb', line 12

def self.profile(options = {}, &block)
  stackflame = self.new
  stackflame.run(options, &block)
  stackflame.open_flamegraph
end

Instance Method Details

#open_flamegraphObject



27
28
29
30
31
32
33
34
# File 'lib/stackflame.rb', line 27

def open_flamegraph
  if system("which osascript > /dev/null")
    # NOTE: `open` can't open path with query string
    `osascript -e 'open location "#{flamegraph_path}"'`
  else
    puts "This OS is not supported. Please open: #{flamegraph_path}"
  end
end

#run(options = {}, &block) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/stackflame.rb', line 18

def run(options = {}, &block)
  options = DEFAULT_OPTIONS.merge(options)
  result  = StackProf.run(options, &block)

  File.open(temp_js_path, 'w') do |f|
    StackProf::Report.new(result).print_flamegraph(f)
  end
end