Sha256: b9920623de678daf76b756aa40125c4a0c1ef343e1599f69998de008f0f3ce3a

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

module Misc
  def self.pid_alive?(pid)
    return true if Process.pid == pid
    !! Process.kill(0, pid) rescue false
  end

  def self.benchmark(repeats = 1, message = nil)
    require 'benchmark'
    res = nil
    begin
      measure = Benchmark.measure do
        repeats.times do |i|
          res = yield i
        end
      end
      if message
        puts "#{message }: #{ repeats } repeats"
      else
        puts "Benchmark for #{ repeats } repeats (#{caller.first})"
      end
      puts measure
    rescue Exception
      puts "Benchmark aborted"
      raise $!
    end
    res
  end

  def self.profile(options = {})
    require 'ruby-prof'
    profiler = RubyProf::Profile.new
    profiler.start
    begin
      res = yield
    rescue Exception
      puts "Profiling aborted"
      raise $!
    ensure
      result = profiler.stop
      printer = RubyProf::FlatPrinter.new(result)
      printer.print(STDOUT, options)
    end

    res
  end

  def self.exec_time(&block)
    start = Time.now
    eend = nil
    begin
      yield
    ensure
      eend = Time.now
    end
    eend - start
  end

  def self.wait_for_interrupt
    while true
      begin
        sleep 1
      rescue Interrupt
        break
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
scout-essentials-1.6.5 lib/scout/misc/monitor.rb
scout-essentials-1.6.4 lib/scout/misc/monitor.rb
scout-essentials-1.6.3 lib/scout/misc/monitor.rb
scout-essentials-1.6.2 lib/scout/misc/monitor.rb