Sha256: 7b58e1348cfa76069264a91b489ff48c076dd1a76d50a3373965fba8b59e4061

Contents?: true

Size: 947 Bytes

Versions: 1

Compression:

Stored size: 947 Bytes

Contents

require "stackviz/version"
require "stackprof"
require "tempfile"

class Stackviz
  def self.profile(mode: :cpu, path: nil, open: true, &block)
    stackviz = self.new(path)
    stackviz.run(mode, &block)
    stackviz.open_graph if open
  end

  def initialize(path)
    return @path = path if path

    temp  = `mktemp /tmp/stackviz-XXXXXXXX`.strip
    @path = "#{temp}.gif"
    `mv #{temp} #{@path}`
  end

  def run(mode, &block)
    ensure_dot_existence

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

    Tempfile.open('stackviz') do |f|
      StackProf::Report.new(result).print_graphviz(nil, f)
      f.flush

      File.open(@path, 'w') do |graph|
        `dot -Tgif #{f.path} -o #{graph.path}`
      end
    end
  end

  def open_graph
    system("open #{@path}")
  end

  private

  def ensure_dot_existence
    unless system("which dot > /dev/null")
      abort "`dot` does not exist. Please install graphviz first."
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stackviz-0.0.2 lib/stackviz.rb