hitimes 2.0.0 → 3.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.
data/spec/spec_helper.rb DELETED
@@ -1,11 +0,0 @@
1
- if RUBY_VERSION >= '1.9.2' then
2
- require 'simplecov'
3
- puts "Using coverage!"
4
- SimpleCov.start if ENV['COVERAGE']
5
- end
6
-
7
- gem 'minitest'
8
- require 'hitimes'
9
- require 'minitest/autorun'
10
- require 'minitest/pride'
11
-
data/spec/stats_spec.rb DELETED
@@ -1,98 +0,0 @@
1
- require 'spec_helper'
2
- require 'json'
3
-
4
- describe Hitimes::Stats do
5
- before( :each ) do
6
- @stats = Hitimes::Stats.new
7
- @full_stats = Hitimes::Stats.new
8
-
9
- [ 1, 2, 3].each { |i| @full_stats.update( i ) }
10
- end
11
-
12
- it "is initialized with usable values" do
13
- _(@stats.count).must_equal 0
14
- _(@stats.min).must_equal Float::INFINITY
15
- _(@stats.max).must_equal(-Float::INFINITY)
16
- _(@stats.sum).must_equal 0.0
17
- _(@stats.rate).must_equal 0.0
18
- end
19
-
20
- it "calculates the mean correctly" do
21
- _(@full_stats.mean).must_equal 2.0
22
- end
23
-
24
- it "calculates the rate correctly" do
25
- _(@full_stats.rate).must_equal 0.5
26
- end
27
-
28
- it "tracks the maximum value" do
29
- _(@full_stats.max).must_equal 3.0
30
- end
31
-
32
- it "tracks the minimum value" do
33
- _(@full_stats.min).must_equal 1.0
34
- end
35
-
36
- it "tracks the count" do
37
- _(@full_stats.count).must_equal 3
38
- end
39
-
40
- it "tracks the sum" do
41
- _(@full_stats.sum).must_equal 6.0
42
- end
43
-
44
- it "calculates the standard deviation" do
45
- _(@full_stats.stddev).must_equal 1.0
46
- end
47
-
48
- it "calculates the sum of squares " do
49
- _(@full_stats.sumsq).must_equal 14.0
50
- end
51
-
52
- describe "#to_hash " do
53
- it "converts to a Hash" do
54
- h = @full_stats.to_hash
55
- _(h.size).must_equal ::Hitimes::Stats::STATS.size
56
- _(h.keys.sort).must_equal ::Hitimes::Stats::STATS
57
- end
58
-
59
- it "converts to a limited Hash if given arguments" do
60
- h = @full_stats.to_hash( "min", "max", "mean" )
61
- _(h.size).must_equal 3
62
- _(h.keys.sort).must_equal %w[ max mean min ]
63
-
64
- h = @full_stats.to_hash( %w[ count rate ] )
65
- _(h.size).must_equal 2
66
- _(h.keys.sort).must_equal %w[ count rate ]
67
- end
68
-
69
- it "raises NoMethodError if an invalid stat is used" do
70
- _(lambda { @full_stats.to_hash( "wibble" ) }).must_raise( NoMethodError )
71
- end
72
- end
73
-
74
- describe "#to_json" do
75
- it "converts to a json string" do
76
- j = @full_stats.to_json
77
- h = JSON.parse( j )
78
- _(h.size).must_equal ::Hitimes::Stats::STATS.size
79
- _(h.keys.sort).must_equal ::Hitimes::Stats::STATS
80
- end
81
-
82
- it "converts to a limited Hash if given arguments" do
83
- j = @full_stats.to_json( "min", "max", "mean" )
84
- h = JSON.parse( j )
85
- _(h.size).must_equal 3
86
- _(h.keys.sort).must_equal %w[ max mean min ]
87
-
88
- j = @full_stats.to_json( %w[ count rate ] )
89
- h = JSON.parse( j )
90
- _(h.size).must_equal 2
91
- _(h.keys.sort).must_equal %w[ count rate ]
92
- end
93
-
94
- it "raises NoMethodError if an invalid stat is used" do
95
- _(lambda { @full_stats.to_json( "wibble" ) }).must_raise( NoMethodError )
96
- end
97
- end
98
- end
@@ -1,155 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Hitimes::TimedMetric do
4
- before( :each ) do
5
- @tm = Hitimes::TimedMetric.new( 'test-timed-metric' )
6
- end
7
-
8
- it "knows if it is running or not" do
9
- _(@tm.running?).must_equal false
10
- @tm.start
11
- _(@tm.running?).must_equal true
12
- @tm.stop
13
- _(@tm.running?).must_equal false
14
- end
15
-
16
- it "#split returns the last duration and the timer is still running" do
17
- @tm.start
18
- d = @tm.split
19
- _(@tm.running?).must_equal true
20
- _(d).must_be :>, 0
21
- _(@tm.count).must_equal 1
22
- _(@tm.duration).must_equal d
23
- end
24
-
25
- it "#stop returns false if called more than once in a row" do
26
- @tm.start
27
- _(@tm.stop).must_be :>, 0
28
- _(@tm.stop).must_equal false
29
- end
30
-
31
- it "does not count a currently running interval as an interval in calculations" do
32
- @tm.start
33
- _(@tm.count).must_equal 0
34
- @tm.split
35
- _(@tm.count).must_equal 1
36
- end
37
-
38
- it "#split called on a stopped timer does nothing" do
39
- @tm.start
40
- @tm.stop
41
- _(@tm.split).must_equal false
42
- end
43
-
44
- it "calculates the mean of the durations" do
45
- 2.times { @tm.start ; sleep 0.05 ; @tm.stop }
46
- _(@tm.mean).must_be_close_to(0.05, 0.002)
47
- end
48
-
49
- it "calculates the rate of the counts " do
50
- 5.times { @tm.start ; sleep 0.05 ; @tm.stop }
51
- _(@tm.rate).must_be_close_to(20.00, 0.5)
52
- end
53
-
54
-
55
- it "calculates the stddev of the durations" do
56
- 3.times { |x| @tm.start ; sleep(0.05 * x) ; @tm.stop }
57
- _(@tm.stddev).must_be_close_to(0.05)
58
- end
59
-
60
- it "returns 0.0 for stddev if there is no data" do
61
- _(@tm.stddev).must_equal 0.0
62
- end
63
-
64
- it "keeps track of the min value" do
65
- 2.times { @tm.start ; sleep 0.05 ; @tm.stop }
66
- _(@tm.min).must_be_close_to(0.05, 0.01)
67
- end
68
-
69
- it "keeps track of the max value" do
70
- 2.times { @tm.start ; sleep 0.05 ; @tm.stop }
71
- _(@tm.max).must_be_close_to(0.05, 0.01)
72
- end
73
-
74
- it "keeps track of the sum value" do
75
- 2.times { @tm.start ; sleep 0.05 ; @tm.stop }
76
- _(@tm.sum).must_be_close_to(0.10, 0.01)
77
- end
78
-
79
- it "keeps track of the sum of squars value" do
80
- 3.times { @tm.start ; sleep 0.05 ; @tm.stop }
81
- _(@tm.sumsq).must_be_close_to(0.0075)
82
- end
83
-
84
- it "keeps track of the minimum start time of all the intervals" do
85
- f1 = Time.now.gmtime.to_f * 1_000_000
86
- 5.times { @tm.start ; sleep 0.05 ; @tm.stop }
87
- f2 = Time.now.gmtime.to_f * 1_000_000
88
- _(@tm.sampling_start_time).must_be :>=, f1
89
- _(@tm.sampling_start_time).must_be :<, f2
90
- # distance from now to start time should be greater than the distance from
91
- # the start to the min start_time
92
- _((f2 - @tm.sampling_start_time)).must_be :>, ( @tm.sampling_start_time - f1 )
93
- end
94
-
95
- it "keeps track of the last stop time of all the intervals" do
96
- f1 = Time.now.gmtime.to_f * 1_000_000
97
- sleep 0.01
98
- 5.times { @tm.start ; sleep 0.05 ; @tm.stop }
99
- sleep 0.01
100
- f2 = Time.now.gmtime.to_f * 1_000_000
101
- _(@tm.sampling_stop_time).must_be :>, f1
102
- _(@tm.sampling_stop_time).must_be :<=, f2
103
- # distance from now to max stop time time should be less than the distance
104
- # from the start to the max stop time
105
- _((f2 - @tm.sampling_stop_time)).must_be :<, ( @tm.sampling_stop_time - f1 )
106
- end
107
-
108
- it "can create an already running timer" do
109
- t = Hitimes::TimedMetric.now( 'already-running' )
110
- _(t.running?).must_equal true
111
- end
112
-
113
- it "can measure a block of code from an instance" do
114
- t = Hitimes::TimedMetric.new( 'measure a block' )
115
- 3.times { t.measure { sleep 0.05 } }
116
- _(t.duration).must_be_close_to(0.15, 0.01)
117
- _(t.count).must_equal 3
118
- end
119
-
120
- it "returns the value of the block when measuring" do
121
- t = Hitimes::TimedMetric.new( 'measure a block' )
122
- x = t.measure { sleep 0.05; 42 }
123
- _(t.duration).must_be_close_to(0.05, 0.002)
124
- _(x).must_equal 42
125
- end
126
-
127
- describe "#to_hash" do
128
-
129
- it "has name value" do
130
- h = @tm.to_hash
131
- _(h['name']).must_equal "test-timed-metric"
132
- end
133
-
134
- it "has an empty hash for additional_data" do
135
- h = @tm.to_hash
136
- _(h['additional_data']).must_equal Hash.new
137
- _(h['additional_data'].size).must_equal 0
138
- end
139
-
140
- it "has the right sum" do
141
- 10.times { |x| @tm.measure { sleep 0.01*x } }
142
- h = @tm.to_hash
143
- _(h['sum']).must_be_close_to(0.45, 0.01)
144
- end
145
-
146
- fields = ::Hitimes::Stats::STATS.dup + %w[ name additional_data sampling_start_time sampling_stop_time ]
147
- fields.each do |f|
148
- it "has a value for #{f}" do
149
- @tm.measure { sleep 0.001 }
150
- h = @tm.to_hash
151
- _(h[f]).wont_be_nil
152
- end
153
- end
154
- end
155
- end
@@ -1,171 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Hitimes::TimedValueMetric do
4
- before( :each ) do
5
- @tm = Hitimes::TimedValueMetric.new( 'test-timed-value-metric' )
6
- end
7
-
8
- it "knows if it is running or not" do
9
- _(@tm.running?).must_equal false
10
- @tm.start
11
- _(@tm.running?).must_equal true
12
- @tm.stop( 1 )
13
- _(@tm.running?).must_equal false
14
- end
15
-
16
- it "#split returns the last duration and the timer is still running" do
17
- @tm.start
18
- d = @tm.split( 1 )
19
- _(@tm.running?).must_equal true
20
- _(d).must_be :>, 0
21
- _(@tm.value_stats.count).must_equal 1
22
- _(@tm.timed_stats.count).must_equal 1
23
- _(@tm.duration).must_equal d
24
- end
25
-
26
- it "#stop returns false if called more than once in a row" do
27
- @tm.start
28
- _(@tm.stop( 1 )).must_be :>, 0
29
- _(@tm.stop( 1 )).must_equal false
30
- end
31
-
32
- it "does not count a currently running interval as an interval in calculations" do
33
- @tm.start
34
- _(@tm.value_stats.count).must_equal 0
35
- _(@tm.timed_stats.count).must_equal 0
36
- @tm.split( 1 )
37
- _(@tm.value_stats.count).must_equal 1
38
- _(@tm.timed_stats.count).must_equal 1
39
- end
40
-
41
- it "#split called on a stopped timer does nothing" do
42
- @tm.start
43
- @tm.stop( 1 )
44
- _(@tm.split( 1 )).must_equal false
45
- end
46
-
47
- it "calculates the mean of the durations" do
48
- 3.times { |x| @tm.start ; sleep 0.05 ; @tm.stop(x) }
49
- _(@tm.timed_stats.mean).must_be_close_to(0.05, 0.01)
50
- _(@tm.value_stats.mean).must_equal 1.00
51
- end
52
-
53
- it "calculates the rate of the counts " do
54
- 5.times { |x| @tm.start ; sleep 0.05 ; @tm.stop( x ) }
55
- _(@tm.rate).must_be_close_to(40.0, 1.0)
56
- end
57
-
58
-
59
- it "calculates the stddev of the durations" do
60
- 3.times { |x| @tm.start ; sleep(0.05 * x) ; @tm.stop(x) }
61
- _(@tm.timed_stats.stddev).must_be_close_to(0.05, 0.001)
62
- _(@tm.value_stats.stddev).must_equal 1.0
63
- end
64
-
65
- it "returns 0.0 for stddev if there is no data" do
66
- _(@tm.timed_stats.stddev).must_equal 0.0
67
- _(@tm.value_stats.stddev).must_equal 0.0
68
- end
69
-
70
- it "keeps track of the min value" do
71
- 3.times { |x| @tm.start ; sleep 0.05 ; @tm.stop( x ) }
72
- _(@tm.timed_stats.min).must_be_close_to( 0.05, 0.003 )
73
- _(@tm.value_stats.min).must_equal 0
74
- end
75
-
76
- it "keeps track of the max value" do
77
- 3.times { |x| @tm.start ; sleep 0.05 ; @tm.stop( x ) }
78
- _(@tm.timed_stats.max).must_be_close_to( 0.05, 0.003 )
79
- _(@tm.value_stats.max).must_equal 2
80
- end
81
-
82
- it "keeps track of the sum value" do
83
- 3.times { |x| @tm.start ; sleep 0.05 ; @tm.stop( x ) }
84
- _(@tm.timed_stats.sum).must_be_close_to( 0.15, 0.01 )
85
- _(@tm.value_stats.sum).must_equal 3
86
- end
87
-
88
- it "keeps track of the sum of squares value" do
89
- 3.times { |x| @tm.start ; sleep 0.05 ; @tm.stop( x ) }
90
- _(@tm.timed_stats.sumsq).must_be_close_to(0.0075, 0.0005)
91
- _(@tm.value_stats.sumsq).must_equal 5
92
- end
93
-
94
- it "keeps track of the minimum start time of all the intervals" do
95
- f1 = Time.now.gmtime.to_f * 1000000
96
- 5.times { @tm.start ; sleep 0.05 ; @tm.stop( 1 ) }
97
- f2 = Time.now.gmtime.to_f * 1000000
98
- _(@tm.sampling_start_time).must_be :>=, f1
99
- _(@tm.sampling_start_time).must_be :<, f2
100
- # distance from now to start time should be greater than the distance from
101
- # the start to the min start_time
102
- _((f2 - @tm.sampling_start_time)).must_be :>, ( @tm.sampling_start_time - f1 )
103
- end
104
-
105
- it "keeps track of the last stop time of all the intervals" do
106
- f1 = Time.now.gmtime.to_f * 1_000_000
107
- 5.times { @tm.start ; sleep 0.05 ; @tm.stop( 1 ) }
108
- sleep 0.05
109
- f2 = Time.now.gmtime.to_f * 1_000_000
110
- _(@tm.sampling_stop_time).must_be :>, f1
111
- _(@tm.sampling_stop_time).must_be :<=, f2
112
- # distance from now to max stop time time should be less than the distance
113
- # from the start to the max stop time
114
- _((f2 - @tm.sampling_stop_time)).must_be :<, ( @tm.sampling_stop_time - f1 )
115
- end
116
-
117
- it "can create an already running timer" do
118
- t = Hitimes::TimedValueMetric.now( 'already-running' )
119
- _(t.running?).must_equal true
120
- end
121
-
122
- it "can measure a block of code from an instance" do
123
- t = Hitimes::TimedValueMetric.new( 'measure a block' )
124
- 3.times { t.measure( 1 ) { sleep 0.05 } }
125
- _(t.duration).must_be_close_to(0.15, 0.004)
126
- _(t.timed_stats.count).must_equal 3
127
- _(t.value_stats.count).must_equal 3
128
- end
129
-
130
- it "returns the value of the block when measuring" do
131
- t = Hitimes::TimedValueMetric.new( 'measure a block' )
132
- x = t.measure( 42 ) { sleep 0.05; 42 }
133
- _(t.duration).must_be_close_to(0.05, 0.002)
134
- _(x).must_equal 42
135
- end
136
-
137
- describe "#to_hash" do
138
-
139
- it "has name value" do
140
- h = @tm.to_hash
141
- _(h['name']).must_equal "test-timed-value-metric"
142
- end
143
-
144
- it "has an empty has for additional_data" do
145
- h = @tm.to_hash
146
- _(h['additional_data']).must_equal Hash.new
147
- _(h['additional_data'].size).must_equal 0
148
- end
149
-
150
- it "has a rate" do
151
- 5.times { |x| @tm.start ; sleep 0.05 ; @tm.stop( x ) }
152
- h = @tm.to_hash
153
- _(h['rate']).must_be_close_to(40.0, 1.0)
154
- end
155
-
156
- it "has a unit_count" do
157
- 5.times { |x| @tm.start ; sleep 0.05 ; @tm.stop( x ) }
158
- h = @tm.to_hash
159
- _(h['unit_count']).must_equal 10
160
- end
161
-
162
- fields = %w[ name additional_data sampling_start_time sampling_stop_time value_stats timed_stats rate unit_count ]
163
- fields.each do |f|
164
- it "has a value for #{f}" do
165
- 3.times { |x| @tm.measure(x) { sleep 0.001 } }
166
- h = @tm.to_hash
167
- _(h[f]).wont_be_nil
168
- end
169
- end
170
- end
171
- end
@@ -1,108 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Hitimes::ValueMetric do
4
- before( :each ) do
5
- @metric = Hitimes::ValueMetric.new( "testing" )
6
- 10.times { |x| @metric.measure( x ) }
7
- end
8
-
9
- it 'has a name' do
10
- _(@metric.name).must_equal "testing"
11
- end
12
-
13
- it "has associated data from initialization" do
14
- m = Hitimes::ValueMetric.new( "more-data", 'foo' => 'bar', 'this' => 'that' )
15
- _(m.additional_data['foo']).must_equal 'bar'
16
- _(m.additional_data['this']).must_equal 'that'
17
-
18
- m = Hitimes::ValueMetric.new( "more-data", { 'foo' => 'bar', 'this' => 'that' } )
19
- _(m.additional_data['foo']).must_equal 'bar'
20
- _(m.additional_data['this']).must_equal 'that'
21
- end
22
-
23
- it "calculates the mean of the measurements" do
24
- _(@metric.mean).must_equal 4.5
25
- end
26
-
27
- it "calculates the stddev of the measurements" do
28
- _(@metric.stddev).must_be :>, 0.0
29
- end
30
-
31
- it "returns 0.0 for stddev if there is no data" do
32
- m = Hitimes::ValueMetric.new('0-data')
33
- _(m.stddev).must_equal 0.0
34
- end
35
-
36
- it "keeps track of the sum of data" do
37
- _(@metric.sum).must_equal 45.0
38
- end
39
-
40
- it "keeps track of the sum of squars of data" do
41
- _(@metric.sumsq).must_equal 285.0
42
- end
43
-
44
- it "retuns 0.0 for mean if there is no data" do
45
- _(Hitimes::ValueMetric.new('0-data').mean).must_equal 0.0
46
- end
47
-
48
- it "keeps track of the min value" do
49
- _(@metric.min).must_equal 0
50
- end
51
-
52
- it "keeps track of the max value" do
53
- _(@metric.max).must_equal 9
54
- end
55
-
56
- it "keeps track of the first start time of all the measurements" do
57
- m = Hitimes::ValueMetric.new( "first-start-time" )
58
- f1 = Time.now.gmtime.to_f * 1_000_000
59
- 10.times{ |x| m.measure( x ); sleep 0.1 }
60
- f2 = Time.now.gmtime.to_f * 1_000_000
61
- _(m.sampling_start_time).must_be :>=, f1
62
- _(m.sampling_start_time).must_be :<, f2
63
- # distance from now to start time should be greater than the distance from
64
- # the start to the min start_time
65
- _((f2 - m.sampling_start_time)).must_be :>, ( m.sampling_start_time - f1 )
66
- end
67
-
68
- it "keeps track of the last stop time of all the intervals" do
69
- m = Hitimes::ValueMetric.new( "last-stop-time" )
70
- f1 = Time.now.gmtime.to_f * 1_000_000
71
- 10.times {|x| m.measure( x ); sleep 0.1 }
72
- f2 = Time.now.gmtime.to_f * 1_000_000
73
- _(m.sampling_stop_time).must_be :>, f1
74
- _(m.sampling_stop_time).must_be :<=, f2
75
- # distance from now to max stop time time should be less than the distance
76
- # from the start to the max stop time
77
- _((f2 - m.sampling_stop_time)).must_be :<, ( m.sampling_stop_time - f1 )
78
- end
79
-
80
- describe "#to_hash" do
81
-
82
- it "has name value" do
83
- h = @metric.to_hash
84
- _(h['name']).must_equal "testing"
85
- end
86
-
87
- it "has an empty has for additional_data" do
88
- h = @metric.to_hash
89
- _(h['additional_data']).must_equal Hash.new
90
- _(h['additional_data'].size).must_equal 0
91
- end
92
-
93
- it "has the right sum" do
94
- h = @metric.to_hash
95
- _(h['sum']).must_equal 45
96
- end
97
-
98
- fields = ::Hitimes::Stats::STATS.dup + %w[ name additional_data sampling_start_time sampling_stop_time ]
99
- fields = fields - [ 'rate' ]
100
- fields.each do |f|
101
- it "has a value for #{f}" do
102
- h = @metric.to_hash
103
- _(h[f]).wont_be_nil
104
- end
105
- end
106
- end
107
- end
108
-
data/spec/version_spec.rb DELETED
@@ -1,7 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe "Hitimes::Version" do
4
- it "should be accessable as a constant" do
5
- _(Hitimes::VERSION).must_match(/\d+\.\d+\.\d+/)
6
- end
7
- end