Source:
public void testStats() { for (int i = 0; i < TOTAL_ENTRIES; i++) { cache.put("key" + i, "value" + i); } refreshStats(); assertEquals(TOTAL_ENTRIES, stats.getCurrentNumberOfEntries()); assertEquals(EVICTION_MAX_ENTRIES, stats.getCurrentNumberOfEntriesInMemory()); eventuallyEquals((long) TOTAL_ENTRIES - EVICTION_MAX_ENTRIES, () -> { refreshStats(); return stats.getEvictions(); }); cache.evict("key0"); refreshStats(); assertEquals(TOTAL_ENTRIES - EVICTION_MAX_ENTRIES + 1, stats.getEvictions()); assertEquals("value1", cache.get("key1")); assertEquals("value2", cache.get("key2")); assertEquals("value1", cache.remove("key1")); assertEquals("value2", cache.remove("key2")); assertNull(cache.remove("non-existing")); assertNull(cache.get("key1")); assertNull(cache.get("key2")); refreshStats(); assertEquals(2, stats.getHits()); assertEquals(2, stats.getMisses()); assertEquals(2, stats.getRemoveHits()); assertEquals(1, stats.getRemoveMisses()); assertEquals(4, stats.getRetrievals()); assertEquals(TOTAL_ENTRIES, stats.getStores()); cache.put("other-key", "value"); refreshStats(); assertEquals(TOTAL_ENTRIES + 1, stats.getTotalNumberOfEntries()); assertTrue(stats.getAverageReadTime() >= 0); assertTrue(stats.getAverageRemoveTime() >= 0); assertTrue(stats.getAverageWriteTime() >= 0); assertTrue(stats.getAverageReadTimeNanos() >= 0); assertTrue(stats.getAverageRemoveTimeNanos() >= 0); assertTrue(stats.getAverageWriteTimeNanos() >= 0); if (evictionType == EvictionType.COUNT) { assertEquals(0, stats.getDataMemoryUsed()); } else { assertTrue(stats.getDataMemoryUsed() > 0); } if (storageType == StorageType.OFF_HEAP) { assertTrue(stats.getOffHeapMemoryUsed() > 0); } }

Target:
public void testStats() { for (int i = 0; i < TOTAL_ENTRIES; i++) { cache.put("key" + i, "value" + i); } refreshStats(); assertEquals(TOTAL_ENTRIES, stats.getCurrentNumberOfEntries()); assertEquals(EVICTION_MAX_ENTRIES, stats.getCurrentNumberOfEntriesInMemory()); eventuallyEquals((long) TOTAL_ENTRIES - EVICTION_MAX_ENTRIES, () -> { refreshStats(); return stats.getEvictions(); }); int additionalMisses = 0; for (int i = 0; i < TOTAL_ENTRIES; i++) { Cache skipLoaderCache = cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD); String key = "key" + i; if (skipLoaderCache.containsKey(key)) { cache.evict(key); break; } additionalMisses++; } refreshStats(); assertEquals(TOTAL_ENTRIES - EVICTION_MAX_ENTRIES + 1, stats.getEvictions()); assertEquals("value1", cache.get("key1")); assertEquals("value2", cache.get("key2")); assertEquals("value1", cache.remove("key1")); assertEquals("value2", cache.remove("key2")); assertNull(cache.remove("non-existing")); assertNull(cache.get("key1")); assertNull(cache.get("key2")); refreshStats(); assertEquals(3, stats.getHits()); assertEquals(2 + additionalMisses, stats.getMisses()); assertEquals(2, stats.getRemoveHits()); assertEquals(1, stats.getRemoveMisses()); assertEquals(5 + additionalMisses, stats.getRetrievals()); assertEquals(TOTAL_ENTRIES, stats.getStores()); cache.put("other-key", "value"); refreshStats(); assertEquals(TOTAL_ENTRIES + 1, stats.getTotalNumberOfEntries()); assertTrue(stats.getAverageReadTime() >= 0); assertTrue(stats.getAverageRemoveTime() >= 0); assertTrue(stats.getAverageWriteTime() >= 0); assertTrue(stats.getAverageReadTimeNanos() >= 0); assertTrue(stats.getAverageRemoveTimeNanos() >= 0); assertTrue(stats.getAverageWriteTimeNanos() >= 0); if (evictionType == EvictionType.COUNT) { assertEquals(0, stats.getDataMemoryUsed()); } else { assertTrue(stats.getDataMemoryUsed() > 0); } if (storageType == StorageType.OFF_HEAP) { assertTrue(stats.getOffHeapMemoryUsed() > 0); } }