-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add support for derivative metrics #22
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,13 @@ | |||
require 'atomic' | |||
|
|||
require 'metriks/meter' | |||
|
|||
module Metriks | |||
class Derive < Metriks::Meter | |||
def mark(val = 1) | |||
@last ||= Atomic.new(val) | |||
last = @last.swap(val) | |||
super(last > val ? val : val - last) | |||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One problem with doing this simplistically is that the rate calculations, etc. are all updated every 5 seconds. If a counter is polled (and I'll have to dig into the underlying classes more to see if there's a reasonable way to do this sort of thing, but it may be that a meter isn't the right thing to be using as an underlying implementation for a derived counter. |
|||
end | |||
end | |||
end |
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,32 @@ | |||
require 'test_helper' | |||
|
|||
require 'metriks/derive' | |||
|
|||
class DeriveTest < Test::Unit::TestCase | |||
include ThreadHelper | |||
|
|||
def setup | |||
@meter = Metriks::Derive.new | |||
end | |||
|
|||
def teardown | |||
@meter.stop | |||
end | |||
|
|||
def test_meter | |||
@meter.mark(100) | |||
@meter.mark(150) | |||
|
|||
assert_equal 50, @meter.count | |||
end | |||
|
|||
def test_one_minute_rate | |||
@meter.mark(1000) | |||
@meter.mark(2000) | |||
|
|||
# Pretend it's been 5 seconds | |||
@meter.tick | |||
|
|||
assert_equal 200, @meter.one_minute_rate | |||
end | |||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be initialized in the constructor to be threadsafe.