Last active
June 9, 2017 16:04
-
-
Save zanker/65cd09c8a80d322f4c6080edc67f4bc4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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