forked from johnewart/ruby-metrics
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding sampling classes to implement Histograms
- Loading branch information
Showing
5 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |