Sha256: 7c9081b4794c47c74805f83e1b9092e306a9487937450529b27364782593e193

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

#!/usr/bin/env ruby

require 'optparse'
require 'stackprof'

def main
  opt = OptionParser.new

  params = {
    mode: ENV['STACKPROF_MODE']&.to_sym || :cpu,
    out: ENV['STACKPROF_OUT'] || 'stackprof-out'
  }

  opt.banner = 'Usage: stackprof-run [options] command [command options]'
  opt.on('-m MODE', '--mode=MODE', ':cpu(default), :wall, :object', &:to_sym)
  opt.on('-o FILE', '--out=FILE', 'Output file name. (default: stackprof-out)')
  opt.on('-i INTERVAL', '--interval=INTERVAL', &:to_i)
  opt.on('-no-aggregate')
  opt.on('-r', '--raw')
  opt.on('-e command')

  opt.order!(ARGV, into: params)

  if params[:e]
    exec_code params
  else
    exec_command opt, params
  end
end

def exec_command(opt, params)
  cmd = ARGV.shift
  unless cmd
    puts opt.help
    exit 1
  end

  path =
    if File.exist?(cmd)
      cmd
    else
      `which #{cmd}`.chomp
    end

  $PROGRAM_NAME = cmd

  exec(params) do
    load path
  end
end

def exec_code(params)
  exec(params) do
    eval params[:e]
  end
end

def exec(params)
  opt = params.slice(:mode, :out, :interval, :raw).tap do |o|
    o[:aggregate] = !!params[:'no-aggregate']
  end
  ex = nil
  StackProf.run(opt) do
    begin
      yield
    rescue Exception => ex
    end
  end

  raise ex if ex
end

main

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stackprof-run-0.4.0 exe/stackprof-run