require File.dirname(__FILE__) + '/test_helper.rb' class TestEvdispatch < Test::Unit::TestCase def test_object_test # start up the test server pid = fork do require File.dirname(__FILE__) + '/../ext/revdispatch/server' start_test_server end sleep 1 unless ARGV[0] == 'r' d = Evdispatch::Loop.new # start the event loop thread d.start begin ObjectSpace.garbage_collect 10.times do begin start_count = ObjectSpace.each_object { } duration = run_trial(d, 10) new_count = ObjectSpace.each_object { } puts "10 trials: #{duration} seconds, new objects #{new_count - start_count}, #{new_count} - #{start_count}" end begin start_count = ObjectSpace.each_object { } duration = run_trial(d, 100) new_count = ObjectSpace.each_object { } puts "100 trials: #{duration} seconds, new objects #{new_count - start_count}, #{new_count} - #{start_count}" end end rescue => e puts e.message, e.backtrace end count = ObjectSpace.each_object { } puts "Final Total objects: #{count}" count = nil ObjectSpace.garbage_collect count = ObjectSpace.each_object { } puts "After garbage collection objects: #{count}" # sometime later you can stop the event loop d.stop ensure Process.kill('HUP', pid) unless ARGV[0] == 'r' end def request_bytes_from( d, base, amount, range ) ids = [] amount.times do|i| am = rand( range ) ids << d.request_http( base + "bytes/#{am}/" ) end ids end def request_delay_from( d, base, amount, delay ) ids = [] amount.times do|i| am = rand( delay ) ids << d.request_http( base + "delay/#{delay}/" ) end ids end def run_trial( d, trials ) ebbbase = "http://127.0.0.1:4044/" timer = Time.now ids = request_bytes_from( d, ebbbase, trials, 1000 ) ids += request_delay_from( d, ebbbase, trials, 1 ) # wait for each response puts "expecting #{ids.size} responses..." ids.each do|id| response = d.response( id ) #puts response[:name] end duration = Time.now - timer duration end end