plr-speedtest 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 917e92197eb60a1304f9caf430bb6c29c71d02d79a2b2c5b3cec0b59d98d53fe
4
+ data.tar.gz: 64494899740408ee2d7fc2d8809cdacbdc845567f0e60a5d0534996e673cfd59
5
+ SHA512:
6
+ metadata.gz: b320f647b1facbdaccf131c42b1f5e6afaa9fe8dd9aa3edd543a171a1fa94771d7bf3f273fd733305026c27e538f86ce75ce579255b46492be1278c6998423f5
7
+ data.tar.gz: 3e340043d664bace37a2f415b0248bdd1aa31a345dcf1ccf290276b96e1c058f11fd9cb7c1158d41783ccf4c2a9fd663030e1bc6b32eea2b1ae1080260ec9830
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Jerome Lacoste
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # Speedtest
2
+ A ruby gem for speedtest.net results
3
+
4
+ Adapted from https://github.com/lacostej/speedtest.rb
5
+
6
+ ## Installation
7
+ ```ruby
8
+ $ gem install speedtest
9
+ ```
10
+ or put it in your Gemfile
11
+ ```ruby
12
+ gem 'speedtest'
13
+ ```
14
+ and install with
15
+ ```ruby
16
+ $ bundle install
17
+ ```
18
+
19
+ ## Usage:
20
+ require it in your script
21
+ ```ruby
22
+ require 'logger'
23
+ require 'speedtest'
24
+ ```
25
+
26
+ Configure a new test with whatever options you want—all are optional:
27
+ * download_runs - The number of attempts to download each file
28
+ * upload_runs - The number of attempts to upload each file
29
+ * ping_runs - The number of ping attempts to establish latency
30
+ * download_sizes - an array of .jpg dimensions (must be one or more of `[350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000]`)
31
+ * upload_sizes
32
+ * logger
33
+
34
+ ```ruby
35
+ test = Speedtest::Test.new(
36
+ download_runs: 4,
37
+ upload_runs: 4,
38
+ ping_runs: 4,
39
+ download_sizes: [750, 1500],
40
+ upload_sizes: [10000, 400000],
41
+ logger: Logger.new(STDOUT)
42
+ )
43
+ => #<Speedtest::Test:0x007fac5ac9dca0 @download_runs=4, @upload_runs=4, @ping_runs=4, @download_sizes=[750, 1500], @upload_sizes=[10000, 400000], @debug=true>
44
+ ```
45
+
46
+ test.run() returns some results:
47
+ ```ruby
48
+ results = test.run
49
+ ```
50
+ With logger set, this produces:
51
+ ```ruby
52
+ Your IP: 97.126.32.16
53
+ Your coordinates: [47.4356, -122.1141]
54
+ Automatically selected server: http://lg.sea-z.fdcservers.net - 32.985 ms
55
+ Server http://lg.sea-z.fdcservers.net
56
+
57
+ starting download tests:
58
+ downloading: http://lg.sea-z.fdcservers.net/speedtest/random1500x1500.jpg
59
+ downloading: http://lg.sea-z.fdcservers.net/speedtest/random750x750.jpg
60
+ downloading: http://lg.sea-z.fdcservers.net/speedtest/random1500x1500.jpg
61
+ downloading: http://lg.sea-z.fdcservers.net/speedtest/random750x750.jpg
62
+ downloading: http://lg.sea-z.fdcservers.net/speedtest/random750x750.jpg
63
+ downloading: http://lg.sea-z.fdcservers.net/speedtest/random750x750.jpg
64
+ downloading: http://lg.sea-z.fdcservers.net/speedtest/random1500x1500.jpg
65
+ downloading: http://lg.sea-z.fdcservers.net/speedtest/random1500x1500.jpg
66
+ Took 6.10022 seconds to download 22345012 bytes in 8 threads
67
+ Download: 27.95 Mbps
68
+
69
+ starting upload tests:
70
+ uploading size 10000: http://lg.sea-z.fdcservers.net/speedtest/upload.php
71
+ uploading size 10000: http://lg.sea-z.fdcservers.net/speedtest/upload.php
72
+ uploading size 10000: http://lg.sea-z.fdcservers.net/speedtest/upload.php
73
+ uploading size 10000: http://lg.sea-z.fdcservers.net/speedtest/upload.php
74
+ uploading size 400000: http://lg.sea-z.fdcservers.net/speedtest/upload.php
75
+ uploading size 400000: http://lg.sea-z.fdcservers.net/speedtest/upload.php
76
+ uploading size 400000: http://lg.sea-z.fdcservers.net/speedtest/upload.php
77
+ uploading size 400000: http://lg.sea-z.fdcservers.net/speedtest/upload.php
78
+ Took 3.437126 seconds to upload 1644080 bytes in 8 threads
79
+ Upload: 3.65 Mbps
80
+
81
+ => #<Speedtest::Result:0x007fac5ac1e680 @server="http://lg.sea-z.fdcservers.net", @latency=32.985, @download_size=22345012, @upload_size=1644080, @download_time=6.10022, @upload_time=3.437126>
82
+ ```
83
+
84
+ ## Interesting links
85
+ * https://github.com/lacostej/speedtest.rb
86
+ * http://www.phuket-data-wizards.com/blog/2011/09/17/speedtest-vs-dslreports-analysis/
87
+ * https://github.com/fopina/pyspeedtest
88
+ * http://tech.ivkin.net/wiki/Run_Speedtest_from_command_line
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,18 @@
1
+ module Speedtest
2
+ class GeoPoint
3
+ attr_accessor :lat, :lon
4
+
5
+ def initialize(lat, lon)
6
+ @lat = Float(lat)
7
+ @lon = Float(lon)
8
+ end
9
+
10
+ def to_s
11
+ "[#{lat}, #{lon}]"
12
+ end
13
+
14
+ def distance(point)
15
+ Math.sqrt((point.lon - lon)**2 + (point.lat - lat)**2)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ module Speedtest
2
+ class Result
3
+ attr_accessor :server, :latency, :download_size, :upload_size, :download_time, :upload_time
4
+
5
+ def initialize(values = {})
6
+ @server = values[:server]
7
+ @latency = values[:latency]
8
+ @download_size = values[:download_size]
9
+ @upload_size = values[:upload_size]
10
+ @download_time = values[:download_time]
11
+ @upload_time = values[:upload_time]
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Speedtest
2
+ VERSION = "0.2.3"
3
+ end
data/lib/speedtest.rb ADDED
@@ -0,0 +1,186 @@
1
+ require 'httparty'
2
+
3
+ require_relative 'speedtest/result'
4
+ require_relative 'speedtest/geo_point'
5
+
6
+ module Speedtest
7
+ class Test
8
+
9
+ class FailedTransfer < StandardError; end
10
+
11
+ def initialize(options = {})
12
+ @download_runs = options[:download_runs] || 4
13
+ @upload_runs = options[:upload_runs] || 4
14
+ @ping_runs = options[:ping_runs] || 4
15
+ @download_sizes = options[:download_sizes] || [750, 1500]
16
+ @upload_sizes = options[:upload_sizes] || [197190, 483960]
17
+ @logger = options[:logger]
18
+ end
19
+
20
+ def run()
21
+ server = pick_server
22
+ @server_root = server[:url]
23
+ log "Server #{@server_root}"
24
+
25
+ latency = server[:latency]
26
+
27
+ download_size, download_time = download
28
+ download_rate = download_size / download_time
29
+ log "Download: #{pretty_speed download_rate}"
30
+
31
+ upload_size, upload_time = upload
32
+ upload_rate = upload_size / upload_time
33
+ log "Upload: #{pretty_speed upload_rate}"
34
+
35
+ Result.new(:server => @server_root, :latency => latency,
36
+ download_size: download_size, download_time: download_time,
37
+ upload_size: upload_size, upload_time: upload_time)
38
+ end
39
+
40
+ def pretty_speed(speed)
41
+ units = ["bps", "Kbps", "Mbps", "Gbps", "Tbps"]
42
+ i = 0
43
+ while speed > 1024
44
+ speed /= 1024
45
+ i += 1
46
+ end
47
+ "%.2f #{units[i]}" % speed
48
+ end
49
+
50
+ def log(msg)
51
+ @logger.debug msg if @logger
52
+ end
53
+
54
+ def error(msg)
55
+ @logger.error msg if @logger
56
+ end
57
+
58
+ def downloadthread(url)
59
+ log " downloading: #{url}"
60
+ page = HTTParty.get(url)
61
+ unless page.code / 100 == 2
62
+ error "GET #{url} failed with code #{page.code}"
63
+ Thread.current["error"] = true
64
+ end
65
+ Thread.current["downloaded"] = page.body.length
66
+ end
67
+
68
+ def download
69
+ log "\nstarting download tests:"
70
+ threads = []
71
+
72
+ start_time = Time.new
73
+ @download_sizes.each { |size|
74
+ 1.upto(@download_runs) { |i|
75
+ threads << Thread.new { |thread|
76
+ downloadthread("#{@server_root}/speedtest/random#{size}x#{size}.jpg")
77
+ }
78
+ }
79
+ }
80
+
81
+ total_downloaded = 0
82
+ threads.each { |t|
83
+ t.join
84
+ total_downloaded += t["downloaded"]
85
+ raise FailedTransfer.new("Download failed.") if t["error"] == true
86
+ }
87
+
88
+ total_time = Time.new - start_time
89
+ log "Took #{total_time} seconds to download #{total_downloaded} bytes in #{threads.length} threads\n"
90
+
91
+ [ total_downloaded * 8, total_time ]
92
+ end
93
+
94
+ def uploadthread(url, content)
95
+ page = HTTParty.post(url, :body => { "content" => content })
96
+ unless page.code / 100 == 2
97
+ error "GET #{url} failed with code #{page.code}"
98
+ Thread.current["error"] = true
99
+ end
100
+ Thread.current["uploaded"] = page.body.split('=')[1].to_i
101
+ end
102
+
103
+ def randomString(alphabet, size)
104
+ (1.upto(size)).map { alphabet[rand(alphabet.length)] }.join
105
+ end
106
+
107
+ def upload
108
+ log "\nstarting upload tests:"
109
+
110
+ data = []
111
+ @upload_sizes.each { |size|
112
+ 1.upto(@upload_runs) {
113
+ data << randomString(('A'..'Z').to_a, size)
114
+ }
115
+ }
116
+
117
+ threads = []
118
+ start_time = Time.new
119
+ threads = data.map { |data|
120
+ Thread.new(data) { |content|
121
+ log " uploading size #{content.size}: #{@server_root}/speedtest/upload.php"
122
+ uploadthread("#{@server_root}/speedtest/upload.php", content)
123
+ }
124
+ }
125
+
126
+ total_uploaded = 0
127
+ threads.each { |t|
128
+ t.join
129
+ total_uploaded += t["uploaded"]
130
+ raise FailedTransfer.new("Upload failed.") if t["error"] == true
131
+ }
132
+ total_time = Time.new - start_time
133
+ log "Took #{total_time} seconds to upload #{total_uploaded} bytes in #{threads.length} threads\n"
134
+
135
+ # bytes to bits / time = bps
136
+ [ total_uploaded * 8, total_time ]
137
+ end
138
+
139
+ def pick_server
140
+ page = HTTParty.get("http://www.speedtest.net/speedtest-config.php")
141
+ ip,lat,lon = page.body.scan(/<client ip="([^"]*)" lat="([^"]*)" lon="([^"]*)"/)[0]
142
+ orig = GeoPoint.new(lat, lon)
143
+ log "Your IP: #{ip}\nYour coordinates: #{orig}\n"
144
+
145
+ page = HTTParty.get("http://www.speedtest.net/speedtest-servers.php")
146
+ sorted_servers=page.body.scan(/<server url="([^"]*)" lat="([^"]*)" lon="([^"]*)/).map { |x| {
147
+ :distance => orig.distance(GeoPoint.new(x[1],x[2])),
148
+ :url => x[0].split(/(http:\/\/.*)\/speedtest.*/)[1]
149
+ } }
150
+ .reject { |x| x[:url].nil? } # reject 'servers' without a domain
151
+ .sort_by { |x| x[:distance] }
152
+
153
+ # sort the nearest 10 by download latency
154
+ latency_sorted_servers = sorted_servers[0..9].map { |x|
155
+ {
156
+ :latency => ping(x[:url]),
157
+ :url => x[:url]
158
+ }}.sort_by { |x| x[:latency] }
159
+ selected = latency_sorted_servers[0]
160
+ log "Automatically selected server: #{selected[:url]} - #{selected[:latency]} ms"
161
+
162
+ selected
163
+ end
164
+
165
+ def ping(server)
166
+ times = []
167
+ 1.upto(@ping_runs) {
168
+ start = Time.new
169
+ begin
170
+ page = HTTParty.get("#{server}/speedtest/latency.txt")
171
+ times << Time.new - start
172
+ rescue Timeout::Error, Net::HTTPNotFound, Errno::ENETUNREACH => e
173
+ log "#{e.class} #{e}"
174
+ times << 999999
175
+ end
176
+ }
177
+ times.sort
178
+ times[1, @ping_runs].inject(:+) * 1000 / @ping_runs # average in milliseconds
179
+ end
180
+ end
181
+ end
182
+
183
+ if __FILE__ == $PROGRAM_NAME
184
+ x = Speedtest::Test.new(ARGV)
185
+ x.run
186
+ end
data/speedtest.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ lib = File.expand_path('../lib/', __FILE__)
4
+ $:.unshift lib unless $:.include?(lib)
5
+
6
+ require 'speedtest/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = "plr-speedtest"
10
+ spec.version = Speedtest::VERSION
11
+ spec.authors = ["Pete Myron", "Philippe Le Rohellec"]
12
+ spec.email = ["pete.myron@gmail.com", "philippe.lerohellec@gmail.com"]
13
+
14
+ spec.summary = %q{Gemmed version of lacostej's speedtest.rb script - Test your speed with speedtest.net!}
15
+ spec.description = %q{Gemmed version of lacostej's speedtest.rb script @ https://github.com/lacostej/speedtest.rb - Test your speed with speedtest.net!}
16
+ spec.homepage = "https://github.com/petemyron/speedtest"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "httparty", "~> 0.13"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.11"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: plr-speedtest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.3
5
+ platform: ruby
6
+ authors:
7
+ - Pete Myron
8
+ - Philippe Le Rohellec
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2018-05-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '0.13'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '0.13'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.11'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.11'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '10.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '10.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.0'
70
+ description: Gemmed version of lacostej's speedtest.rb script @ https://github.com/lacostej/speedtest.rb
71
+ - Test your speed with speedtest.net!
72
+ email:
73
+ - pete.myron@gmail.com
74
+ - philippe.lerohellec@gmail.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".gitignore"
80
+ - ".rspec"
81
+ - ".travis.yml"
82
+ - Gemfile
83
+ - LICENSE
84
+ - README.md
85
+ - Rakefile
86
+ - lib/speedtest.rb
87
+ - lib/speedtest/geo_point.rb
88
+ - lib/speedtest/result.rb
89
+ - lib/speedtest/version.rb
90
+ - speedtest.gemspec
91
+ homepage: https://github.com/petemyron/speedtest
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.7.6
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Gemmed version of lacostej's speedtest.rb script - Test your speed with speedtest.net!
115
+ test_files: []