Skip to content

Commit

Permalink
Adding sampling classes to implement Histograms
Browse files Browse the repository at this point in the history
  • Loading branch information
johnewart committed Apr 11, 2011
1 parent 0f4a5f7 commit 1cb5799
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/metrics/instruments.rb
Expand Up @@ -3,6 +3,9 @@
require File.join(File.dirname(__FILE__), 'instruments', 'meter')
require File.join(File.dirname(__FILE__), 'instruments', 'gauge')

require File.join(File.dirname(__FILE__), 'statistics', 'sample')
require File.join(File.dirname(__FILE__), 'statistics', 'uniform_sample')

require 'json'

module Metrics
Expand Down
21 changes: 21 additions & 0 deletions lib/metrics/statistics/sample.rb
@@ -0,0 +1,21 @@
module Metrics
module Statistics
class Sample
def clear
raise NotImplementedError
end

def size
raise NotImplementedError
end

def update(value)
raise NotImplementedError
end

def values
raise NotImplementedError
end
end
end
end
38 changes: 38 additions & 0 deletions lib/metrics/statistics/uniform_sample.rb
@@ -0,0 +1,38 @@
module Metrics
module Statistics
class UniformSample < Sample

def initialize(size)
@values = Array.new(size)
@count = 0
@size = size
self.clear
end

def clear
(0..@values.length-1).each do |i|
@values[i] = 0
end
@count = 0
end

def size
@values.length
end

def update(value)
if @count < @values.length
@values[@count] = value
@count += 1
else
index = rand(@size) % @count
@values[index] = value
end
end

def values
@values.dup
end
end
end
end
22 changes: 22 additions & 0 deletions spec/statistics/sample_spec.rb
@@ -0,0 +1,22 @@
require 'spec_helper'

describe Metrics::Statistics::Sample do
before(:each) do
@sample = Metrics::Statistics::Sample.new
end

%w( clear values size ).each do |method|
it "should raise a NotImplementedError for ##{method}" do
lambda do
@sample.send(method)
end.should raise_exception(NotImplementedError)
end
end

it "should raise a NotImplementedError for 'update'" do
lambda do
@sample.update(0)
end.should raise_exception(NotImplementedError)
end

end
59 changes: 59 additions & 0 deletions spec/statistics/uniform_sample_spec.rb
@@ -0,0 +1,59 @@
require 'spec_helper'

describe Metrics::Statistics::UniformSample do
before(:each) do
end

it "should have a size equal to the initialization parameter" do
sample = Metrics::Statistics::UniformSample.new(100)
sample.size.should == 100

end

it "should allocate an array of the requested size" do
sample = Metrics::Statistics::UniformSample.new(100)
sample.values.length.should == 100

sample.values.each do |value|
value.should == 0
end
end

it "should update at the end of the list" do
sample = Metrics::Statistics::UniformSample.new(100)
(1..100).each do |i|
sample.update(i)
end

values = sample.values

(0..99).each do |index|
values[index].should == (index + 1)
end
end

it "should update a random entry in the list when it's full" do

sample = Metrics::Statistics::UniformSample.new(100)
sample.should_receive(:rand).with(any_args()).and_return(50)

(1..101).each do |i|
sample.update(i)
end

values = sample.values

(0..49).each do |index|
values[index].should == (index + 1)
end

values[50].should == 101

(51..99).each do |index|
values[index].should == (index + 1)
end

end


end

0 comments on commit 1cb5799

Please sign in to comment.