Sha256: e0104a1feb49264c43771d451efc4b092dd6276541046b6945f89fe56d871b82

Contents?: true

Size: 1.7 KB

Versions: 71

Compression:

Stored size: 1.7 KB

Contents

require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
#require 'rubygems'
#require 'rmem'

#
# Run some tests to measure the memory usage of curb, these tests require fork and ps
#
class TestCurbMemory < Test::Unit::TestCase

  def test_easy_memory
    easy_avg, easy_std = measure_object_memory(Curl::Easy)
    printf "Easy average: %.2f kilobytes +/- %.2f kilobytes\n", easy_avg.to_f, easy_std.to_f

    multi_avg, multi_std = measure_object_memory(Curl::Multi)
    printf "Multi average: %.2f kilobytes +/- %.2f kilobytes\n", multi_avg.to_f, multi_std.to_f

    # now that we have the average size of an easy handle lets see how much a multi request consumes with 10 requests
  end

  def c_avg(report)
    sum = 0
    report.each {|r| sum += r.last }
    (sum.to_f / report.size)
  end

  def c_std(report,avg)
    var = 0
    report.each {|r| var += (r.last-avg)*(r.last-avg) }
    Math.sqrt(var / (report.size-1))
  end

  def measure_object_memory(klass)
    report = []
    200.times do
      res = mem_check do
        obj = klass.new
      end
      report << res
    end
    avg = c_avg(report)
    std = c_std(report,avg)
    [avg,std]
  end

  def mem_check
    # see: http://gist.github.com/264060 for inspiration of ps command line
    rd, wr = IO.pipe
    memory_usage = `ps -o rss= -p #{Process.pid}`.to_i # in kilobytes
    fork do
      before = `ps -o rss= -p #{Process.pid}`.to_i # in kilobytes
      rd.close
      yield
      after = `ps -o rss= -p #{Process.pid}`.to_i # in kilobytes
      wr.write((after - before))
      wr.flush
      wr.close
    end
    wr.close
    total = rd.read.to_i
    rd.close
    Process.wait
    # return the delta and the total
    [memory_usage, total]
  end
end

Version data entries

71 entries across 71 versions & 6 rubygems

Version Path
curb-1.0.9 tests/mem_check.rb
curb-1.0.8 tests/mem_check.rb
curb-1.0.7 tests/mem_check.rb
curb-1.0.6 tests/mem_check.rb
curb-1.0.5 tests/mem_check.rb
curb-1.0.4 tests/mem_check.rb
curb-1.0.3 tests/mem_check.rb
curb-1.0.2 tests/mem_check.rb
curb-1.0.1 tests/mem_check.rb
curb-1.0.0 tests/mem_check.rb
curb-0.9.11 tests/mem_check.rb
curb-0.9.10 tests/mem_check.rb
curb-0.9.9 tests/mem_check.rb
curb-0.9.8 tests/mem_check.rb
curb-0.9.7 tests/mem_check.rb
curb-0.9.6 tests/mem_check.rb
curb-0.9.5 tests/mem_check.rb
curb-0.9.4 tests/mem_check.rb
curb-0.9.3 tests/mem_check.rb
curb-0.9.2 tests/mem_check.rb