use of com.hazelcast.nearcache.NearCacheStats in project hazelcast by hazelcast.
the class NearCacheTest method testNearCacheStats.
@Test
public void testNearCacheStats() {
int mapSize = 1000;
String mapName = randomMapName();
Config config = getConfig();
config.getMapConfig(mapName).setNearCacheConfig(newNearCacheConfig().setInvalidateOnChange(false));
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
HazelcastInstance[] instances = factory.newInstances(config);
// populate map
IMap<Integer, Integer> map = instances[0].getMap(mapName);
populateMap(map, mapSize);
// populate Near Cache
populateNearCache(map, mapSize);
NearCacheStats stats = getNearCacheStats(map);
long ownedEntryCount = stats.getOwnedEntryCount();
long misses = stats.getMisses();
assertTrue(format("Near Cache entry count should be > %d but were %d", 400, ownedEntryCount), ownedEntryCount > 400);
assertEquals(format("Near Cache misses should be %d but were %d", mapSize, misses), mapSize, misses);
// make some hits
populateNearCache(map, mapSize);
long hits = stats.getHits();
misses = stats.getMisses();
long hitsAndMisses = hits + misses;
assertTrue(format("Near Cache hits should be > %d but were %d", 400, hits), hits > 400);
assertTrue(format("Near Cache misses should be > %d but were %d", 400, misses), misses > 400);
assertEquals(format("Near Cache hits + misses should be %s but were %d", mapSize * 2, hitsAndMisses), mapSize * 2, hitsAndMisses);
}
use of com.hazelcast.nearcache.NearCacheStats in project hazelcast by hazelcast.
the class NearCacheTest method multiple_get_on_non_existing_key_generates_one_miss.
@Test
public void multiple_get_on_non_existing_key_generates_one_miss() {
String mapName = "test";
Config config = getConfig();
NearCacheConfig nearCacheConfig = newNearCacheConfig();
nearCacheConfig.setCacheLocalEntries(true);
config.getMapConfig(mapName).setNearCacheConfig(nearCacheConfig);
HazelcastInstance node = createHazelcastInstance(config);
IMap map = node.getMap(mapName);
map.get(1);
map.get(1);
map.get(1);
NearCacheStats nearCacheStats = map.getLocalMapStats().getNearCacheStats();
assertEquals(1, nearCacheStats.getMisses());
}
use of com.hazelcast.nearcache.NearCacheStats in project hazelcast by hazelcast.
the class AbstractNearCachePreloaderTest method assertNearCachePreloadDoneEventually.
private static void assertNearCachePreloadDoneEventually(final NearCacheTestContext clientContext, long timeoutSeconds) {
assertTrueEventually(() -> {
NearCache nearCache = clientContext.nearCache;
NearCacheStats stats = nearCache.getNearCacheStats();
int size = nearCache.size();
assertTrue(format("Preloading has not finished yet. [size: %d, %s]", size, stats), nearCache.isPreloadDone());
});
}
use of com.hazelcast.nearcache.NearCacheStats in project hazelcast by hazelcast.
the class NearCacheRecordStoreTestSupport method statsCalculated.
void statsCalculated(InMemoryFormat inMemoryFormat) {
long creationStartTime = System.currentTimeMillis();
NearCacheConfig nearCacheConfig = createNearCacheConfig(DEFAULT_NEAR_CACHE_NAME, inMemoryFormat);
NearCacheRecordStore<Integer, String> nearCacheRecordStore = createNearCacheRecordStore(nearCacheConfig, inMemoryFormat);
long creationEndTime = System.currentTimeMillis();
int expectedEntryCount = 0;
int expectedHits = 0;
int expectedMisses = 0;
for (int i = 0; i < DEFAULT_RECORD_COUNT; i++) {
nearCacheRecordStore.put(i, null, "Record-" + i, null);
expectedEntryCount++;
}
for (int i = 0; i < DEFAULT_RECORD_COUNT; i++) {
if (nearCacheRecordStore.get(i * 3) != null) {
expectedHits++;
} else {
expectedMisses++;
}
}
NearCacheStats nearCacheStats = nearCacheRecordStore.getNearCacheStats();
long memoryCostWhenFull = nearCacheStats.getOwnedEntryMemoryCost();
// Note that System.currentTimeMillis() is not monotonically increasing.
// Below assertions can fail anytime but for testing purposes we can use `assertTrueEventually`.
long nearCacheStatsCreationTime = nearCacheStats.getCreationTime();
assertTrue("nearCacheStatsCreationTime=" + nearCacheStatsCreationTime + ", and creationStartTime=" + creationStartTime, nearCacheStatsCreationTime >= creationStartTime);
assertTrue("nearCacheStatsCreationTime=" + nearCacheStatsCreationTime + ", and creationEndTime=" + creationEndTime, nearCacheStatsCreationTime <= creationEndTime);
assertEquals(expectedHits, nearCacheStats.getHits());
assertEquals(expectedMisses, nearCacheStats.getMisses());
assertEquals(expectedEntryCount, nearCacheStats.getOwnedEntryCount());
switch(inMemoryFormat) {
case NATIVE:
case BINARY:
assertTrue(memoryCostWhenFull > 0);
break;
case OBJECT:
assertEquals(0, memoryCostWhenFull);
break;
default:
}
int sizeBefore = nearCacheRecordStore.size();
for (int i = 0; i < DEFAULT_RECORD_COUNT; i++) {
nearCacheRecordStore.invalidate(i * 3);
}
int sizeAfter = nearCacheRecordStore.size();
int invalidatedSize = sizeBefore - sizeAfter;
expectedEntryCount -= invalidatedSize;
assertEquals(expectedEntryCount, nearCacheStats.getOwnedEntryCount());
switch(inMemoryFormat) {
case NATIVE:
case BINARY:
assertTrue(nearCacheStats.getOwnedEntryMemoryCost() > 0);
assertTrue(nearCacheStats.getOwnedEntryMemoryCost() < memoryCostWhenFull);
break;
case OBJECT:
assertEquals(0, nearCacheStats.getOwnedEntryMemoryCost());
break;
default:
}
nearCacheRecordStore.clear();
assertEquals(0, nearCacheStats.getOwnedEntryMemoryCost());
}
use of com.hazelcast.nearcache.NearCacheStats in project hazelcast by hazelcast.
the class NearCacheTestUtils method assertNearCacheStats.
/**
* Asserts the {@link NearCacheStats} for expected values.
*
* @param context the {@link NearCacheTestContext} to retrieve the stats from
* @param expectedOwnedEntryCount the expected owned entry count
* @param expectedHits the expected Near Cache hits
* @param expectedMisses the expected Near Cache misses
* @param expectedEvictions the expected Near Cache evictions
* @param expectedExpirations the expected Near Cache expirations
*/
public static void assertNearCacheStats(NearCacheTestContext<?, ?, ?, ?> context, long expectedOwnedEntryCount, long expectedHits, long expectedMisses, long expectedEvictions, long expectedExpirations) {
NearCacheStats stats = new NearCacheStatsImpl(context.stats);
assertEqualsFormat("Near Cache entry count should be %d, but was %d (%s)", expectedOwnedEntryCount, stats.getOwnedEntryCount(), stats);
assertEqualsFormat("Near Cache hits should be %d, but were %d (%s)", expectedHits, stats.getHits(), stats);
assertEqualsFormat("Near Cache misses should be %d, but were %d (%s)", expectedMisses, stats.getMisses(), stats);
assertEqualsFormat("Near Cache evictions should be %d, but were %d (%s)", expectedEvictions, stats.getEvictions(), stats);
assertEqualsFormat("Near Cache expirations should be %d, but were %d (%s)", expectedExpirations, stats.getExpirations(), stats);
}
Aggregations