should_be_faster 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/should_be_faster.rb +22 -16
- data/spec/should_be_faster_spec.rb +62 -7
- metadata +3 -3
data/lib/should_be_faster.rb
CHANGED
@@ -1,25 +1,12 @@
|
|
1
1
|
require 'benchmark'
|
2
2
|
require 'spec'
|
3
3
|
require 'spec/matchers'
|
4
|
+
require 'spec/matchers/be'
|
4
5
|
|
5
6
|
def _benchmark(code, iter)
|
6
7
|
Benchmark.measure { iter.times { code.call } }
|
7
8
|
end
|
8
9
|
|
9
|
-
Spec::Matchers.define(:be_faster_than) do |rhs_code, options|
|
10
|
-
options ||= {}
|
11
|
-
options[:matcher] = self
|
12
|
-
options[:faster] = true
|
13
|
-
Spec::Matchers::BenchmarkComparison.new(rhs_code, options).benchmark_comparison
|
14
|
-
end
|
15
|
-
|
16
|
-
Spec::Matchers.define(:be_slower_than) do |rhs_code, options|
|
17
|
-
options ||= {}
|
18
|
-
options[:matcher] = self
|
19
|
-
options[:faster] = false
|
20
|
-
Spec::Matchers::BenchmarkComparison.new(rhs_code, options).benchmark_comparison
|
21
|
-
end
|
22
|
-
|
23
10
|
module Spec
|
24
11
|
module Matchers
|
25
12
|
class BenchmarkComparison
|
@@ -28,7 +15,7 @@ module Spec
|
|
28
15
|
@options = options
|
29
16
|
@options[:iterations] ||= 100
|
30
17
|
@options[:factor] ||= 1
|
31
|
-
@options[:faster] = @options[:faster]
|
18
|
+
@options[:faster] = @options[:faster]
|
32
19
|
end
|
33
20
|
|
34
21
|
def benchmark_comparison
|
@@ -76,7 +63,7 @@ module Spec
|
|
76
63
|
end
|
77
64
|
end
|
78
65
|
|
79
|
-
|
66
|
+
module BeFasterThan
|
80
67
|
def times
|
81
68
|
@factor = @args[0]
|
82
69
|
self
|
@@ -102,5 +89,24 @@ module Spec
|
|
102
89
|
end
|
103
90
|
end
|
104
91
|
end
|
92
|
+
|
105
93
|
end
|
106
94
|
end
|
95
|
+
|
96
|
+
Spec::Matchers::BePredicate.send(:include, Spec::Matchers::BeFasterThan)
|
97
|
+
Spec::Matchers::BeSameAs.send(:include, Spec::Matchers::BeFasterThan)
|
98
|
+
|
99
|
+
Spec::Matchers.define(:be_faster_than) do |rhs_code, options|
|
100
|
+
options ||= {}
|
101
|
+
options[:matcher] = self
|
102
|
+
options[:faster] = true
|
103
|
+
Spec::Matchers::BenchmarkComparison.new(rhs_code, options).benchmark_comparison
|
104
|
+
end
|
105
|
+
|
106
|
+
Spec::Matchers.define(:be_slower_than) do |rhs_code, options|
|
107
|
+
options ||= {}
|
108
|
+
options[:matcher] = self
|
109
|
+
options[:faster] = false
|
110
|
+
Spec::Matchers::BenchmarkComparison.new(rhs_code, options).benchmark_comparison
|
111
|
+
end
|
112
|
+
|
@@ -9,13 +9,24 @@ describe 'should_be_faster' do
|
|
9
9
|
Spec::Matchers::BenchmarkComparison.new(nil, options)
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
context 'with an options hash' do
|
13
|
+
it 'should allow you to specify iterations' do
|
14
|
+
options = mock('hash')
|
15
|
+
options.stub!(:[])
|
16
|
+
options.stub!(:[]=)
|
17
|
+
options.stub!(:[]).with(:iterations).and_return(20)
|
18
|
+
Spec::Matchers::BenchmarkComparison.new(nil, options)
|
19
|
+
options[:iterations].should == 20
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should allow you to specify factor' do
|
23
|
+
options = mock('hash')
|
24
|
+
options.stub!(:[])
|
25
|
+
options.stub!(:[]=)
|
26
|
+
options.stub!(:[]).with(:factor).and_return(20)
|
27
|
+
Spec::Matchers::BenchmarkComparison.new(nil, options)
|
28
|
+
options[:factor].should == 20
|
29
|
+
end
|
19
30
|
end
|
20
31
|
|
21
32
|
context 'when ensuring code is' do
|
@@ -29,9 +40,21 @@ describe 'should_be_faster' do
|
|
29
40
|
@fast.should be_faster_than(@slow)
|
30
41
|
end
|
31
42
|
|
43
|
+
it 'should ensure lhs < rhs by x times' do
|
44
|
+
@fast.should be_faster_than(@slow, :factor => 2)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should ensure lhs < rhs by y secs'
|
48
|
+
|
32
49
|
it 'should ensure lhs !> rhs' do
|
33
50
|
@slow.should_not be_faster_than(@fast)
|
34
51
|
end
|
52
|
+
|
53
|
+
it 'should ensure lhs !> rhs by x times' do
|
54
|
+
@slow.should_not be_faster_than(@fast, :factor => 2)
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should ensure lhs !> rhs by y secs'
|
35
58
|
end
|
36
59
|
|
37
60
|
context 'slower_than' do
|
@@ -39,9 +62,21 @@ describe 'should_be_faster' do
|
|
39
62
|
@slow.should be_slower_than(@fast)
|
40
63
|
end
|
41
64
|
|
65
|
+
it 'should ensure lhs > rhs by x times' do
|
66
|
+
@slow.should be_slower_than(@fast, :factor => 2)
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'should ensure lhs > rhs by y secs'
|
70
|
+
|
42
71
|
it 'should ensure lhs !< rhs' do
|
43
72
|
@fast.should_not be_slower_than(@slow)
|
44
73
|
end
|
74
|
+
|
75
|
+
it 'should ensure lhs !< rhs by x times' do
|
76
|
+
@fast.should_not be_slower_than(@slow, :factor => 2)
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should ensure lhs !< rhs by y secs'
|
45
80
|
end
|
46
81
|
|
47
82
|
context 'at_least(x).times.faster_than' do
|
@@ -63,5 +98,25 @@ describe 'should_be_faster' do
|
|
63
98
|
@fast.should_not be_at_least(2).times.slower_than(@slow)
|
64
99
|
end
|
65
100
|
end
|
101
|
+
|
102
|
+
context 'x.times.slower_than' do
|
103
|
+
it 'should ensure lhs > rhs' do
|
104
|
+
@slow.should be(2).times.slower_than(@fast)
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'should ensure lhs !< rhs' do
|
108
|
+
@fast.should_not be(2).times.slower_than(@slow)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context 'x.times.faster_than' do
|
113
|
+
it 'should ensure lhs > rhs' do
|
114
|
+
@fast.should be(2).times.faster_than(@slow)
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'should ensure lhs !< rhs' do
|
118
|
+
@slow.should_not be(2).times.faster_than(@fast)
|
119
|
+
end
|
120
|
+
end
|
66
121
|
end
|
67
122
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- David Turnbull
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-05-06 00:00:00 +10:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|