Skip to content

Instantly share code, notes, and snippets.

@enesakar
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save enesakar/9123542 to your computer and use it in GitHub Desktop.

Select an option

Save enesakar/9123542 to your computer and use it in GitHub Desktop.
stats example
public static void main(String[] args) throws InterruptedException {
Config cfg = new XmlConfigBuilder().build();
cfg.setProperty("hazelcast.local.localAddress", "127.0.0.1");
cfg.setProperty(GroupProperties.PROP_HEALTH_MONITORING_LEVEL, HealthMonitorLevel.NOISY.toString());
// memory stats will be logged in every 10 seconds
cfg.setProperty(GroupProperties.PROP_HEALTH_MONITORING_DELAY_SECONDS, "10");
String cacheName = "cache";
MemorySize memorySize = new MemorySize(100, MemoryUnit.MEGABYTES);
OffHeapMemoryConfig memoryConfig = new OffHeapMemoryConfig()
.setSize(memorySize).setEnabled(true);
cfg.setOffHeapMemoryConfig(memoryConfig);
SerializationConfig serializationConfig = cfg.getSerializationConfig();
serializationConfig.setAllowUnsafe(true);
Hazelcast.newHazelcastInstance(cfg);
HazelcastInstance client = HazelcastClient.newHazelcastClient(new ClientConfig());
CacheManager cacheManager = new HazelcastCachingProvider(client).getCacheManager();
final ICache<String, String> cache = (ICache) cacheManager.getCache(cacheName);
Random random = new Random(System.currentTimeMillis());
for (int i = 0; i < 1000; i++) {
cache.put("key" + i, "value" + i);
cache.get("key" + random.nextInt(100));
CacheStats stats = cache.getStats();
System.out.println("CACHE STATS:");
System.out.println("Size:"+ stats.getSize()+ " - Hits:"+stats.getHits() + " - Misses:" + stats.getMisses());
Thread.sleep(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment