Sha256: 9f0241916c5cb96f3cb5ff934de593371f1cbb76568cdbec73b119a1bb151c83

Contents?: true

Size: 1.86 KB

Versions: 13

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 file_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(file)
    store_result(:file, "size", File.size(file) / 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

13 entries across 13 versions & 1 rubygems

Version Path
RedCloth-4.2.4-x86-mswin32-60 test/ragel_profiler.rb
RedCloth-4.2.4-x86-mingw32 test/ragel_profiler.rb
RedCloth-4.2.4-java test/ragel_profiler.rb
RedCloth-4.2.4 test/ragel_profiler.rb
RedCloth-4.2.4.pre3-x86-mswin32-60 test/ragel_profiler.rb
RedCloth-4.2.4.pre3-x86-mingw32 test/ragel_profiler.rb
RedCloth-4.2.4.pre3-java test/ragel_profiler.rb
RedCloth-4.2.4.pre3 test/ragel_profiler.rb
RedCloth-4.2.4.pre2-x86-mswin32-60 test/ragel_profiler.rb
RedCloth-4.2.4.pre2-x86-mingw32 test/ragel_profiler.rb
RedCloth-4.2.4.pre2-java test/ragel_profiler.rb
RedCloth-4.2.4.pre2 test/ragel_profiler.rb
RedCloth-4.2.4.pre1 test/ragel_profiler.rb