Skip to content

Commit

Permalink
[ryan] added derivative stat
Browse files Browse the repository at this point in the history
  • Loading branch information
perf committed May 29, 2012
1 parent a74c9e6 commit 982bdd0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .rvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rvm ree@am
rvm 1.9.3-p0@am
9 changes: 8 additions & 1 deletion lib/active_metric/stat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def calculate(measurement)
def complete
end

def sample_duration_in_seconds
calculable.duration_in_seconds
end

def summary_duration_in_seconds
subject.summary.duration_in_seconds
end

def self.class_for(stat)
eval(stat.to_s.classify)
end
Expand All @@ -50,7 +58,6 @@ def self.create_custom_stat(name_of_stat, value_type, default, axis, calculate_b
klass.send(:field, :value, :type => value_type, :default => default)
ActiveMetric.const_set(class_name,klass)
return klass

end

def subject
Expand Down
17 changes: 17 additions & 0 deletions lib/active_metric/statistics/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ def complete
end
end

class Derivative < Stat
field :first
field :last

def calculate(measurement)
self.last = (measurement.send(self.property))
self.first ||= self.last
end

def complete
self.value = (self.last - self.first).to_f / sample_duration_in_seconds
super
end

end

class Eightieth < Stat
def calculate(measurement)
end
Expand All @@ -44,4 +60,5 @@ def complete
super
end
end

end
9 changes: 8 additions & 1 deletion test/stat_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class StatTest < ActiveSupport::TestCase
assert_equal 4.5, stat.value
end

test "can calculate derivative" do
stat = Derivative.new(:value, :calculable => @sample)
stat.expects(:sample_duration_in_seconds).returns(10)
test_stat(stat, 10.times)
assert_equal 0.9, stat.value
end

#this test is here for the user, not for automated tests
#test "random distributions are good too" do
# stat = StandardDeviation.new(:value, :calculable => @sample)
Expand All @@ -79,7 +86,7 @@ class StatTest < ActiveSupport::TestCase

def test_stat(stat, values)
values.each do |value|
stat.calculate TestMeasurement.new(:value => value)
stat.calculate TestMeasurement.new(:value => value, :timestamp => value)
end
stat.complete
end
Expand Down

0 comments on commit 982bdd0

Please sign in to comment.