def test_cache_whole_structure(self): def run(): for key, value in FILTER_KEYS.iteritems(): # list_ might be, for example, "hosts" list_ = getattr(livestatus, key) list_raw = list_.raw() for el in list_raw: if value is not None: getattr(list_, el[value]) # Non-cached run livestatus._query._cache = False livestatus.clear_cache() with Timer() as t_non_cached: run() # Cached run livestatus._query._cache = True with Timer() as t_caching: livestatus.cache_whole_structure() cached_items = len(livestatus._query._cache_call) # Repeat the run again and set the port to some silly address. # It should raise an error if something was not cached # Note: *do not* set the port with livestatus.set_server_address # as this will invalidate the cache (which we are trying to test) livestatus._query.peer = ("127.0.0.1", 50001) with Timer() as t_cached: run() print print cached_items, "items in cache" print "Duration of caching:", t_caching.msecs print "Duration of cached run:", t_cached.msecs print "Duration of non-cached run:", t_non_cached.msecs print "The duration of the non-cached run was more than %d "\ "times longer"%\ (t_non_cached.msecs/t_cached.msecs) # Reset the socket port to its normal value livestatus._query.peer = SERVER_ADDRESS livestatus.clear_cache() self.assertEqual(len(livestatus._query._cache_call), 0) self.assertGreater(t_non_cached.msecs, t_cached.msecs)
def test_cache_whole_structure(self): def run(): for key, value in FILTER_KEYS.iteritems(): # list_ might be, for example, "hosts" list_ = getattr(livestatus, key) list_raw = list_.raw() for el in list_raw: if value is not None: getattr(list_, el[value]) # Non-cached run livestatus._query._cache = False livestatus.clear_cache() with Timer() as t_non_cached: run() # Cached run livestatus._query._cache = True with Timer() as t_caching: livestatus.cache_whole_structure() cached_items = len(livestatus._query._cache_call) # Repeat the run again and set the port to some silly address. # It should raise an error if something was not cached # Note: *do not* set the port with livestatus.set_server_address # as this will invalidate the cache (which we are trying to test) livestatus._query.peer = ("127.0.0.1", 50001) with Timer() as t_cached: run() print print cached_items, "items in cache" print "Duration of caching:", t_caching.msecs print "Duration of cached run:", t_cached.msecs print "Duration of non-cached run:", t_non_cached.msecs print "The duration of the non-cached run was more than %d "\ "times longer"%\ (t_non_cached.msecs/t_cached.msecs) # Reset the socket port to its normal value livestatus._query.peer = SERVER_ADDRESS livestatus.clear_cache() self.assertEqual(len(livestatus._query._cache_call), 0) self.assertGreater(t_non_cached.msecs, t_cached.msecs)
def test_caching(self): def run(): for i in range(5): livestatus.hosts.blackmamba.services # Cached run livestatus._query._cache = True with Timer() as t_cached: run() # Non-cached run livestatus._query._cache = False livestatus.clear_cache() with Timer() as t_non_cached: run() print print "Duration of cached run:", t_cached.msecs print "Duration of non-cached run:", t_non_cached.msecs print "The duration of the non-cached run was more than %d "\ "times longer"%\ (t_non_cached.msecs/t_cached.msecs) livestatus.clear_cache() self.assertEqual(len(livestatus._query._cache_call), 0) self.assertGreater(t_non_cached.msecs, t_cached.msecs)
def test_caching(self): def run(): for i in range(5): livestatus.hosts.blackmamba.services # Cached run livestatus._query._cache = True with Timer() as t_cached: run() # Non-cached run livestatus._query._cache = False livestatus.clear_cache() with Timer() as t_non_cached: run() print print "Duration of cached run:", t_cached.msecs print "Duration of non-cached run:", t_non_cached.msecs print "The duration of the non-cached run was more than %d "\ "times longer"%\ (t_non_cached.msecs/t_cached.msecs) livestatus.clear_cache() self.assertEqual(len(livestatus._query._cache_call), 0) self.assertGreater(t_non_cached.msecs, t_cached.msecs)