Created
October 8, 2015 17:42
-
-
Save simon0191/16eb342a7ecbf6e23a95 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 'thread' | |
require 'pp' | |
def download_time(url) | |
`curl --silent -w "%{time_total}" --output /dev/null #{url}`.to_f | |
end | |
def compare_download_times(urls,opts={}) | |
tests = opts[:tests] || 1000 | |
concurrency = opts[:concurrency] || 1 | |
results = {} | |
urls.each { |url| results[url] = [] } | |
threads = [] | |
pending_tests = tests | |
tests_per_thread = tests/concurrency | |
mutex = Mutex.new | |
concurrency.times do |c| | |
if c < (concurrency-1) | |
current_tests = tests_per_thread | |
else | |
current_tests = pending_tests | |
end | |
pending_tests -= current_tests | |
threads << Thread.new do | |
urls.each do |url| | |
current_tests.times do |i| | |
time = download_time(url) | |
mutex.synchronize do | |
results[url] << time | |
end | |
end | |
end | |
end | |
end | |
threads.each(&:join) | |
table = [] | |
results.each do |url,times| | |
table << { url: url,times: times, sum: times.reduce(0,&:+),mean: times.reduce(0,&:+)/times.length, tests: times.length } | |
end | |
table | |
end | |
pp(compare_download_times ['https://s3.amazonaws.com/makeitreal/rodri.jpg','http://ricardorojass.github.io/images/avatar-484cbd85.jpg'], tests: 50, concurrency: 15) | |
# [{:url=>"https://s3.amazonaws.com/makeitreal/rodri.jpg", | |
# :times=> | |
# [1.479, 1.499, 1.722, 0.804, 0.879, 1.168, 0.948, 1.043, 0.972, 0.837], | |
# :sum=>11.350999999999999, | |
# :mean=>1.1351, | |
# :tests=>10}, | |
# {:url=>"http://ricardorojass.github.io/images/avatar-484cbd85.jpg", | |
# :times=> | |
# [0.898, 0.398, 0.385, 0.369, 0.381, 0.392, 0.395, 0.411, 0.384, 0.365], | |
# :sum=>4.378, | |
# :mean=>0.4378, | |
# :tests=>10}] | |
# | |
# [{:url=>"https://s3.amazonaws.com/makeitreal/rodri.jpg", | |
# :times=> | |
# [1.732,...] | |
# :sum=>82.97699999999999, | |
# :mean=>1.6595399999999998, | |
# :tests=>50}, | |
# {:url=>"http://ricardorojass.github.io/images/avatar-484cbd85.jpg", | |
# :times=> | |
# [0.964, ...] | |
# :sum=>48.01800000000002, | |
# :mean=>0.9603600000000004, | |
# :tests=>50}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment