1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
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
|