Sha256: d0007eb0376fa0d9ed9838369170c65de011d6b0d32f6a733549ecbb22ecc1c7

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 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 = {}
  opt[:mode] = params[:mode] if params[:mode]
  opt[:out] = params[:out] if params[:out]
  opt[:interval] = params[:interval] if params[:interval]
  opt[:raw] = params[:raw] if params[:raw]
  opt[:aggregate] = !params[:'no-aggregate']

  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.3 exe/stackprof-run