Class: Speedtest::Test
- Inherits:
-
Object
- Object
- Speedtest::Test
- Defined in:
- lib/speedtest.rb
Instance Method Summary collapse
- #download ⇒ Object
- #downloadthread(url) ⇒ Object
-
#initialize(options = {}) ⇒ Test
constructor
A new instance of Test.
- #log(msg) ⇒ Object
- #pick_server ⇒ Object
- #ping(server) ⇒ Object
- #pretty_speed(speed) ⇒ Object
- #randomString(alphabet, size) ⇒ Object
- #run ⇒ Object
- #upload ⇒ Object
- #uploadthread(url, content) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Test
Returns a new instance of Test.
9 10 11 12 13 14 15 16 |
# File 'lib/speedtest.rb', line 9 def initialize( = {}) @download_runs = [:download_runs] || 4 @upload_runs = [:upload_runs] || 4 @ping_runs = [:ping_runs] || 4 @download_sizes = [:download_sizes] || [750, 1500] @upload_sizes = [:upload_sizes] || [197190, 483960] @debug = [:debug] || false end |
Instance Method Details
#download ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/speedtest.rb', line 58 def download log "\nstarting download tests:" threads = [] start_time = Time.new @download_sizes.each { |size| 1.upto(@download_runs) { |i| threads << Thread.new { |thread| downloadthread("#{@server_root}/speedtest/random#{size}x#{size}.jpg") } } } total_downloaded = 0 threads.each { |t| t.join total_downloaded += t["downloaded"] } total_time = Time.new - start_time log "Took #{total_time} seconds to download #{total_downloaded} bytes in #{threads.length} threads\n" total_downloaded * 8 / total_time end |
#downloadthread(url) ⇒ Object
52 53 54 55 56 |
# File 'lib/speedtest.rb', line 52 def downloadthread(url) log " downloading: #{url}" page = HTTParty.get(url) Thread.current["downloaded"] = page.body.length end |
#log(msg) ⇒ Object
46 47 48 49 50 |
# File 'lib/speedtest.rb', line 46 def log(msg) if @debug puts msg end end |
#pick_server ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/speedtest.rb', line 123 def pick_server page = HTTParty.get("http://www.speedtest.net/speedtest-config.php") ip,lat,lon = page.body.scan(/<client ip="([^"]*)" lat="([^"]*)" lon="([^"]*)"/)[0] orig = GeoPoint.new(lat, lon) log "Your IP: #{ip}\nYour coordinates: #{orig}\n" page = HTTParty.get("http://www.speedtest.net/speedtest-servers.php") sorted_servers=page.body.scan(/<server url="([^"]*)" lat="([^"]*)" lon="([^"]*)/).map { |x| { :distance => orig.distance(GeoPoint.new(x[1],x[2])), :url => x[0].split(/(http:\/\/.*)\/speedtest.*/)[1] } } .reject { |x| x[:url].nil? } # reject 'servers' without a domain .sort_by { |x| x[:distance] } # sort the nearest 10 by download latency latency_sorted_servers = sorted_servers[0..9].map { |x| { :latency => ping(x[:url]), :url => x[:url] }}.sort_by { |x| x[:latency] } selected = latency_sorted_servers[0] log "Automatically selected server: #{selected[:url]} - #{selected[:latency]} ms" selected end |
#ping(server) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/speedtest.rb', line 149 def ping(server) times = [] 1.upto(@ping_runs) { start = Time.new begin page = HTTParty.get("#{server}/speedtest/latency.txt") times << Time.new - start rescue Timeout::Error times << 999999 rescue Net::HTTPNotFound times << 999999 end } times.sort times[1, @ping_runs].inject(:+) * 1000 / @ping_runs # average in milliseconds end |
#pretty_speed(speed) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/speedtest.rb', line 36 def pretty_speed(speed) units = ["bps", "Kbps", "Mbps", "Gbps", "Tbps"] i = 0 while speed > 1024 speed /= 1024 i += 1 end "%.2f #{units[i]}" % speed end |
#randomString(alphabet, size) ⇒ Object
88 89 90 |
# File 'lib/speedtest.rb', line 88 def randomString(alphabet, size) (1.upto(size)).map { alphabet[rand(alphabet.length)] }.join end |
#run ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/speedtest.rb', line 18 def run() server = pick_server @server_root = server[:url] log "Server #{@server_root}" latency = server[:latency] download_rate = download log "Download: #{pretty_speed download_rate}" upload_rate = upload log "Upload: #{pretty_speed upload_rate}" Result.new(:server => @server_root, :latency => latency, :download_rate => download_rate, :pretty_download_rate => pretty_speed(download_rate), :pretty_upload_rate => pretty_speed(upload_rate), :upload_rate => upload_rate) end |
#upload ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/speedtest.rb', line 92 def upload log "\nstarting upload tests:" data = [] @upload_sizes.each { |size| 1.upto(@upload_runs) { data << randomString(('A'..'Z').to_a, size) } } threads = [] start_time = Time.new threads = data.map { |data| Thread.new(data) { |content| log " uploading size #{content.size}: #{@server_root}/speedtest/upload.php" uploadthread("#{@server_root}/speedtest/upload.php", content) } } total_uploaded = 0 threads.each { |t| t.join total_uploaded += t["uploaded"] } total_time = Time.new - start_time log "Took #{total_time} seconds to upload #{total_uploaded} bytes in #{threads.length} threads\n" # bytes to bits / time = bps total_uploaded * 8 / total_time end |
#uploadthread(url, content) ⇒ Object
83 84 85 86 |
# File 'lib/speedtest.rb', line 83 def uploadthread(url, content) page = HTTParty.post(url, :body => { "content" => content }) Thread.current["uploaded"] = page.body.split('=')[1].to_i end |