Sha256: 9bbd4cded1bde3978d9062ecdd62aab991b4137a87e1edf8c6521016f8e16baf

Contents?: true

Size: 1.86 KB

Versions: 29

Compression:

Stored size: 1.86 KB

Contents

class RagelProfiler
  MEM_CONVERSION = 1024
  
  COMMANDS =  { :compile => %w(ragel rlgen-cd gcc-4.0 gnumake cc1),
                :test    => %w(ruby) }
                
  FIELDS = %w(compile_time compile_max_rss test_time test_max_rss ext_so_size)
                
  @@results = {}

  def initialize(name)
    @name = name
    @@results[name] = []
  end
  
  def measure(type)
    raise "not a valid type" unless COMMANDS.keys.include?(type)
    regex = COMMANDS[type].map {|c| Regexp.escape(c) }.join("|")
    t = Thread.new do
      Thread.current[:max] = 0
      loop do
        Thread.current[:max] = [run(regex), Thread.current[:max]].max
        sleep 0.5
      end
    end
    begin_time = Time.now
    yield
    total_time = Time.now - begin_time

    t.kill
    store_result(type, "time", total_time)
    store_result(type, "max_rss", t[:max])
  end
  
  def ext_size(ext_so)
    store_result(:ext_so, "size", File.size(ext_so) / MEM_CONVERSION)
  end
  
  def self.results
    out = []
    out << "name\t" + FIELDS.join("\t")
    @@results.each do |name, results|
      out << [name, results ].flatten.join("\t")
    end
    out.join("\n")
  end
  
  private
  
  def store_result(type, metric, value)
    index = FIELDS.index("#{type.to_s}_#{metric}")
    @@results[@name][index] = "%.2f" % value
  end
  
  def run(ps_regex)
    ps_command   = "ps axucww"
    ps_output    = `#{ps_command}`
    fields       = ps_output.to_a.first.downcase.split
    memory_index = fields.index("rss")
    pid_index    = fields.index("pid")
    ppid_index   = fields.index("ppid")
    total        = ps_output.grep(/(#{ps_regex})\s+$/i).map do |com|
                     Float(com.split[memory_index]).abs
                   end.inject(0) { |s,v| s += v }
    if total
      return total/MEM_CONVERSION
    else
      STDERR.puts "Command not found. No processes found matching #{ps_regex}."
    end

  end

end

Version data entries

29 entries across 29 versions & 3 rubygems

Version Path
jeremy-RedCloth-4.1.1 extras/ragel_profiler.rb
BBRedCloth-0.8.8 extras/ragel_profiler.rb
BBRedCloth-0.8.7 extras/ragel_profiler.rb
BBRedCloth-0.8.6 extras/ragel_profiler.rb
BBRedCloth-0.8.5 extras/ragel_profiler.rb
BBRedCloth-0.8.4 extras/ragel_profiler.rb
BBRedCloth-0.8.3 extras/ragel_profiler.rb
BBRedCloth-0.8.2 extras/ragel_profiler.rb
BBRedCloth-0.8.1 extras/ragel_profiler.rb
BBRedCloth-0.8.0 extras/ragel_profiler.rb
RedCloth-4.0.3-x86-mswin32-60 extras/ragel_profiler.rb
RedCloth-4.0.1 extras/ragel_profiler.rb
RedCloth-4.0.3 extras/ragel_profiler.rb
RedCloth-4.0.0 extras/ragel_profiler.rb
RedCloth-4.0.0-x86-mswin32-60 extras/ragel_profiler.rb
RedCloth-4.0.1-x86-mswin32-60 extras/ragel_profiler.rb
RedCloth-4.0.2 extras/ragel_profiler.rb
RedCloth-4.0.2-x86-mswin32-60 extras/ragel_profiler.rb
RedCloth-4.1.0 extras/ragel_profiler.rb
RedCloth-4.1.0-universal-java extras/ragel_profiler.rb