Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add derive instrument
- Loading branch information
1 parent
ae17368
commit cbde378
Showing
6 changed files
with
72 additions
and
1 deletion.
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
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,28 @@ | ||
from atomic import Atomic | ||
|
||
from metrology.instruments.meter import Meter | ||
from metrology.stats import EWMA | ||
|
||
|
||
class Derive(Meter): | ||
""" | ||
A derive is like a meter but accepts an absolute counter as input. | ||
derive = Metrology.derive('network.io') | ||
derive.mark() | ||
derive.count | ||
""" | ||
def __init__(self, average_class=EWMA): | ||
self.last = Atomic(0) | ||
super(Derive, self).__init__(average_class) | ||
|
||
def mark(self, value=1): | ||
"""Record an event with the derive. | ||
:param value: counter value to record | ||
""" | ||
last = self.last.get_and_set(value) | ||
if last <= value: | ||
value = value - last | ||
super(Derive, self).mark(value) |
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,28 @@ | ||
from unittest import TestCase | ||
|
||
from metrology.instruments.derive import Derive | ||
|
||
|
||
class DeriveTest(TestCase): | ||
def setUp(self): | ||
self.derive = Derive() | ||
|
||
def test_derive(self): | ||
self.derive.mark() | ||
self.assertEqual(1, self.derive.count) | ||
|
||
def test_blank_derive(self): | ||
self.assertEqual(0, self.derive.count) | ||
self.assertEqual(0.0, self.derive.mean_rate) | ||
|
||
def test_derive_value(self): | ||
self.derive.mark(3) | ||
self.assertEqual(3, self.derive.count) | ||
|
||
def test_one_minute_rate(self): | ||
self.derive.mark(1000) | ||
self.derive.tick() | ||
self.assertEqual(200, self.derive.one_minute_rate) | ||
|
||
def tearDown(self): | ||
self.derive.stop() |