Sha256: e190970035e32a87efe2de408f755ebb7cd48ef22fb3bc9328d4de66111383be

Contents?: true

Size: 928 Bytes

Versions: 1

Compression:

Stored size: 928 Bytes

Contents

require "stackviz/version"
require "stackprof"

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.1 lib/stackviz.rb