Sha256: 8f4f769d89b082ee23683b925b8b1efa2e7f6935d265dc8d5d006698913f9fb6

Contents?: true

Size: 1017 Bytes

Versions: 1

Compression:

Stored size: 1017 Bytes

Contents

require "stackflame/version"
require "stackprof"

class Stackflame
  def self.profile(mode: :cpu, &block)
    stackflame = self.new
    stackflame.run(mode, &block)
    stackflame.open_flamegraph
  end

  def run(mode, &block)
    result = StackProf.run(mode: mode, raw: true, &block)

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

  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

  private

  def flamegraph_path
    viewer_path = File.expand_path('../../vendor/viewer.html', __FILE__)
    "file://#{viewer_path}?data=#{temp_js_path}"
  end

  def temp_js_path
    return @js_path if @js_path

    temp = `mktemp /tmp/stackflame-XXXXXXXX`.strip
    @js_path = "#{temp}.js"
    `mv #{temp} #{@js_path}`
    @js_path
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stackflame-0.0.1 lib/stackflame.rb