@Test public void testEvictionLFU() { try { final int k = 1; final int size = 10000; final String mapName = "testEvictionLFU"; Config cfg = new Config(); MapConfig mc = cfg.getMapConfig(mapName); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LFU); mc.setEvictionPercentage(20); MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE); msc.setSize(size); mc.setMaxSizeConfig(msc); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(cfg); IMap<Object, Object> map = instances[0].getMap(mapName); for (int i = 0; i < size / 2; i++) { map.put(i, i); map.get(i); } Thread.sleep(1000); for (int i = size / 2; i < size; i++) { map.put(i, i); } Thread.sleep(3000); Assert.assertFalse("No eviction!?!?!?", map.size() == size); boolean isFrequentlyUsedEvicted = false; for (int i = 0; i < size / 2; i++) { if (map.get(i) == null) { isFrequentlyUsedEvicted = true; break; } } Assert.assertFalse(isFrequentlyUsedEvicted); instances[0].getLifecycleService().shutdown(); } catch (InterruptedException e) { e.printStackTrace(); } }
@Test public void testEvictionLRU() { final int k = 2; final int size = 10000; try { final String mapName = "testEvictionLRU"; Config cfg = new Config(); MapConfig mc = cfg.getMapConfig(mapName); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU); mc.setEvictionPercentage(10); MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE); msc.setSize(size); mc.setMaxSizeConfig(msc); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(cfg); IMap<Object, Object> map = instances[0].getMap(mapName); Thread.sleep(1000); for (int i = size / 2; i < size; i++) { map.put(i, i); } Thread.sleep(2000); for (int i = 0; i < size / 2; i++) { map.put(i, i); } Thread.sleep(1000); int recentlyUsedEvicted = 0; for (int i = 0; i < size / 2; i++) { if (map.get(i) == null) { recentlyUsedEvicted++; } } Assert.assertTrue(recentlyUsedEvicted == 0); } catch (InterruptedException e) { e.printStackTrace(); } }
@Test public void testEvictionLFU2() { try { final int k = 2; final int size = 10000; final String mapName = "testEvictionLFU2"; Config cfg = new Config(); MapConfig mc = cfg.getMapConfig(mapName); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LFU); mc.setEvictionPercentage(90); MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE); msc.setSize(size); mc.setMaxSizeConfig(msc); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(cfg); IMap<Object, Object> map = instances[0].getMap(mapName); for (int i = 0; i < size; i++) { map.put(i, i); } for (int i = 0; i < 3; i++) { for (int j = 0; j < 100; j++) { assertNotNull(map.get(j)); } for (int j = size - 100; j < size; j++) { assertNotNull(map.get(j)); } Thread.sleep(1000); } } catch (Exception e) { e.printStackTrace(); } }
@Test public void testEvictionPerPartition() throws InterruptedException { final int k = 2; final int size = 10; final String mapName = "testEvictionPerPartition"; Config cfg = new Config(); final MapConfig mc = cfg.getMapConfig(mapName); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU); mc.setEvictionPercentage(25); final MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_PARTITION); msc.setSize(size); mc.setMaxSizeConfig(msc); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(cfg); final int pnum = instances[0].getPartitionService().getPartitions().size(); int insertCount = size * pnum * 2; Map map = instances[0].getMap(mapName); for (int i = 0; i < insertCount; i++) { map.put(i, i); } Thread.sleep(2000); assertTrue(map.size() < size * pnum * (100 - mc.getEvictionPercentage()) / 100); }
@Test public void testMapWideEviction() throws InterruptedException { int size = 10000; Config cfg = new Config(); MapConfig mc = cfg.getMapConfig("testMapWideEviction"); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU); mc.setEvictionPercentage(25); MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE); msc.setSize(size); mc.setMaxSizeConfig(msc); int n = 3; TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n); final HazelcastInstance[] instances = factory.newInstances(cfg); IMap map = instances[0].getMap("testMapWideEviction"); for (int i = 0; i < size; i++) { map.put(i, i); } Thread.sleep(1200); assertTrue(map.size() <= (size * n * (100 - mc.getEvictionPercentage()) / 100)); }
@Test public void testEvictionSpeedTestPerPartition() { final int k = 2; final int size = 100; final CountDownLatch latch = new CountDownLatch(k); final String mapName = "testEvictionSpeedTestPerPartition"; Config cfg = new Config(); final MapConfig mc = cfg.getMapConfig(mapName); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU); mc.setEvictionPercentage(25); final MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_PARTITION); msc.setSize(size); mc.setMaxSizeConfig(msc); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(cfg); final int pnum = instances[0].getPartitionService().getPartitions().size(); final AtomicInteger failCount = new AtomicInteger(0); new Thread() { final IMap map = instances[0].getMap(mapName); public void run() { try { Thread.sleep(1000); while (latch.getCount() != 0) { try { int msize = map.size(); if (msize > (size * pnum * 1.2)) { failCount.incrementAndGet(); } Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); for (int i = 0; i < k; i++) { final IMap map = instances[i].getMap(mapName); new Thread() { public void run() { for (int j = 0; j < 100000; j++) { map.put(k + "-" + j, j); } latch.countDown(); } }.start(); } try { Assert.assertEquals(latch.await(10, TimeUnit.MINUTES), true); Assert.assertEquals(0, failCount.get()); } catch (InterruptedException e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
// current eviction check period is 1 second. // about 30.000 records can be put in one second // so the size should be adapted @Test public void testEvictionSpeedTest() { final int k = 3; final int size = 10000; final CountDownLatch latch = new CountDownLatch(k); final String mapName = "testEvictionSpeedTest"; Config cfg = new Config(); final MapConfig mc = cfg.getMapConfig(mapName); mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU); mc.setEvictionPercentage(25); final MaxSizeConfig msc = new MaxSizeConfig(); msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE); msc.setSize(size); mc.setMaxSizeConfig(msc); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k); final HazelcastInstance[] instances = factory.newInstances(cfg); final AtomicBoolean success = new AtomicBoolean(true); new Thread() { final IMap map = instances[0].getMap(mapName); public void run() { try { Thread.sleep(1000); while (latch.getCount() != 0) { try { int msize = map.size(); if (msize > (size * k + size * k * 10 / 100)) { success.set(false); break; } Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); for (int i = 0; i < k; i++) { final IMap map = instances[i].getMap(mapName); new Thread() { public void run() { for (int j = 0; j < size; j++) { map.put(k + "-" + j, j); } latch.countDown(); } }.start(); } try { Assert.assertTrue(latch.await(10, TimeUnit.MINUTES)); Assert.assertTrue(success.get()); } catch (InterruptedException e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }