def setUp(self): test_directory = os.path.dirname(os.path.realpath(__file__)) settings = { 'CONF_DIR': os.path.join(test_directory, 'data', 'conf-directory'), 'USE_INSECURE_PICKLER': False } self._settings_patch = patch.dict('carbon.conf.settings', settings) self._settings_patch.start() from carbon.protocols import CacheManagementHandler self.handler = CacheManagementHandler() self.cache = _MetricCache() def _get_cache(): return self.cache self._metriccache_patch = patch('carbon.protocols.MetricCache', _get_cache) self._metriccache_patch.start() self.handler.unpickler = pickle self.handler.peerAddr = 'localhost:7002' self.send_string_mock = Mock(side_effect=self._save_response) self._send_string_patch = patch.object(self.handler, 'sendString', self.send_string_mock) self._send_string_patch.start()
def test_store_checks_fullness(self): is_full_mock = PropertyMock() with patch.object(_MetricCache, 'is_full', is_full_mock): with patch('carbon.cache.events'): metric_cache = _MetricCache() metric_cache.store('foo', (123456, 1.0)) self.assertEqual(1, is_full_mock.call_count)
def setUp(self): settings = { 'MAX_CACHE_SIZE': float('inf'), 'CACHE_SIZE_LOW_WATERMARK': float('inf') } self._settings_patch = patch.dict('carbon.conf.settings', settings) self._settings_patch.start() self.strategy_mock = Mock(spec=DrainStrategy) self.metric_cache = _MetricCache(self.strategy_mock)
def test_write_strategy_naive(self): """Create a metric cache, insert metrics, ensure naive writes""" config = self.makeFile(content="[foo]\nCACHE_WRITE_STRATEGY = naive") settings = read_config("carbon-foo", FakeOptions(config=config, instance=None, pidfile=None, logdir=None), ROOT_DIR="foo") cache = _MetricCache(method=settings.CACHE_WRITE_STRATEGY) self.assertEqual("naive", cache.method)
def test_write_strategy_max(self): """Create a metric cache, insert metrics, ensure naive writes""" config = self.makeFile(content="[foo]\nCACHE_WRITE_STRATEGY = max") settings = read_config("carbon-foo", FakeOptions(config=config, instance=None, pidfile=None, logdir=None), ROOT_DIR="foo") cache = _MetricCache(method=settings.CACHE_WRITE_STRATEGY) self.assertEqual("max", cache.method) now = time.time() datapoint1 = (now - 10, float(1)) datapoint2 = (now, float(2)) cache.store("d.e.f", datapoint1) cache.store("a.b.c", datapoint1) cache.store("a.b.c", datapoint2) (m, d) = cache.pop() self.assertEqual(("a.b.c", deque([datapoint1, datapoint2])), (m, d)) (m, d) = cache.pop() self.assertEqual(("d.e.f", deque([datapoint1])), (m, d)) self.assertEqual(0, cache.size)
def setUp(self): self.metric_cache = _MetricCache()
def test_drain_metric_works_without_strategy(self): metric_cache = _MetricCache() # No strategy metric_cache.store('foo', (123456, 1.0)) self.assertEqual('foo', metric_cache.drain_metric()[0])
import timeit from carbon.cache import _MetricCache, DrainStrategy, \ NaiveStrategy, MaxStrategy, RandomStrategy, SortedStrategy, TimeSortedStrategy metric_cache = _MetricCache(DrainStrategy) count = 0 strategies = { 'naive': NaiveStrategy, 'max': MaxStrategy, 'random': RandomStrategy, 'sorted': SortedStrategy, 'timesorted': TimeSortedStrategy, } def command_store_foo(): global count count = count + 1 return metric_cache.store('foo', (count, 1.0)) def command_store_foo_n(): global count count = count + 1 return metric_cache.store("foo.%d" % count, (count, 1.0)) def command_drain(): while metric_cache: metric_cache.drain_metric()
import timeit from carbon.cache import _MetricCache, DrainStrategy, \ NaiveStrategy, MaxStrategy, RandomStrategy, SortedStrategy, TimeSortedStrategy metric_cache = _MetricCache(DrainStrategy) count = 0 strategies = { 'naive': NaiveStrategy, 'max': MaxStrategy, 'random': RandomStrategy, 'sorted': SortedStrategy, 'timesorted': TimeSortedStrategy, } def command_store_foo(): global count count = count + 1 return metric_cache.store('foo', (count, 1.0)) def command_store_foo_n(): global count count = count + 1 return metric_cache.store("foo.%d" % count, (count, 1.0)) def command_drain(): while metric_cache: metric_cache.drain_metric() return metric_cache.size
def setUp(self): settings = {"MAX_CACHE_SIZE": float("inf"), "CACHE_SIZE_LOW_WATERMARK": float("inf")} self._settings_patch = patch.dict("carbon.conf.settings", settings) self._settings_patch.start() self.strategy_mock = Mock(spec=DrainStrategy) self.metric_cache = _MetricCache(self.strategy_mock)
import timeit from carbon.cache import _MetricCache, DrainStrategy, \ NaiveStrategy, MaxStrategy, RandomStrategy, SortedStrategy, \ TimeSortedStrategy, BucketMaxStrategy metric_cache = _MetricCache(DrainStrategy) count = 0 strategies = { 'naive': NaiveStrategy, 'max': MaxStrategy, 'random': RandomStrategy, 'sorted': SortedStrategy, 'timesorted': TimeSortedStrategy, 'bucketmax': BucketMaxStrategy, } def command_store_foo(): global count count = count + 1 return metric_cache.store('foo', (count, 1.0)) def command_store_foo_n(): global count count = count + 1 return metric_cache.store("foo.%d" % count, (count, 1.0)) def command_drain():