allotment 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/allotment/stopwatch.rb +12 -7
- data/lib/allotment.rb +20 -0
- data/spec/allotment_spec.rb +134 -3
- data/spec/stopwatch/stopwatch_spec.rb +24 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ebc11149d0e75eab78814f7c06832ba1ebf09550
|
4
|
+
data.tar.gz: ccd06e0b331f68b762c1d3d8363e314f1d7122fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 288acb76f032599689b50bd687a683ec77ad6f931ab61f106b8cc5e0d953ac102ca6c1579691111913cdec5d752d9cceda8601ac514f0d4a58b6c8461bd9f187
|
7
|
+
data.tar.gz: d41a2fe690bf038a3f20ff6a74679b555056e2d453bdbba44fd8d76a7919ff24962c213c846f694680c857c04f23e682062760db40021cdef3a822e58c9cdb63
|
data/lib/allotment/stopwatch.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
require 'allotment/stopwatch'
|
2
|
-
|
3
1
|
module Allotment
|
4
2
|
class Stopwatch
|
5
|
-
attr_reader :name, :status
|
3
|
+
attr_reader :name, :status
|
6
4
|
|
7
|
-
def initialize name =
|
8
|
-
@
|
5
|
+
def initialize name = nil
|
6
|
+
@stopwatch = self.class.uniqe_name
|
7
|
+
@name = name || @stopwatch
|
9
8
|
@status = 'running'
|
10
9
|
@start_time = Time.now
|
11
10
|
end
|
@@ -19,13 +18,19 @@ module Allotment
|
|
19
18
|
@status = 'stopped'
|
20
19
|
@current_time = Time.now - @start_time
|
21
20
|
end
|
22
|
-
|
21
|
+
|
23
22
|
def reset
|
24
23
|
@start_time = Time.now
|
25
|
-
@current_time =
|
24
|
+
@current_time = nil
|
26
25
|
end
|
27
26
|
|
28
27
|
def lap
|
28
|
+
@new_lap = Time.now - (@lap_time || @start_time)
|
29
|
+
@lap_time = Time.now
|
30
|
+
return @new_lap
|
31
|
+
end
|
32
|
+
|
33
|
+
def split
|
29
34
|
Time.now - @start_time
|
30
35
|
end
|
31
36
|
|
data/lib/allotment.rb
CHANGED
@@ -35,4 +35,24 @@ module Allotment
|
|
35
35
|
JSON.pretty_generate @results
|
36
36
|
end
|
37
37
|
end
|
38
|
+
|
39
|
+
def record_event name = 'unnamed', &block
|
40
|
+
Allotment.record_event name, &block
|
41
|
+
end
|
42
|
+
|
43
|
+
def start_recording name = 'unnamed'
|
44
|
+
Allotment.start_recording name
|
45
|
+
end
|
46
|
+
|
47
|
+
def stop_recording name
|
48
|
+
Allotment.stop_recording name
|
49
|
+
end
|
50
|
+
|
51
|
+
def results
|
52
|
+
Allotment.results
|
53
|
+
end
|
54
|
+
|
55
|
+
def results_string
|
56
|
+
Allotment.results_string
|
57
|
+
end
|
38
58
|
end
|
data/spec/allotment_spec.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
|
+
class DummyClass
|
4
|
+
include Allotment
|
5
|
+
end
|
6
|
+
|
3
7
|
describe Allotment do
|
4
8
|
it "is a module" do
|
5
9
|
Allotment.class.should eq Module
|
@@ -7,16 +11,48 @@ describe Allotment do
|
|
7
11
|
|
8
12
|
describe ".record_event" do
|
9
13
|
it "records the time for the block" do
|
10
|
-
Allotment.record_event('
|
14
|
+
Allotment.record_event('my_recording 0.1') { sleep 0.01 }
|
15
|
+
end
|
16
|
+
|
17
|
+
it "records the time for the proc" do
|
18
|
+
Allotment.record_event 'my_recording 0.2' do
|
19
|
+
sleep 0.01
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns a float" do
|
24
|
+
result = Allotment.record_event('my_recording 0.3') { sleep 0.01 }
|
25
|
+
result.class.should eq Float
|
26
|
+
end
|
27
|
+
|
28
|
+
it "returns the execute time of the block" do
|
29
|
+
result = Allotment.record_event('my_recording 0.4') { sleep 0.01 }
|
30
|
+
result.round(2).should eq 0.01
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#record_event" do
|
35
|
+
before(:each) do
|
36
|
+
@dummy_class = DummyClass.new
|
37
|
+
end
|
38
|
+
|
39
|
+
it "records the time for the block" do
|
40
|
+
@dummy_class.record_event('my_recording 0.1') { sleep 0.01 }
|
41
|
+
end
|
42
|
+
|
43
|
+
it "records the time for the proc" do
|
44
|
+
@dummy_class.record_event 'my_recording 0.2' do
|
45
|
+
sleep 0.01
|
46
|
+
end
|
11
47
|
end
|
12
48
|
|
13
49
|
it "returns a float" do
|
14
|
-
result =
|
50
|
+
result = @dummy_class.record_event('my_recording 0.3') { sleep 0.01 }
|
15
51
|
result.class.should eq Float
|
16
52
|
end
|
17
53
|
|
18
54
|
it "returns the execute time of the block" do
|
19
|
-
result =
|
55
|
+
result = @dummy_class.record_event('my_recording 0.4') { sleep 0.01 }
|
20
56
|
result.round(2).should eq 0.01
|
21
57
|
end
|
22
58
|
end
|
@@ -43,6 +79,32 @@ describe Allotment do
|
|
43
79
|
end
|
44
80
|
end
|
45
81
|
|
82
|
+
describe "#start_recording" do
|
83
|
+
before(:each) do
|
84
|
+
@dummy_class = DummyClass.new
|
85
|
+
end
|
86
|
+
|
87
|
+
it "returns a stopwatch instance" do
|
88
|
+
result = @dummy_class.start_recording
|
89
|
+
result.class.should eq Allotment::Stopwatch
|
90
|
+
end
|
91
|
+
|
92
|
+
it "returns an unnamed stopwatch if no name given" do
|
93
|
+
result = @dummy_class.start_recording
|
94
|
+
result.name.should eq 'unnamed'
|
95
|
+
end
|
96
|
+
|
97
|
+
it "returns a stopwatch with the given name" do
|
98
|
+
result = @dummy_class.start_recording 'my_recording'
|
99
|
+
result.name.should eq 'my_recording'
|
100
|
+
end
|
101
|
+
|
102
|
+
it "returns a stopwatch that is 'running'" do
|
103
|
+
result = @dummy_class.start_recording
|
104
|
+
result.status.should eq 'running'
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
46
108
|
describe ".stop_recording" do
|
47
109
|
it "returns a float" do
|
48
110
|
Allotment.start_recording 'my_recording1'
|
@@ -63,6 +125,30 @@ describe Allotment do
|
|
63
125
|
end
|
64
126
|
end
|
65
127
|
|
128
|
+
describe "#stop_recording" do
|
129
|
+
before(:each) do
|
130
|
+
@dummy_class = DummyClass.new
|
131
|
+
end
|
132
|
+
|
133
|
+
it "returns a float" do
|
134
|
+
@dummy_class.start_recording 'my_recording1'
|
135
|
+
sleep 0.01
|
136
|
+
result = @dummy_class.stop_recording 'my_recording1'
|
137
|
+
result.class.should eq Float
|
138
|
+
end
|
139
|
+
|
140
|
+
it "returns the execute time of the code" do
|
141
|
+
@dummy_class.start_recording 'my_recording2'
|
142
|
+
sleep 0.01
|
143
|
+
result = @dummy_class.stop_recording 'my_recording2'
|
144
|
+
result.round(2).should eq 0.01
|
145
|
+
end
|
146
|
+
|
147
|
+
it "raises an error if the recording does not exist" do
|
148
|
+
expect { @dummy_class.stop_recording 'my_recording3' }.to raise_error NameError, "No recording:my_recording3"
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
66
152
|
describe ".results" do
|
67
153
|
it "returns a hash" do
|
68
154
|
Allotment.record_event('my_recording4') { sleep 0.01 }
|
@@ -87,6 +173,34 @@ describe Allotment do
|
|
87
173
|
end
|
88
174
|
end
|
89
175
|
|
176
|
+
describe "#results" do
|
177
|
+
before(:each) do
|
178
|
+
@dummy_class = DummyClass.new
|
179
|
+
end
|
180
|
+
|
181
|
+
it "returns a hash" do
|
182
|
+
@dummy_class.record_event('my_recording4') { sleep 0.01 }
|
183
|
+
@dummy_class.results.class.should eq Hash
|
184
|
+
end
|
185
|
+
|
186
|
+
it "returns a hash with the event in" do
|
187
|
+
@dummy_class.record_event('my_recording5') { sleep 0.01 }
|
188
|
+
@dummy_class.results.should include('my_recording5')
|
189
|
+
end
|
190
|
+
|
191
|
+
it "stores the result under the event" do
|
192
|
+
@dummy_class.record_event('my_recording6') { sleep 0.01 }
|
193
|
+
@dummy_class.results['my_recording6'][0].round(2).should eq 0.01
|
194
|
+
end
|
195
|
+
|
196
|
+
it "stores each result of each event" do
|
197
|
+
@dummy_class.record_event('my_recording7') { sleep 0.01 }
|
198
|
+
@dummy_class.record_event('my_recording7') { sleep 0.02 }
|
199
|
+
@dummy_class.results['my_recording7'][0].round(2).should eq 0.01
|
200
|
+
@dummy_class.results['my_recording7'][1].round(2).should eq 0.02
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
90
204
|
describe ".results_string" do
|
91
205
|
it "returns a string" do
|
92
206
|
Allotment.record_event('my_recording8') { sleep 0.01 }
|
@@ -99,4 +213,21 @@ describe Allotment do
|
|
99
213
|
Allotment.results_string.should include('0.03')
|
100
214
|
end
|
101
215
|
end
|
216
|
+
|
217
|
+
describe "#results_string" do
|
218
|
+
before(:each) do
|
219
|
+
@dummy_class = DummyClass.new
|
220
|
+
end
|
221
|
+
|
222
|
+
it "returns a string" do
|
223
|
+
@dummy_class.record_event('my_recording8') { sleep 0.01 }
|
224
|
+
@dummy_class.results_string.class.should eq String
|
225
|
+
end
|
226
|
+
|
227
|
+
it "returns a string with the event in" do
|
228
|
+
@dummy_class.record_event('my_recording9') { sleep 0.03 }
|
229
|
+
@dummy_class.results_string.should include('my_recording9')
|
230
|
+
@dummy_class.results_string.should include('0.03')
|
231
|
+
end
|
232
|
+
end
|
102
233
|
end
|
@@ -99,6 +99,28 @@ describe Allotment::Stopwatch do
|
|
99
99
|
end
|
100
100
|
end
|
101
101
|
|
102
|
+
describe "#split" do
|
103
|
+
it "returns a float" do
|
104
|
+
sw = Allotment::Stopwatch.new
|
105
|
+
sw.split.class.should eq Float
|
106
|
+
end
|
107
|
+
|
108
|
+
it "returns the correct time" do
|
109
|
+
sw = Allotment::Stopwatch.new
|
110
|
+
sleep 0.01
|
111
|
+
sw.split.round(2).should eq 0.01
|
112
|
+
sleep 0.01
|
113
|
+
sw.split.round(2).should eq 0.02
|
114
|
+
sw.stop.round(2).should eq 0.02
|
115
|
+
end
|
116
|
+
|
117
|
+
it "keeps its stopwatch name" do
|
118
|
+
sw = Allotment::Stopwatch.new('stopwatch')
|
119
|
+
sw.split
|
120
|
+
sw.name.should eq 'stopwatch'
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
102
124
|
describe "#lap" do
|
103
125
|
it "returns a float" do
|
104
126
|
sw = Allotment::Stopwatch.new
|
@@ -110,7 +132,8 @@ describe Allotment::Stopwatch do
|
|
110
132
|
sleep 0.01
|
111
133
|
sw.lap.round(2).should eq 0.01
|
112
134
|
sleep 0.01
|
113
|
-
sw.lap.round(2).should eq 0.
|
135
|
+
sw.lap.round(2).should eq 0.01
|
136
|
+
sw.stop.round(2).should eq 0.02
|
114
137
|
end
|
115
138
|
|
116
139
|
it "keeps its stopwatch name" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: allotment
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Slaughter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
11
|
+
date: 2013-08-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A gem for recording performance and timings of blocks or from point to
|
14
14
|
point
|
@@ -25,7 +25,7 @@ files:
|
|
25
25
|
- spec/helper.rb
|
26
26
|
- spec/stopwatch/array_spec.rb
|
27
27
|
- spec/stopwatch/stopwatch_spec.rb
|
28
|
-
homepage:
|
28
|
+
homepage: http://benslaughter.github.io/allotment/
|
29
29
|
licenses:
|
30
30
|
- MIT
|
31
31
|
metadata: {}
|