speed-test 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in speed.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Guillermo Álvarez
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Speed
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'speed'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install speed
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env ruby
2
+ require 'socket'
3
+
4
+ SIZE=1024*1024
5
+ File.open('/dev/urandom') { |f| DATA=f.read(SIZE) }
6
+
7
+ CFG_FILE=File.expand_path("~/.speed_test_host")
8
+
9
+ host = nil
10
+ if ARGV[0]
11
+ host = ARGV[0]
12
+ File.open(CFG_FILE,"w"){|f| f.write(ARGV[0]) }
13
+ end
14
+
15
+ if !host && File.file?(CFG_FILE)
16
+ host = File.read(CFG_FILE)
17
+ end
18
+
19
+
20
+ if !host
21
+ puts "Usage: speed [host] - After the first usage the host will be saved in ~/.speed_test_host and not asked again"
22
+ exit -1
23
+ end
24
+
25
+
26
+ class Speed
27
+ def initialize(speed)
28
+ @speed = speed
29
+ end
30
+
31
+ def Bps ; @speed ; end
32
+ def bps ; @speed*8 ; end
33
+ def kBps ; @speed/1024 ; end
34
+ def kbps ; @speed*8/1024 ; end
35
+ def mBps ; @speed/(1024*1024) ; end
36
+ def mbps ; @speed*8/(1024*1024) ; end
37
+
38
+ def to_s
39
+ case @speed
40
+ when 0..1024
41
+ "%.3f bps" % [bps]
42
+ when 1024..(1024*1024)
43
+ "%.3f kbps" % [kbps]
44
+ else
45
+ "%.3f mbps" % [mbps]
46
+ end
47
+ end
48
+
49
+ # speed in bytes per second
50
+ def to_i
51
+ @speed
52
+ end
53
+
54
+ def bps
55
+ @speed * 8
56
+ end
57
+ end
58
+
59
+
60
+ def calculate_speed
61
+ t1 = Time.now
62
+ yield
63
+ t2 = Time.now
64
+ Speed.new(SIZE/(t2-t1))
65
+ ensure
66
+ end
67
+
68
+ begin
69
+ download_speed = calculate_speed do
70
+ socket = TCPSocket.new(host,16000)
71
+ socket.read(SIZE)
72
+ end
73
+
74
+ upload_speed = calculate_speed do
75
+ socket = TCPSocket.new(host,16001)
76
+ socket.write(DATA)
77
+ end
78
+ rescue Errno::ECONNREFUSED
79
+ $stderr.puts "Impossible to connect to #{host}. Is the daemon running?"
80
+ exit
81
+ end
82
+
83
+
84
+ puts "#{download_speed.to_s} / #{upload_speed.to_s}"
85
+
@@ -0,0 +1,115 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ require 'daemons'
5
+
6
+ Daemons.run_proc('speed-server') do
7
+ require 'socket'
8
+ require 'syslog'
9
+ require 'timeout'
10
+ LOG = Syslog.open("speed-server", Syslog::LOG_CONS | Syslog::LOG_NDELAY | Syslog::LOG_PID, Syslog::LOG_DAEMON)
11
+ def info(*args)
12
+ LOG.log(Syslog::LOG_INFO,*args)
13
+ end
14
+
15
+ SIZE=1024*1024
16
+ File.open('/dev/urandom') { |f| DATA=f.read(SIZE) }
17
+
18
+ download_server = TCPServer.new(16000) and info("Download Server listening at 16000")
19
+ upload_server = TCPServer.new(16001) and info("Upload Server listening at 16001")
20
+
21
+ def shutdown
22
+ download_server.shutdown
23
+ info("Downlod server shutdown")
24
+ upload_server.shutdown
25
+ info("Upload server shutdown")
26
+ exit
27
+ end
28
+
29
+ trap("INT"){ shutdown }
30
+ trap("QUIT"){ shutdown }
31
+ trap("TERM"){ shutdown }
32
+
33
+ def process_client
34
+ pid = fork do
35
+ yield
36
+ exit!
37
+ end
38
+ Process.detach(pid)
39
+ end
40
+
41
+ def calculate_speed
42
+ t1 = Time.now
43
+ yield
44
+ t2 = Time.now
45
+ Speed.new(SIZE/(t2-t1))
46
+ ensure
47
+ end
48
+
49
+ class Speed
50
+ def initialize(speed)
51
+ @speed = speed
52
+ end
53
+
54
+ def Bps ; @speed ; end
55
+ def bps ; @speed*8 ; end
56
+ def kBps ; @speed/1024 ; end
57
+ def kbps ; @speed*8/1024 ; end
58
+ def mBps ; @speed/(1024*1024) ; end
59
+ def mbps ; @speed*8/(1024*1024) ; end
60
+
61
+ def to_s
62
+ case @speed
63
+ when 0..1024
64
+ "%.3f bps" % [bps]
65
+ when 1024..(1024*1024)
66
+ "%.3f kbps" % [kbps]
67
+ else
68
+ "%.3f mbps" % [mbps]
69
+ end
70
+ end
71
+
72
+ # speed in bytes per second
73
+ def to_i
74
+ @speed
75
+ end
76
+
77
+ def bps
78
+ @speed * 8
79
+ end
80
+ end
81
+
82
+ loop do
83
+ begin
84
+ events = IO.select([download_server, upload_server])
85
+ events.first.each do |event|
86
+ case event
87
+ when download_server
88
+ process_client do
89
+ client = download_server.accept
90
+ remote_ip = client.remote_address.ip_address
91
+
92
+ speed = calculate_speed do
93
+ client.write(DATA)
94
+ client.close
95
+ end
96
+ info "#{remote_ip} Download speed: #{speed}"
97
+ end
98
+ when upload_server
99
+ process_client do
100
+ client = upload_server.accept
101
+ remote_ip = client.remote_address.ip_address
102
+ speed = calculate_speed do
103
+ client.read(SIZE)
104
+ client.close
105
+ end
106
+ info "#{remote_ip} Upload speed: #{speed}"
107
+ end
108
+ else
109
+ raise event.to_s
110
+ end
111
+ end
112
+ end
113
+ end
114
+
115
+ end
@@ -0,0 +1,5 @@
1
+ require "speed/version"
2
+
3
+ module Speed
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,3 @@
1
+ module Speed
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/speed/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Guillermo Álvarez"]
6
+ gem.email = ["guillermo@cientifico.net"]
7
+ gem.description = %q{Internet speed test for home ussage}
8
+ gem.summary = %q{A daemon (to be run in your servers), and a client. The main differece between netperf and iperf is that this one only have one way of operation. It download 1Mb and then upload 1Mb.}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "speed-test"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Speed::VERSION
17
+
18
+ gem.add_dependency "daemons"
19
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: speed-test
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Guillermo Álvarez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: daemons
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Internet speed test for home ussage
31
+ email:
32
+ - guillermo@cientifico.net
33
+ executables:
34
+ - speed
35
+ - speed-server
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - .gitignore
40
+ - Gemfile
41
+ - LICENSE
42
+ - README.md
43
+ - Rakefile
44
+ - bin/speed
45
+ - bin/speed-server
46
+ - lib/speed.rb
47
+ - lib/speed/version.rb
48
+ - speed-test.gemspec
49
+ homepage: ''
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.24
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: A daemon (to be run in your servers), and a client. The main differece between
73
+ netperf and iperf is that this one only have one way of operation. It download 1Mb
74
+ and then upload 1Mb.
75
+ test_files: []