lps 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -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 lps.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,7 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :test do
5
+ watch(%r{^test/test_.+\.rb$})
6
+ end
7
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Junegunn Choi
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.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ LPS: Loops Per Second
2
+ =====================
3
+
4
+ Rate-controlled loop execution.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'lps'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install lps
20
+
21
+ Usage
22
+ -----
23
+
24
+ ```ruby
25
+ # - Loops 10 times per second
26
+ # - Loops for 10 seconds
27
+ now = Time.now
28
+ LPS.freq(10).while { Time.now - now < 10 }.loop do
29
+ # do something
30
+ end
31
+
32
+ # - Loops 10 times per second
33
+ # - Loops indefinitely
34
+ LPS.freq(10).loop do
35
+ # do something
36
+ end
37
+ ```
38
+
39
+ Breaking out of the loop
40
+ ------------------------
41
+
42
+ ```ruby
43
+ LPS.freq(10).loop { break if rand(10) == 0 }
44
+ ```
45
+
46
+ Falling behind
47
+ --------------
48
+
49
+ With LPS, the given loop block is run synchronously,
50
+ which means that if the block execution takes longer than the interval for the given frequency,
51
+ (e.g. 0.01 second for 100)
52
+ it may not be possible to achieve the desired frequency.
53
+
54
+ ```ruby
55
+ 12.times.map { |i| 1 << i }.each do |ps|
56
+ cnt = 0
57
+ now = Time.now
58
+ LPS.freq(ps).while { Time.now - now <= 1 }.loop do
59
+ cnt += 1
60
+ sleep 0.01
61
+ end
62
+
63
+ puts [ps, cnt].join ' => '
64
+ end
65
+ ```
66
+
67
+ ```
68
+ 1 => 1
69
+ 2 => 2
70
+ 4 => 4
71
+ 8 => 8
72
+ 16 => 16
73
+ 32 => 32
74
+ 64 => 64
75
+ 128 => 98
76
+ 256 => 98
77
+ 512 => 99
78
+ 1024 => 97
79
+ 2048 => 98
80
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.pattern = 'test/**/test_*.rb'
7
+ test.verbose = true
8
+ end
data/lib/lps.rb ADDED
@@ -0,0 +1,60 @@
1
+ require "lps/version"
2
+
3
+ class LPS
4
+ def self.freq freq
5
+ LPS.new :freq => freq
6
+ end
7
+
8
+ def self.while &cond
9
+ LPS.new :cond => cond
10
+ end
11
+
12
+ def freq freq
13
+ LPS.new(:freq => freq, :cond => @cond)
14
+ end
15
+
16
+ def while &cond
17
+ LPS.new(:freq => @freq, :cond => cond)
18
+ end
19
+
20
+ def loop &block
21
+ ret = nil
22
+ if @freq.nil?
23
+ while @cond.call
24
+ ret = block.call
25
+ end
26
+ else
27
+ sleep_interval = 1.0 / @freq
28
+
29
+ nt = Time.now
30
+ while @cond.call
31
+ nt += sleep_interval
32
+ ret = block.call
33
+
34
+ now = Time.now
35
+ diff = nt - now
36
+
37
+ if diff > 0.01
38
+ sleep diff
39
+ elsif diff < 0
40
+ nt = now
41
+ end
42
+ end
43
+ end
44
+ ret
45
+ end
46
+
47
+ def initialize opts = {}
48
+ raise ArgumentError.new("Not a Hash") unless opts.is_a?(Hash)
49
+
50
+ @freq = opts[:freq]
51
+ raise ArgumentError.new(
52
+ "Frequency must be a positive number (or nil)") unless
53
+ @freq.nil? || (@freq.is_a?(Numeric) && @freq > 0)
54
+
55
+ @cond = opts[:cond] || proc { true }
56
+ raise ArgumentError.new("Invalid condition block") unless
57
+ @cond.is_a?(Proc)
58
+ end
59
+ end
60
+
@@ -0,0 +1,3 @@
1
+ class LPS
2
+ VERSION = '0.1.0'
3
+ end
data/lps.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/lps/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Junegunn Choi"]
6
+ gem.email = ["junegunn.c@gmail.com"]
7
+ gem.description = %q{Rate-controlled loop execution}
8
+ gem.summary = %q{Rate-controlled loop execution}
9
+ gem.homepage = "https://github.com/junegunn/lps"
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 = "lps"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = LPS::VERSION
17
+
18
+ gem.add_development_dependency 'test-unit'
19
+ gem.add_development_dependency 'guard'
20
+ gem.add_development_dependency 'guard-test'
21
+ end
data/test/test_lps.rb ADDED
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '../lib')
5
+ require 'lps'
6
+ require 'test-unit'
7
+
8
+ class TestLPS < Test::Unit::TestCase
9
+ def test_lps
10
+ duration = 3
11
+
12
+ [0.2, 10, 20].each do |ps|
13
+ cnt = 0
14
+
15
+ now = Time.now
16
+ LPS.while { Time.now - now <= duration }.freq(ps).loop { cnt += 1 }
17
+
18
+ expected = (duration * ps).to_i
19
+ # FIXME: naive assertion
20
+ assert (expected-1..expected+1).include?(cnt)
21
+ end
22
+ end
23
+
24
+ def test_no_frequency
25
+ cnt = 0
26
+ now = Time.now
27
+ LPS.while { Time.now - now <= 1 }.loop { cnt += 1 }
28
+ # FIXME: naive assertion
29
+ assert cnt > 1000
30
+ end
31
+
32
+ def test_non_positive_frequency
33
+ assert_raise(ArgumentError) { LPS.freq(0) }
34
+ assert_raise(ArgumentError) { LPS.freq(-1) }
35
+ end
36
+
37
+ def test_non_number_frequency
38
+ assert_raise(ArgumentError) { LPS.freq('freq') }
39
+ end
40
+
41
+ def test_non_proc_cond
42
+ assert_raise(ArgumentError) { LPS.while('freq') }
43
+ end
44
+
45
+ def test_return_value
46
+ cnt = 0
47
+ now = Time.now
48
+ ret = LPS.while { Time.now - now <= 1 }.loop { cnt += 1 }
49
+ assert_equal cnt, ret
50
+ end
51
+
52
+ def test_loop_break
53
+ cnt = 0
54
+ LPS.freq(100).loop { break if (cnt += 1) >= 50 }
55
+ assert_equal 50, cnt
56
+ end
57
+
58
+ def test_lps_high_freq
59
+ 20.times.map { |i| 1 << i }.each do |ps|
60
+ cnt = 0
61
+
62
+ now = Time.now
63
+ LPS.freq(ps).while { Time.now - now <= 1 }.loop { cnt += 1 }
64
+
65
+ # TODO: need assertion
66
+ puts [ps, cnt].join ' => '
67
+ end
68
+ end
69
+ end
70
+
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lps
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Junegunn Choi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: test-unit
16
+ requirement: &2155956340 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2155956340
25
+ - !ruby/object:Gem::Dependency
26
+ name: guard
27
+ requirement: &2155951960 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2155951960
36
+ - !ruby/object:Gem::Dependency
37
+ name: guard-test
38
+ requirement: &2155949980 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2155949980
47
+ description: Rate-controlled loop execution
48
+ email:
49
+ - junegunn.c@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - Guardfile
57
+ - LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - lib/lps.rb
61
+ - lib/lps/version.rb
62
+ - lps.gemspec
63
+ - test/test_lps.rb
64
+ homepage: https://github.com/junegunn/lps
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.11
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Rate-controlled loop execution
88
+ test_files:
89
+ - test/test_lps.rb