ruby-metrics 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Metrics::Instruments::Gauge do
4
+ before(:each) do
5
+ end
6
+
7
+ it "should create a new gauge" do
8
+ callback = Proc.new {}
9
+ @gauge = Metrics::Instruments::Gauge.new &callback
10
+ end
11
+
12
+ it "should correctly callback the block given when we call Gauge#get" do
13
+ result = 42
14
+
15
+ callback = Proc.new do
16
+ {
17
+ :result => result
18
+ }
19
+ end
20
+
21
+ @gauge = Metrics::Instruments::Gauge.new &callback
22
+
23
+ @gauge.get[:result].should == 42
24
+
25
+ result += 1
26
+
27
+ @gauge.get[:result].should == 43
28
+ end
29
+
30
+ it "should JSONify the results when you call to_s" do
31
+ result = 42
32
+
33
+ callback = Proc.new do
34
+ {
35
+ :result => result
36
+ }
37
+ end
38
+
39
+ @gauge = Metrics::Instruments::Gauge.new &callback
40
+
41
+ @gauge.to_s.should == "{\"result\":42}"
42
+ end
43
+
44
+ end
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+
3
+ # This is compared to results from R using the built-in old-faithful dataset
4
+
5
+ describe Metrics::Instruments::Histogram do
6
+
7
+ before(:each) do
8
+ @histogram = Metrics::Instruments::Histogram.new
9
+ data = %w(3.600 1.800 3.333 2.283 4.533 2.883 4.700 3.600 1.950 4.350 1.833 3.917 4.200 1.750 4.700 2.167 1.750 4.800 1.600 4.250 1.800 1.750 3.450 3.067 4.533 3.600 1.967 4.083 3.850 4.433 4.300 4.467 3.367 4.033 3.833 2.017 1.867 4.833 1.833 4.783 4.350 1.883 4.567 1.750 4.533 3.317 3.833 2.100 4.633 2.000 4.800 4.716 1.833 4.833 1.733 4.883 3.717 1.667 4.567 4.317 2.233 4.500 1.750 4.800 1.817 4.400 4.167 4.700 2.067 4.700 4.033 1.967 4.500 4.000 1.983 5.067 2.017 4.567 3.883 3.600 4.133 4.333 4.100 2.633 4.067 4.933 3.950 4.517 2.167 4.000 2.200 4.333 1.867 4.817 1.833 4.300 4.667 3.750 1.867 4.900 2.483 4.367 2.100 4.500 4.050 1.867 4.700 1.783 4.850 3.683 4.733 2.300 4.900 4.417 1.700 4.633 2.317 4.600 1.817 4.417 2.617 4.067 4.250 1.967 4.600 3.767 1.917 4.500 2.267 4.650 1.867 4.167 2.800 4.333 1.833 4.383 1.883 4.933 2.033 3.733 4.233 2.233 4.533 4.817 4.333 1.983 4.633 2.017 5.100 1.800 5.033 4.000 2.400 4.600 3.567 4.000 4.500 4.083 1.800 3.967 2.200 4.150 2.000 3.833 3.500 4.583 2.367 5.000 1.933 4.617 1.917 2.083 4.583 3.333 4.167 4.333 4.500 2.417 4.000 4.167 1.883 4.583 4.250 3.767 2.033 4.433 4.083 1.833 4.417 2.183 4.800 1.833 4.800 4.100 3.966 4.233 3.500 4.366 2.250 4.667 2.100 4.350 4.133 1.867 4.600 1.783 4.367 3.850 1.933 4.500 2.383 4.700 1.867 3.833 3.417 4.233 2.400 4.800 2.000 4.150 1.867 4.267 1.750 4.483 4.000 4.117 4.083 4.267 3.917 4.550 4.083 2.417 4.183 2.217 4.450 1.883 1.850 4.283 3.950 2.333 4.150 2.350 4.933 2.900 4.583 3.833 2.083 4.367 2.133 4.350 2.200 4.450 3.567 4.500 4.150 3.817 3.917 4.450 2.000 4.283 4.767 4.533 1.850 4.250 1.983 2.250 4.750 4.117 2.150 4.417 1.817 4.467)
10
+ data.each do |point|
11
+ @histogram.update(point.to_f)
12
+ end
13
+ @histogram.count.should == 272
14
+ end
15
+
16
+ it "should update variance correctly" do
17
+ @histogram.variance.should == 1.3027283328494685
18
+ end
19
+
20
+ it "should calculate standard deviations properly" do
21
+ @histogram.std_dev.should == 1.1413712511052083
22
+ end
23
+
24
+ it "should accurately calculate quantiles" do
25
+ quantiles = @histogram.quantiles([0.99, 0.98, 0.95, 0.80, 0.57, 0.32])
26
+ quantiles.should ==
27
+ {
28
+ 0.99 => 5.009570000000001,
29
+ 0.98 => 4.93300,
30
+ 0.95 => 4.81700,
31
+ 0.80 => 4.53300,
32
+ 0.57 => 4.13300,
33
+ 0.32 => 2.39524
34
+ }
35
+ end
36
+
37
+ it "should accurately calculate the mean" do
38
+ @histogram.mean.should == 3.4877830882352936
39
+ end
40
+
41
+ it "should accurately represent itself using JSON" do
42
+ @histogram.to_s.should == "{\"min\":1.6,\"max\":5.1,\"mean\":3.4877830882352936,\"variance\":1.3027283328494685,\"percentiles\":{\"0.25\":2.16275,\"0.5\":4.0,\"0.75\":4.45425,\"0.95\":4.817,\"0.97\":4.8977900000000005,\"0.98\":4.933,\"0.99\":5.009570000000001}}"
43
+ end
44
+
45
+ it "should return correct values for mean, std. deviation and variance when no elements are in the histogram" do
46
+ histogram = Metrics::Instruments::Histogram.new
47
+ histogram.variance.should == 0.0
48
+ histogram.mean.should == 0.0
49
+ histogram.std_dev.should == 0.0
50
+ end
51
+
52
+ it "should return the first value as the quantile if only one value" do
53
+ histogram = Metrics::Instruments::Histogram.new
54
+ histogram.update(42)
55
+ histogram.quantiles([0.50]).should == {0.50 => 42}
56
+ end
57
+
58
+ it "should return the last value as the 100%" do
59
+ histogram = Metrics::Instruments::Histogram.new
60
+ histogram.update(42)
61
+ histogram.update(64)
62
+ histogram.quantiles([1.00]).should == {1.00 => 64}
63
+ end
64
+
65
+
66
+ it "should return correct values for min and max" do
67
+ histogram = Metrics::Instruments::Histogram.new
68
+ histogram.update(42)
69
+ histogram.min.should == 42
70
+ histogram.max.should == 42
71
+ end
72
+
73
+ context "resetting the histogram" do
74
+
75
+ it "should clear data correctly" do
76
+ sample = Metrics::Statistics::UniformSample.new
77
+ sample.should_receive(:clear)
78
+ Metrics::Statistics::UniformSample.should_receive(:new).and_return sample
79
+
80
+ histogram = Metrics::Instruments::Histogram.new
81
+ histogram.clear
82
+ histogram.max.should == 0.0
83
+ histogram.min.should == 0.0
84
+ end
85
+
86
+ end
87
+
88
+
89
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+ require 'ruby-units'
3
+
4
+ describe Metrics::Instruments::Meter do
5
+ before(:each) do
6
+ Thread.stub!(:new).and_return do |block|
7
+ # Do nothing
8
+ end
9
+ end
10
+
11
+ it "should initialize averages to 0" do
12
+ meter = Metrics::Instruments::Meter.new
13
+ meter.one_minute_rate.should == 0.0
14
+ meter.five_minute_rate.should == 0.0
15
+ meter.fifteen_minute_rate.should == 0.0
16
+ end
17
+
18
+ it "should increment count" do
19
+ meter = Metrics::Instruments::Meter.new
20
+ meter.mark(500)
21
+ meter.counted.should == 500
22
+ meter.uncounted.should == 500
23
+ end
24
+
25
+ it "should accept options for the constructor" do
26
+ meter = Metrics::Instruments::Meter.new({:interval => "10 seconds", :rateunit => "5 seconds"})
27
+ end
28
+
29
+ context "A timer with an initial mark of 3 at a 1 second rate unit on a 5 second interval" do
30
+
31
+ before(:each) do
32
+ @meter = Metrics::Instruments::Meter.new({:interval => "5 seconds", :rateunit => "1 second"})
33
+ @meter.mark(3)
34
+ @meter.tick()
35
+ end
36
+
37
+ def tick_for(time)
38
+ count = ((time.to("seconds")) / 5).scalar
39
+ (1..count).each do
40
+ @meter.tick()
41
+ end
42
+ end
43
+
44
+ context "For a 1 minute window" do
45
+ it "should have a rate of 0.6 events/sec after the first tick" do
46
+ @meter.one_minute_rate.should == 0.6
47
+ end
48
+
49
+ it "should have a rate of 0.22072766470286553 events/sec after 1 minute" do
50
+ tick_for("1 minute")
51
+ @meter.one_minute_rate.should == 0.22072766470286553
52
+ end
53
+ end
54
+
55
+ context "For a 5 minute window" do
56
+ it "should have a rate of 0.6 events/sec after the first tick" do
57
+ @meter.five_minute_rate.should == 0.6
58
+ end
59
+
60
+ it "should have a rate of 0.49123845184678905 events/sec after 1 minute" do
61
+ tick_for("1 minute")
62
+ @meter.five_minute_rate.should == 0.49123845184678905
63
+ end
64
+ end
65
+
66
+ context "For a 15 minute window" do
67
+ it "should have a rate of 0.6 events/sec after the first tick" do
68
+ @meter.fifteen_minute_rate.should == 0.6
69
+ end
70
+
71
+ it "should have a rate of 0.5613041910189706 events/sec after 1 minute" do
72
+ tick_for("1 minute")
73
+ @meter.fifteen_minute_rate.should == 0.5613041910189706
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+
80
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe Metrics::Instruments do
4
+ before(:each) do
5
+ @instruments = Metrics::Instruments
6
+ @instruments.unregister_all
7
+
8
+ @counter = Metrics::Instruments::Counter.new
9
+ @meter = Metrics::Instruments::Meter.new
10
+ end
11
+
12
+ it "should initially have no instruments in its hash" do
13
+ @instruments.registered.should == {}
14
+ end
15
+
16
+ it "should allow for registering a Counter instrument" do
17
+ Metrics::Instruments::Counter.stub!(:new).and_return @counter
18
+ @instruments.register(:counter, :test_counter).should == @counter
19
+ @instruments.registered.should == {:test_counter => @counter}
20
+ end
21
+
22
+ it "should allow for registering a Meter instrument" do
23
+ Metrics::Instruments::Meter.stub!(:new).and_return @meter
24
+ @instruments.register(:meter, :test_meter).should == @meter
25
+ @instruments.registered.should == {:test_meter => @meter}
26
+ end
27
+
28
+ it "should generate JSON correctly" do
29
+ Metrics::Instruments::Meter.stub!(:new).and_return @meter
30
+ @instruments.register(:meter, :test_meter).should == @meter
31
+ @instruments.registered.should == {:test_meter => @meter}
32
+
33
+ @instruments.to_json.should == "{\"test_meter\":\"{\\\"one_minute_rate\\\":0.0,\\\"five_minute_rate\\\":0.0,\\\"fifteen_minute_rate\\\":0.0}\"}"
34
+ end
35
+
36
+ it "should not allow for creating a gauge with no block" do
37
+ lambda do
38
+ @instruments.register :gauge, :test_gauge
39
+ end.should raise_error(ArgumentError)
40
+ end
41
+
42
+ it "should allow for creating a gauge with a block" do
43
+ proc = Proc.new { "result" }
44
+ @instruments.register :gauge, :test_gauge, &proc
45
+ end
46
+
47
+ end
@@ -0,0 +1,14 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.start do
4
+ add_filter "/spec/"
5
+ add_group "Instruments", "lib/metrics/instruments"
6
+ merge_timeout 3600
7
+ end
8
+
9
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
+ require 'ruby-metrics'
11
+
12
+ RSpec.configure do |config|
13
+ config.mock_with :rspec
14
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Metrics::Statistics::Sample do
4
+ before(:each) do
5
+ @sample = Metrics::Statistics::Sample.new
6
+ end
7
+
8
+ %w( clear values size ).each do |method|
9
+ it "should raise a NotImplementedError for ##{method}" do
10
+ lambda do
11
+ @sample.send(method)
12
+ end.should raise_exception(NotImplementedError)
13
+ end
14
+ end
15
+
16
+ it "should raise a NotImplementedError for 'update'" do
17
+ lambda do
18
+ @sample.update(0)
19
+ end.should raise_exception(NotImplementedError)
20
+ end
21
+
22
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe Metrics::Statistics::UniformSample do
4
+ before(:each) do
5
+ end
6
+
7
+ it "should have a size equal to the initialization parameter" do
8
+ sample = Metrics::Statistics::UniformSample.new(100)
9
+ sample.size.should == 100
10
+
11
+ end
12
+
13
+ it "should allocate an array of the requested size" do
14
+ sample = Metrics::Statistics::UniformSample.new(100)
15
+ sample.values.length.should == 100
16
+
17
+ sample.values.each do |value|
18
+ value.should == 0
19
+ end
20
+ end
21
+
22
+ it "should update at the end of the list" do
23
+ sample = Metrics::Statistics::UniformSample.new(100)
24
+ (1..100).each do |i|
25
+ sample.update(i)
26
+ end
27
+
28
+ values = sample.values
29
+
30
+ (0..99).each do |index|
31
+ values[index].should == (index + 1)
32
+ end
33
+ end
34
+
35
+ it "should update a random entry in the list when it's full" do
36
+
37
+ sample = Metrics::Statistics::UniformSample.new(100)
38
+ sample.should_receive(:rand).with(any_args()).and_return(50)
39
+
40
+ (1..101).each do |i|
41
+ sample.update(i)
42
+ end
43
+
44
+ values = sample.values
45
+
46
+ (0..49).each do |index|
47
+ values[index].should == (index + 1)
48
+ end
49
+
50
+ values[50].should == 101
51
+
52
+ (51..99).each do |index|
53
+ values[index].should == (index + 1)
54
+ end
55
+
56
+ end
57
+
58
+
59
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-metrics
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 5
8
+ - 0
9
+ version: 0.5.0
10
+ platform: ruby
11
+ authors:
12
+ - John Ewart
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-04-13 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: json
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ prerelease: false
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: ruby-units
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
42
+ version: "0"
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: &id003 !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: simplecov
61
+ requirement: &id004 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ - 3
69
+ - 8
70
+ version: 0.3.8
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: *id004
74
+ description: A Ruby implementation of metrics inspired by @coda's JVM metrics for those of us in Ruby land
75
+ email:
76
+ - john@johnewart.net
77
+ executables: []
78
+
79
+ extensions: []
80
+
81
+ extra_rdoc_files: []
82
+
83
+ files:
84
+ - .gitignore
85
+ - .rvmrc
86
+ - Gemfile
87
+ - Gemfile.lock
88
+ - LICENSE
89
+ - README.md
90
+ - Rakefile
91
+ - autotest/discover.rb
92
+ - examples/counter.rb
93
+ - examples/gauge.rb
94
+ - examples/histogram.rb
95
+ - examples/meter.rb
96
+ - lib/ruby-metrics.rb
97
+ - lib/ruby-metrics/agent.rb
98
+ - lib/ruby-metrics/instruments.rb
99
+ - lib/ruby-metrics/instruments/base.rb
100
+ - lib/ruby-metrics/instruments/counter.rb
101
+ - lib/ruby-metrics/instruments/gauge.rb
102
+ - lib/ruby-metrics/instruments/histogram.rb
103
+ - lib/ruby-metrics/instruments/meter.rb
104
+ - lib/ruby-metrics/logging.rb
105
+ - lib/ruby-metrics/statistics/sample.rb
106
+ - lib/ruby-metrics/statistics/uniform_sample.rb
107
+ - lib/ruby-metrics/version.rb
108
+ - ruby-metrics.gemspec
109
+ - spec/agent_spec.rb
110
+ - spec/instruments/base_spec.rb
111
+ - spec/instruments/counter_spec.rb
112
+ - spec/instruments/gauge_spec.rb
113
+ - spec/instruments/histogram_spec.rb
114
+ - spec/instruments/meter_spec.rb
115
+ - spec/instruments_spec.rb
116
+ - spec/spec_helper.rb
117
+ - spec/statistics/sample_spec.rb
118
+ - spec/statistics/uniform_sample_spec.rb
119
+ has_rdoc: true
120
+ homepage: https://github.com/johnewart/ruby-metrics
121
+ licenses: []
122
+
123
+ post_install_message:
124
+ rdoc_options: []
125
+
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ hash: -4356011955923517565
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ hash: -4356011955923517565
143
+ segments:
144
+ - 0
145
+ version: "0"
146
+ requirements: []
147
+
148
+ rubyforge_project: ruby-metrics
149
+ rubygems_version: 1.3.7
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: Metrics for Ruby
153
+ test_files:
154
+ - spec/agent_spec.rb
155
+ - spec/instruments/base_spec.rb
156
+ - spec/instruments/counter_spec.rb
157
+ - spec/instruments/gauge_spec.rb
158
+ - spec/instruments/histogram_spec.rb
159
+ - spec/instruments/meter_spec.rb
160
+ - spec/instruments_spec.rb
161
+ - spec/spec_helper.rb
162
+ - spec/statistics/sample_spec.rb
163
+ - spec/statistics/uniform_sample_spec.rb