Skip to content

Instantly share code, notes, and snippets.

@andrei512
Created February 7, 2014 16:59
Show Gist options
  • Save andrei512/8866894 to your computer and use it in GitHub Desktop.
Save andrei512/8866894 to your computer and use it in GitHub Desktop.
send 1k requests to a site
require 'open-uri'
require 'net/http'
threads = []
target = "http://www.mindcoding.ro/"
boom_time = Time.new + 5
puts "boom time = #{boom_time}"
queue = Queue.new
1000.times do |i|
threads << Thread.new(i, boom_time) do |i, boom_time|
queue << i
while Time.new < boom_time
sleep 0.05
end
begin
x = open(target).read
queue << "done with #{i} - #{x.length}"
rescue Exception => e
queue << "#{i} not working"
end
end
end
Thread.new(boom_time) do |boom_time|
while queue.empty? == false
puts queue.pop
end
while Time.new < boom_time
diff = (boom_time - Time.new)
puts "%d.." % diff
sleep 1
end
sleep 2
while queue.empty? == false
puts queue.pop
end
end.join
threads.each do |thread|
thread.join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment