def test_cache_clear_resets_memory(): m = LRU(2) m.set('a', 1, 1) assert m.get_memory() == 1 m.clear() assert m.get_memory() == 0 assert 'a' not in m.keys()
def test_bench_with_lru(benchmark, data, collector): m = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss c2 = LRUS(300 * M) benchmark.pedantic(run_cache, args=(c2, data), iterations=1, rounds=100) assert c2.get_memory() <= (300 * M) hits, misses, clean = c2.get_stats() collector( dict(hits=hits, misses=misses, clean=clean, items=len(c2.keys()), memory=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss - m))
def test_lrusized_acts_like_a_dict(): m = LRU(1024) m.set('a', _bytes, 3) assert m['a'] == _bytes assert 'a' in m assert m.get('a') == _bytes assert m.get_memory() == 3 del m['a'] assert len(m.keys()) == 0 assert m.get_memory() == 0
def test_cache_stats_are_hit(): m = LRU(1) try: b = m['a'] except KeyError: pass assert m.get_stats() == (0, 1, 0) m.set('a', 1, 1) assert m['a'] is 1 assert m.get_stats() == (1, 1, 0)
def get_memory_cache(): global _lru if _lru is None: settings = app_settings.get('redis', {'memory_cache_size': 209715200}) _lru = LRU(settings['memory_cache_size']) return _lru
serie.append(('w', key, random.choice(items))) else: serie.append(('r', key)) return serie K = 1024 M = K * 1024 P4 = [ K, K, K, M, 5 * M, K / 2, K / 2, K / 2, K, K, K, K, K, K / 2, K / 4, K / 3, 2 * M, K, K, K, K, K, K, K, K * 5 ] def get_memory(): gc.collect() return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss if __name__ == "__main__": serie = create_serie(gen_data(P4), action(_passes)) c = LRU(300 * M) pase = 1 while True: run_cache(c, serie) print( f'Pase: {pase} \t Memory: {get_memory()}/{c.get_memory()} // {c.get_stats()}' ) time.sleep(0.2) pase += 1
def test_setting_a_bigger_value_than_cache_doesnt_brake(): m = LRU(1) m.set('a', 'v', 100) assert 'a' not in m.keys()
def test_clean_till_it_has_enought_space(): m = LRU(19) for k in range(20): m.set(k, k, 1) m.set('a', 1, 1) assert 1 not in m.keys() r = m[2] assert r == 2 m.set('b', 1, 1) assert 2 in m.keys() assert 3 not in m.keys() m.set('b', 1, 10) assert len(m.keys()) is 10 assert 2 in m.keys() assert m.get_memory() is 19 del m['b'] assert m.get_memory() is 9 assert len(m.keys()) is 9 # we should cleanup 12 keys assert m.get_stats() == (1, 0, 12)