require 'celluloid'
require "benchmark"
require 'open-uri'
delay_seconds = [4,4,4,4,4,4]
BASE_URL = "http://slowapi.com/delay"
class Crawler
include Celluloid
def read(delay)
url = "#{BASE_URL}/#{delay}"
open(url) { |x| x.read }
end
end
pool = Crawler.pool(size: delay_seconds.length)
time = Benchmark.measure do
crawlers = delay_seconds.map do |delay|
begin
pool.future(:read, delay)
rescue DeadActorError, MailboxError
end
end
result = crawlers.compact.map { |crawler| crawler.value rescue nil }
end
#result.each_with_index {|i,v| puts "Response ##{v}: #{i}"}
puts time