glutton_ratelimit 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -5
- data/LICENSE +23 -23
- data/README.rdoc +129 -124
- data/Rakefile +52 -52
- data/VERSION +1 -1
- data/examples/limit_instance_methods.rb +33 -33
- data/examples/simple_manual.rb +31 -29
- data/glutton_ratelimit.gemspec +64 -64
- data/lib/glutton_ratelimit.rb +38 -29
- data/lib/glutton_ratelimit/averaged_throttle.rb +44 -44
- data/lib/glutton_ratelimit/bursty_ring_buffer.rb +24 -24
- data/lib/glutton_ratelimit/bursty_token_bucket.rb +26 -26
- data/test/helper.rb +20 -21
- data/test/test_glutton_ratelimit_averaged_throttle.rb +15 -15
- data/test/test_glutton_ratelimit_bursty_ring_buffer.rb +15 -15
- data/test/test_glutton_ratelimit_bursty_token_bucket.rb +15 -15
- data/test/testing_module.rb +69 -67
- metadata +14 -18
- data/.gitignore +0 -21
data/test/helper.rb
CHANGED
@@ -1,21 +1,20 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'test/unit'
|
3
|
-
|
4
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
-
require 'glutton_ratelimit'
|
7
|
-
|
8
|
-
class Test::Unit::TestCase
|
9
|
-
end
|
10
|
-
|
11
|
-
def timed_run ratelimiter, passes = 1
|
12
|
-
before_last_invocation = 0
|
13
|
-
start_time = Time.now
|
14
|
-
(passes * ratelimiter.executions + 1)
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
require 'glutton_ratelimit'
|
7
|
+
|
8
|
+
class Test::Unit::TestCase
|
9
|
+
end
|
10
|
+
|
11
|
+
def timed_run ratelimiter, passes = 1
|
12
|
+
before_last_invocation = 0
|
13
|
+
start_time = Time.now
|
14
|
+
ratelimiter.times(passes * ratelimiter.executions + 1) do
|
15
|
+
before_last_invocation = Time.now
|
16
|
+
yield
|
17
|
+
end
|
18
|
+
before_last_invocation - start_time
|
19
|
+
end
|
20
|
+
|
@@ -1,15 +1,15 @@
|
|
1
|
-
require 'helper'
|
2
|
-
require 'testing_module.rb'
|
3
|
-
|
4
|
-
class TestGluttonRatelimitAveragedThrottle < Test::Unit::TestCase
|
5
|
-
include TestingModule
|
6
|
-
|
7
|
-
def setup
|
8
|
-
@testClass = GluttonRatelimit::AveragedThrottle
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_object_creations
|
12
|
-
rl = @testClass.new 1, 1
|
13
|
-
assert_instance_of GluttonRatelimit::AveragedThrottle, rl
|
14
|
-
end
|
15
|
-
end
|
1
|
+
require 'helper'
|
2
|
+
require 'testing_module.rb'
|
3
|
+
|
4
|
+
class TestGluttonRatelimitAveragedThrottle < Test::Unit::TestCase
|
5
|
+
include TestingModule
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@testClass = GluttonRatelimit::AveragedThrottle
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_object_creations
|
12
|
+
rl = @testClass.new 1, 1
|
13
|
+
assert_instance_of GluttonRatelimit::AveragedThrottle, rl
|
14
|
+
end
|
15
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
require 'helper'
|
2
|
-
require 'testing_module.rb'
|
3
|
-
|
4
|
-
class TestGluttonRatelimitBurstyRingBuffer < Test::Unit::TestCase
|
5
|
-
include TestingModule
|
6
|
-
|
7
|
-
def setup
|
8
|
-
@testClass = GluttonRatelimit::BurstyRingBuffer
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_object_creations
|
12
|
-
rl = @testClass.new 1, 1
|
13
|
-
assert_instance_of GluttonRatelimit::BurstyRingBuffer, rl
|
14
|
-
end
|
15
|
-
end
|
1
|
+
require 'helper'
|
2
|
+
require 'testing_module.rb'
|
3
|
+
|
4
|
+
class TestGluttonRatelimitBurstyRingBuffer < Test::Unit::TestCase
|
5
|
+
include TestingModule
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@testClass = GluttonRatelimit::BurstyRingBuffer
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_object_creations
|
12
|
+
rl = @testClass.new 1, 1
|
13
|
+
assert_instance_of GluttonRatelimit::BurstyRingBuffer, rl
|
14
|
+
end
|
15
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
require 'helper'
|
2
|
-
require 'testing_module.rb'
|
3
|
-
|
4
|
-
class TestGluttonRatelimitBurtyTokenBucket < Test::Unit::TestCase
|
5
|
-
include TestingModule
|
6
|
-
|
7
|
-
def setup
|
8
|
-
@testClass = GluttonRatelimit::BurstyTokenBucket
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_object_creations
|
12
|
-
rl = @testClass.new 1, 1
|
13
|
-
assert_instance_of GluttonRatelimit::BurstyTokenBucket, rl
|
14
|
-
end
|
15
|
-
end
|
1
|
+
require 'helper'
|
2
|
+
require 'testing_module.rb'
|
3
|
+
|
4
|
+
class TestGluttonRatelimitBurtyTokenBucket < Test::Unit::TestCase
|
5
|
+
include TestingModule
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@testClass = GluttonRatelimit::BurstyTokenBucket
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_object_creations
|
12
|
+
rl = @testClass.new 1, 1
|
13
|
+
assert_instance_of GluttonRatelimit::BurstyTokenBucket, rl
|
14
|
+
end
|
15
|
+
end
|
data/test/testing_module.rb
CHANGED
@@ -1,67 +1,69 @@
|
|
1
|
-
module TestingModule
|
2
|
-
# This module assumes use as a mixin within
|
3
|
-
# a test class which defines @testClass.
|
4
|
-
|
5
|
-
def test_120_tasks_every_second_with_ms_task
|
6
|
-
min_time = 1
|
7
|
-
rl = @testClass.new 120, min_time
|
8
|
-
delta = timed_run(rl) { sleep 0.001 }
|
9
|
-
#puts "#{delta} >? #{min_time}"
|
10
|
-
assert delta
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_120_tasks_every_quarter_second_with_ms_task
|
14
|
-
min_time = 0.25
|
15
|
-
rl = @testClass.new 120, min_time
|
16
|
-
delta = timed_run(rl) { sleep 0.001 }
|
17
|
-
#puts "#{delta} >? #{min_time}"
|
18
|
-
assert delta
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_120_tasks_every_second_with_cenisecond_task
|
22
|
-
min_time = 1
|
23
|
-
rl = @testClass.new 120, min_time
|
24
|
-
delta = timed_run(rl) { sleep 0.01 }
|
25
|
-
#puts "#{delta} >? #{min_time}"
|
26
|
-
assert delta
|
27
|
-
end
|
28
|
-
|
29
|
-
def
|
30
|
-
min_time = 1
|
31
|
-
rl = @testClass.new 50, min_time
|
32
|
-
delta = timed_run(rl) { sleep 0 }
|
33
|
-
#puts "#{delta} >? #{min_time}"
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
end
|
1
|
+
module TestingModule
|
2
|
+
# This module assumes use as a mixin within
|
3
|
+
# a test class which defines @testClass.
|
4
|
+
|
5
|
+
def test_120_tasks_every_second_with_ms_task
|
6
|
+
min_time = 1
|
7
|
+
rl = @testClass.new 120, min_time
|
8
|
+
delta = timed_run(rl) { sleep 0.001 }
|
9
|
+
#puts "#{delta} >? #{min_time}"
|
10
|
+
assert delta >= min_time
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_120_tasks_every_quarter_second_with_ms_task
|
14
|
+
min_time = 0.25
|
15
|
+
rl = @testClass.new 120, min_time
|
16
|
+
delta = timed_run(rl) { sleep 0.001 }
|
17
|
+
#puts "#{delta} >? #{min_time}"
|
18
|
+
assert delta >= min_time
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_120_tasks_every_second_with_cenisecond_task
|
22
|
+
min_time = 1
|
23
|
+
rl = @testClass.new 120, min_time
|
24
|
+
delta = timed_run(rl) { sleep 0.01 }
|
25
|
+
#puts "#{delta} >? #{min_time}"
|
26
|
+
assert delta >= min_time
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_50_tasks_every_second_with_no_task
|
30
|
+
min_time = 1
|
31
|
+
rl = @testClass.new 50, min_time
|
32
|
+
delta = timed_run(rl) { sleep 0 }
|
33
|
+
#puts "#{delta} >? #{min_time}"
|
34
|
+
# Allowed to be off by 0.5% since there is no task time.
|
35
|
+
assert((delta / min_time) > 0.995)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_1_task_every_1_seconds_with_1_second_task
|
39
|
+
min_time = 1
|
40
|
+
rl = @testClass.new 1, min_time
|
41
|
+
delta = timed_run(rl) { sleep 1 }
|
42
|
+
#puts "#{delta} >? #{min_time}"
|
43
|
+
assert delta >= min_time
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_5_tasks_every_quarter_second_four_runs_with_ms_task
|
47
|
+
min_time = 0.25
|
48
|
+
rl = @testClass.new 1, min_time
|
49
|
+
delta = timed_run(rl, 4) { sleep 0.001 }
|
50
|
+
#puts "#{delta} >? #{min_time}"
|
51
|
+
assert delta >= min_time * 4
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_120_tasks_every_half_second_two_runs_with_no_task
|
55
|
+
min_time = 0.5
|
56
|
+
rl = @testClass.new 120, min_time
|
57
|
+
delta = timed_run(rl, 2) { sleep 0 }
|
58
|
+
# Allowed to be off by 0.5% since there is no task time.
|
59
|
+
assert((delta / (min_time * 2)) > 0.995)
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_10_tasks_every_1_seconds_two_runs_with_short_rnd_task
|
63
|
+
min_time = 1
|
64
|
+
rl = @testClass.new 10, min_time
|
65
|
+
delta = timed_run(rl, 2) { sleep(0.2 * rand) }
|
66
|
+
assert delta >= min_time * 2
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: glutton_ratelimit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
5
6
|
segments:
|
6
7
|
- 0
|
7
|
-
-
|
8
|
+
- 2
|
8
9
|
- 0
|
9
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Wally Glutton
|
@@ -14,8 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date:
|
18
|
-
default_executable:
|
18
|
+
date: 2011-06-30 00:00:00 Z
|
19
19
|
dependencies: []
|
20
20
|
|
21
21
|
description: A Ruby library for limiting the number of times a method can be invoked within a specified time period.
|
@@ -29,7 +29,6 @@ extra_rdoc_files:
|
|
29
29
|
- README.rdoc
|
30
30
|
files:
|
31
31
|
- .document
|
32
|
-
- .gitignore
|
33
32
|
- LICENSE
|
34
33
|
- README.rdoc
|
35
34
|
- Rakefile
|
@@ -46,41 +45,38 @@ files:
|
|
46
45
|
- test/test_glutton_ratelimit_bursty_ring_buffer.rb
|
47
46
|
- test/test_glutton_ratelimit_bursty_token_bucket.rb
|
48
47
|
- test/testing_module.rb
|
49
|
-
has_rdoc: true
|
50
48
|
homepage: http://github.com/stungeye/glutton_ratelimit
|
51
49
|
licenses: []
|
52
50
|
|
53
51
|
post_install_message:
|
54
|
-
rdoc_options:
|
55
|
-
|
52
|
+
rdoc_options: []
|
53
|
+
|
56
54
|
require_paths:
|
57
55
|
- lib
|
58
56
|
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
59
58
|
requirements:
|
60
59
|
- - ">="
|
61
60
|
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
62
|
segments:
|
63
63
|
- 0
|
64
64
|
version: "0"
|
65
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
66
67
|
requirements:
|
67
68
|
- - ">="
|
68
69
|
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
69
71
|
segments:
|
70
72
|
- 0
|
71
73
|
version: "0"
|
72
74
|
requirements: []
|
73
75
|
|
74
76
|
rubyforge_project:
|
75
|
-
rubygems_version: 1.
|
77
|
+
rubygems_version: 1.8.5
|
76
78
|
signing_key:
|
77
79
|
specification_version: 3
|
78
80
|
summary: Simple Ruby library for self-imposed rater-limiting.
|
79
|
-
test_files:
|
80
|
-
|
81
|
-
- test/helper.rb
|
82
|
-
- test/test_glutton_ratelimit_bursty_ring_buffer.rb
|
83
|
-
- test/testing_module.rb
|
84
|
-
- test/test_glutton_ratelimit_bursty_token_bucket.rb
|
85
|
-
- examples/limit_instance_methods.rb
|
86
|
-
- examples/simple_manual.rb
|
81
|
+
test_files: []
|
82
|
+
|