class CPUUsageMonitor(service.MultiService):
implements(IStatsProducer)
- MINUTES = 15
+ HISTORY_LENGTH = 15
+ POLL_INTERVAL = 60
def __init__(self):
service.MultiService.__init__(self)
eventually(self._set_initial_cpu)
self.samples = []
# we provide 1min, 5min, and 15min moving averages
- TimerService(60, self.check).setServiceParent(self)
+ TimerService(self.POLL_INTERVAL, self.check).setServiceParent(self)
def _set_initial_cpu(self):
self.initial_cpu = time.clock()
now_wall = time.time()
now_cpu = time.clock()
self.samples.append( (now_wall, now_cpu) )
- while len(self.samples) > self.MINUTES+1:
+ while len(self.samples) > self.HISTORY_LENGTH+1:
self.samples.pop(0)
def _average_N_minutes(self, size):
--- /dev/null
+
+from twisted.trial import unittest
+from twisted.application import service
+from allmydata.stats import CPUUsageMonitor
+from allmydata.util import testutil
+
+class FasterMonitor(CPUUsageMonitor):
+ POLL_INTERVAL = 0.1
+
+
+class CPUUsage(unittest.TestCase, testutil.PollMixin):
+ def setUp(self):
+ self.s = service.MultiService()
+ self.s.startService()
+
+ def tearDown(self):
+ return self.s.stopService()
+
+ def test_monitor(self):
+ m = FasterMonitor()
+ s = m.get_stats() # before it has been started
+ self.failIf("cpu_monitor.1min_avg" in s)
+ m.setServiceParent(self.s)
+ def _poller():
+ return bool(len(m.samples) == m.HISTORY_LENGTH+1)
+ d = self.poll(_poller)
+ def _check(res):
+ s = m.get_stats()
+ self.failUnless("cpu_monitor.1min_avg" in s)
+ self.failUnless("cpu_monitor.5min_avg" in s)
+ self.failUnless("cpu_monitor.15min_avg" in s)
+ self.failUnless("cpu_monitor.total" in s)
+ d.addCallback(_check)
+ return d
+