allotment 0.0.1 → 1.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f1725b9fb8ace6c575075d430991795aee92207d
4
- data.tar.gz: b1e799ec1f2f1072399e51ba0784ced21533218f
3
+ metadata.gz: 45adf43db853faac34a57e8be2b94914c4708e35
4
+ data.tar.gz: cfdc29f2a8949df3ba3630e314e735f7b1b1d3d7
5
5
  SHA512:
6
- metadata.gz: c3672fb2aad8b5955d92639dcc8365c53c6324028629dd40ffe6b486527d5c7d0b5486dd49fd616343f1f79d2d700452f118286e4693055a487715e2e3935b50
7
- data.tar.gz: 7c66df8f6fc284a61c50ce889f437127e3dbaea13842bad80f89cfaa500a29b564ab6d52dc3ccd485ed6de45019d130d761f845bb4b27ea2700319975f6ff8a6
6
+ metadata.gz: cc352f7d58035999eaa90c1b591d6f33e139d25ff1d99e93df51b19da7f498a706bbe8171a47c117507eebbf20900a08fa34f1e1999731d0c1185cdc986893bf
7
+ data.tar.gz: 9c3e98e45a41f40fed4e52a906e1994e2d251303c76ec96f889d498aeab5c9a94f4e5a368f539b5c94d8bc9e7689a8cb45e43b20d4581e6ffa88291f7915c9b7
@@ -1,30 +1,38 @@
1
+ require 'allotment/array'
2
+ require 'allotment/stopwatch'
3
+ require 'json'
4
+
1
5
  module Allotment
2
6
  class << self
3
- def record_event name = Time.now.to_s, &block
4
- start_time = Time.now
7
+ def record_event name = 'unnamed', &block
8
+ start_recording name
5
9
  yield
6
- Time.now - start_time
10
+ stop_recording name
7
11
  end
8
12
 
9
- def start_recording name = Time.now.to_s
13
+ def start_recording name = 'unnamed'
10
14
  @watches ||= Hash.new
11
15
  @watches[name] = Stopwatch.new name
12
- @watches[name]
13
16
  end
14
17
 
15
18
  def stop_recording name
16
- watch = @watches.delete(name) {|e| "%s does not exist!" % e }
17
- Time.now - watch.start_time
19
+ watch = @watches.delete(name) { |n| raise NameError, "No recording:" + n }
20
+ result = watch.stop
21
+
22
+ # Dealing with the results
23
+ @results ||= Hash.new
24
+ @results[name] ||= Array.new
25
+ @results[name].push result
26
+
27
+ return result
18
28
  end
19
- end
20
29
 
21
- class Stopwatch
22
- attr_reader :name, :status, :start_time
30
+ def results
31
+ @results
32
+ end
23
33
 
24
- def initialize name
25
- @name = name
26
- @status = 'running'
27
- @start_time = Time.now
34
+ def results_string
35
+ JSON.pretty_generate @results
28
36
  end
29
37
  end
30
38
  end
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def average
3
+ inject(:+).to_f / size
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ require 'allotment'
2
+
3
+ Before do |scenario|
4
+ Allotment.start_recording scenario.title
5
+ end
6
+
7
+ After do |scenario|
8
+ Allotment.stop_recording scenario.title
9
+ end
10
+
11
+ at_exit do
12
+ puts Allotment.results_string
13
+ end
@@ -0,0 +1,37 @@
1
+ require 'allotment/stopwatch'
2
+
3
+ module Allotment
4
+ class Stopwatch
5
+ attr_reader :name, :status, :start_time
6
+
7
+ def initialize name = self.class.uniqe_name
8
+ @name = name
9
+ @status = 'running'
10
+ @start_time = Time.now
11
+ end
12
+
13
+ def start
14
+ @start_time = Time.now - (@current_time || 0)
15
+ @status = 'running'
16
+ end
17
+
18
+ def stop
19
+ @status = 'stopped'
20
+ @current_time = Time.now - @start_time
21
+ end
22
+
23
+ def reset
24
+ @start_time = Time.now
25
+ @current_time = 0
26
+ end
27
+
28
+ def lap
29
+ Time.now - @start_time
30
+ end
31
+
32
+ private
33
+ def self.uniqe_name
34
+ "stopwatch_" + (@id ? @id += 1 : @id = 0).to_s
35
+ end
36
+ end
37
+ end
@@ -1,52 +1,102 @@
1
- require 'coveralls'
2
- Coveralls.wear!
1
+ require 'helper'
3
2
 
4
- require './lib/allotment'
5
-
6
- describe Allotment, "#record_event" do
7
- it "records the time for the block" do
8
- Allotment.record_event('name') { nil }
3
+ describe Allotment do
4
+ it "is a module" do
5
+ Allotment.class.should eq Module
9
6
  end
10
7
 
11
- it "returns a float" do
12
- result = Allotment.record_event('name') { nil }
13
- result.class.should eq Float
14
- end
8
+ describe ".record_event" do
9
+ it "records the time for the block" do
10
+ Allotment.record_event('my_recording0') { sleep 0.01 }
11
+ end
15
12
 
16
- it "returns the execute time of the block" do
17
- result = Allotment.record_event('name') { sleep 0.1 }
18
- result.round(1).should eq 0.1
19
- end
20
- end
13
+ it "returns a float" do
14
+ result = Allotment.record_event('my_recording0') { sleep 0.01 }
15
+ result.class.should eq Float
16
+ end
21
17
 
22
- describe Allotment, "#start_recording" do
23
- it "returns a stopwatch instance" do
24
- result = Allotment.start_recording
25
- result.class.should eq Allotment::Stopwatch
18
+ it "returns the execute time of the block" do
19
+ result = Allotment.record_event('my_recording0') { sleep 0.01 }
20
+ result.round(2).should eq 0.01
21
+ end
26
22
  end
27
23
 
28
- it "returns a stopwatch with the given name" do
29
- result = Allotment.start_recording
30
- result.class.should eq Allotment::Stopwatch
24
+ describe ".start_recording" do
25
+ it "returns a stopwatch instance" do
26
+ result = Allotment.start_recording
27
+ result.class.should eq Allotment::Stopwatch
28
+ end
29
+
30
+ it "returns an unnamed stopwatch if no name given" do
31
+ result = Allotment.start_recording
32
+ result.name.should eq 'unnamed'
33
+ end
34
+
35
+ it "returns a stopwatch with the given name" do
36
+ result = Allotment.start_recording 'my_recording'
37
+ result.name.should eq 'my_recording'
38
+ end
39
+
40
+ it "returns a stopwatch that is 'running'" do
41
+ result = Allotment.start_recording
42
+ result.status.should eq 'running'
43
+ end
31
44
  end
32
45
 
33
- it "returns a stopwatch that is 'running'" do
34
- result = Allotment.start_recording 'my_recording'
35
- result.name.should eq 'my_recording'
46
+ describe ".stop_recording" do
47
+ it "returns a float" do
48
+ Allotment.start_recording 'my_recording1'
49
+ sleep 0.01
50
+ result = Allotment.stop_recording 'my_recording1'
51
+ result.class.should eq Float
52
+ end
53
+
54
+ it "returns the execute time of the code" do
55
+ Allotment.start_recording 'my_recording2'
56
+ sleep 0.01
57
+ result = Allotment.stop_recording 'my_recording2'
58
+ result.round(2).should eq 0.01
59
+ end
60
+
61
+ it "raises an error if the recording does not exist" do
62
+ expect { Allotment.stop_recording 'my_recording3' }.to raise_error NameError, "No recording:my_recording3"
63
+ end
36
64
  end
37
- end
38
65
 
39
- describe Allotment, "#stop_recording" do
40
- it "returns a float" do
41
- Allotment.start_recording 'my_recording1'
42
- result = Allotment.stop_recording 'my_recording1'
43
- result.class.should eq Float
66
+ describe ".results" do
67
+ it "returns a hash" do
68
+ Allotment.record_event('my_recording4') { sleep 0.01 }
69
+ Allotment.results.class.should eq Hash
70
+ end
71
+
72
+ it "returns a hash with the event in" do
73
+ Allotment.record_event('my_recording5') { sleep 0.01 }
74
+ Allotment.results.should include('my_recording5')
75
+ end
76
+
77
+ it "stores the result under the event" do
78
+ Allotment.record_event('my_recording6') { sleep 0.01 }
79
+ Allotment.results['my_recording6'][0].round(2).should eq 0.01
80
+ end
81
+
82
+ it "stores each result of each event" do
83
+ Allotment.record_event('my_recording7') { sleep 0.01 }
84
+ Allotment.record_event('my_recording7') { sleep 0.02 }
85
+ Allotment.results['my_recording7'][0].round(2).should eq 0.01
86
+ Allotment.results['my_recording7'][1].round(2).should eq 0.02
87
+ end
44
88
  end
45
89
 
46
- it "returns the execute time of the code" do
47
- Allotment.start_recording 'my_recording2'
48
- sleep 0.1
49
- result = Allotment.stop_recording 'my_recording2'
50
- result.round(1).should eq 0.1
90
+ describe ".results_string" do
91
+ it "returns a string" do
92
+ Allotment.record_event('my_recording8') { sleep 0.01 }
93
+ Allotment.results_string.class.should eq String
94
+ end
95
+
96
+ it "returns a string with the event in" do
97
+ Allotment.record_event('my_recording9') { sleep 0.03 }
98
+ Allotment.results_string.should include('my_recording9')
99
+ Allotment.results_string.should include('0.03')
100
+ end
51
101
  end
52
102
  end
@@ -0,0 +1,9 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ require 'allotment'
5
+
6
+ RSpec.configure do |config|
7
+ config.color_enabled = true
8
+ config.formatter = :documentation
9
+ end
@@ -0,0 +1,15 @@
1
+ describe Array do
2
+ describe "#average" do
3
+ it "returns a float" do
4
+ [1,2,3,4,5].average.class.should eq Float
5
+ end
6
+
7
+ it "returns the mean of the array" do
8
+ [1,2,3,4,5,6,7,7].average.should eq 4.375
9
+ end
10
+
11
+ it "raises an error if one of the values is not a fixnum" do
12
+ expect { [1,2,3,4,5,'invalid'].average }.to raise_error TypeError, "String can't be coerced into Fixnum"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,122 @@
1
+ describe Allotment::Stopwatch do
2
+ it "is a class" do
3
+ Allotment::Stopwatch.class.should eq Class
4
+ end
5
+
6
+ describe ".new" do
7
+ it "returns a class of Stopwatch" do
8
+ Allotment::Stopwatch.new.class.should eq Allotment::Stopwatch
9
+ end
10
+
11
+ it "sets the stopwatch status to running" do
12
+ Allotment::Stopwatch.new.status.should eq 'running'
13
+ end
14
+
15
+ it "gives the stopwatch a name if none given" do
16
+ Allotment::Stopwatch.new.name.should =~ /stopwatch_\d+/
17
+ end
18
+
19
+ it "sets the name of the stopwatch" do
20
+ Allotment::Stopwatch.new('stopwatch').name.should eq 'stopwatch'
21
+ end
22
+ end
23
+
24
+ describe "#stop" do
25
+ it "returns a float" do
26
+ sw = Allotment::Stopwatch.new
27
+ sw.stop.class.should eq Float
28
+ end
29
+
30
+ it "returns the correct time" do
31
+ sw = Allotment::Stopwatch.new
32
+ sleep 0.01
33
+ sw.stop.round(2).should eq 0.01
34
+ end
35
+
36
+ it "sets the stopwatch status to stopped" do
37
+ sw = Allotment::Stopwatch.new
38
+ sw.stop
39
+ sw.status.should eq 'stopped'
40
+ end
41
+
42
+ it "keeps its stopwatch name" do
43
+ sw = Allotment::Stopwatch.new('stopwatch')
44
+ sw.stop
45
+ sw.name.should eq 'stopwatch'
46
+ end
47
+ end
48
+
49
+ describe "#start" do
50
+ it "sets the stopwatch status to running" do
51
+ sw = Allotment::Stopwatch.new
52
+ sw.stop
53
+ sw.start
54
+ sw.status.should eq 'running'
55
+ end
56
+
57
+ it "keeps track of total time" do
58
+ sw = Allotment::Stopwatch.new
59
+ sleep 0.01
60
+ sw.stop
61
+ sw.start
62
+ sleep 0.01
63
+ sw.stop.round(2).should eq 0.02
64
+ end
65
+
66
+ it "keeps its stopwatch name" do
67
+ sw = Allotment::Stopwatch.new('stopwatch')
68
+ sw.stop
69
+ sw.start
70
+ sw.name.should eq 'stopwatch'
71
+ end
72
+ end
73
+
74
+ describe "#reset" do
75
+ it "resets the stopwatch total time when stopped" do
76
+ sw = Allotment::Stopwatch.new
77
+ sleep 0.01
78
+ sw.stop
79
+ sw.status.should eq 'stopped'
80
+ sw.reset
81
+ sw.start
82
+ sleep 0.01
83
+ sw.stop.round(2).should eq 0.01
84
+ end
85
+
86
+ it "resets the stopwatch total time when running" do
87
+ sw = Allotment::Stopwatch.new
88
+ sleep 0.01
89
+ sw.status.should eq 'running'
90
+ sw.reset
91
+ sleep 0.01
92
+ sw.stop.round(2).should eq 0.01
93
+ end
94
+
95
+ it "keeps its stopwatch name" do
96
+ sw = Allotment::Stopwatch.new('stopwatch')
97
+ sw.reset
98
+ sw.name.should eq 'stopwatch'
99
+ end
100
+ end
101
+
102
+ describe "#lap" do
103
+ it "returns a float" do
104
+ sw = Allotment::Stopwatch.new
105
+ sw.lap.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.lap.round(2).should eq 0.01
112
+ sleep 0.01
113
+ sw.lap.round(2).should eq 0.02
114
+ end
115
+
116
+ it "keeps its stopwatch name" do
117
+ sw = Allotment::Stopwatch.new('stopwatch')
118
+ sw.lap
119
+ sw.name.should eq 'stopwatch'
120
+ end
121
+ end
122
+ end
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: 0.0.1
4
+ version: 1.0.0
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-04-18 00:00:00.000000000 Z
11
+ date: 2013-08-05 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
@@ -17,8 +17,14 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - lib/allotment/array.rb
21
+ - lib/allotment/cucumber.rb
22
+ - lib/allotment/stopwatch.rb
20
23
  - lib/allotment.rb
21
24
  - spec/allotment_spec.rb
25
+ - spec/helper.rb
26
+ - spec/stopwatch/array_spec.rb
27
+ - spec/stopwatch/stopwatch_spec.rb
22
28
  homepage: https://github.com/benSlaughter/allotment
23
29
  licenses:
24
30
  - MIT
@@ -42,6 +48,10 @@ rubyforge_project:
42
48
  rubygems_version: 2.0.3
43
49
  signing_key:
44
50
  specification_version: 4
45
- summary: Allotment - performance recording
51
+ summary: Performance recording
46
52
  test_files:
47
53
  - spec/allotment_spec.rb
54
+ - spec/helper.rb
55
+ - spec/stopwatch/array_spec.rb
56
+ - spec/stopwatch/stopwatch_spec.rb
57
+ has_rdoc: