Skip to content

Instantly share code, notes, and snippets.

@zanker
Last active June 9, 2017 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zanker/65cd09c8a80d322f4c6080edc67f4bc4 to your computer and use it in GitHub Desktop.
Save zanker/65cd09c8a80d322f4c6080edc67f4bc4 to your computer and use it in GitHub Desktop.
require 'http'
started_at = Time.now
threads = 10.times.map do |i|
sleep 0.1
Thread.new do
begin
started_at = Time.now
response = HTTP.get("http://localhost:8001/#{i}")
[response.respond_to?(:code) ? response.code : -1, (Time.now - started_at), response.body.to_s]
rescue HTTP::ConnectionError => ex
[:conn_error]
rescue => ex
puts "#{ex.class}: #{ex.message}"
[:failed]
end
end
end
time_taken = []
threads.each_with_index do |thread, id|
code, results, body = thread.value
puts "[#{id}] #{code} - #{results} seconds elapsed"
time_taken << results if results
end
puts "Took #{(Time.now - started_at).round(4)} total, avg of #{(time_taken.inject(:+) / time_taken.length).round(4)} in thread time, max #{time_taken.max}, min #{time_taken.min}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment