Skip to content

Instantly share code, notes, and snippets.

@aerostitch
Last active December 5, 2015 23:01
Show Gist options
  • Save aerostitch/fd09567dc13e6bad1014 to your computer and use it in GitHub Desktop.
Save aerostitch/fd09567dc13e6bad1014 to your computer and use it in GitHub Desktop.
This script does a basic benchmark and retrieves some queue size and map size in Hazelcast locally.
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.monitor.LocalMapStats;
import com.hazelcast.monitor.NearCacheStats;
import com.hazelcast.core.IQueue;
import com.hazelcast.monitor.LocalQueueStats;
import com.hazelcast.core.IExecutorService;
import com.hazelcast.monitor.LocalExecutorStats;
import java.util.HashSet;
import java.util.Collection;
import com.hazelcast.core.Client;
import com.hazelcast.core.ClientService;
/*
* This script tests the performance of getting some defined queue size and map sizes locally for a Hazelcast cluster.
*
* To compile:
* LIB_ROOT=/opt/hazelcast/target/dependency
* javac -classpath ${LIB_ROOT}/hazelcast-client-3.2.5.jar:${LIB_ROOT}/hazelcast-3.2.5.jar -Xlint:deprecation HazelcastMetrics.java
* java -cp ./:${LIB_ROOT}/hazelcast-client-3.2.5.jar:${LIB_ROOT}/hazelcast-3.2.5.jar -Dhazelcast.logging.type=none HazelcastMetrics
*
* To use it to send metrics to ganglia via gmetric:
* java -cp ./:${LIB_ROOT}/hazelcast-client-3.2.5.jar:${LIB_ROOT}/hazelcast-3.2.5.jar -Dhazelcast.logging.type=none HazelcastMetrics | while IFS=':' read -r -a line ; do /opt/ganglia/bin/gmetric --name ${line[0]} --value ${line[1]} --type ${line[2]} --units ${line[3]}; done
*/
public class HazelcastMetrics {
private static HazelcastInstance client;
private final static String clientIP = "127.0.0.1";
private final static int clientPort = 5701;
private final static String metricNamePrefix = "hazelcast_client_";
// Name of the queues to test
private final static String[] queueList = {"my", "beautiful", "queue"};
// Name of the maps to test
private final static String[] mapList = {"what_a", "nice_map"};
// Name of the executors to test
private final static String[] execList = {"default"};
private static void showQueueStats(String qName){
final IQueue queue = client.getQueue(qName);
System.out.println(metricNamePrefix +"queue_"+ qName +"_size:"+ queue.size() +":int16:count");
/*
Getting this error at execution time with the hazelcast 3.2.5 lib
Exception in thread "main" java.lang.UnsupportedOperationException: Locality is ambiguous for client!!!
at com.hazelcast.client.proxy.ClientQueueProxy.getLocalQueueStats(ClientQueueProxy.java:94)
at HazelcastMetrics.showQueueStats(HazelcastMetrics.java:34)
at HazelcastMetrics.main(HazelcastMetrics.java:105)
*/
// final LocalQueueStats queueStats = queue.getLocalQueueStats();
// System.out.println(metricNamePrefix +"queue_"+ qName +"_items:"+ queueStats.getOwnedItemCount() +":int16:count");
// System.out.println(metricNamePrefix +"queue_"+ qName +"_backup_items:"+ queueStats.getBackupItemCount() +":int16:count");
// System.out.println(metricNamePrefix +"queue_"+ qName +"_min_age:"+ queueStats.getMinAge() +":int16:ms");
// System.out.println(metricNamePrefix +"queue_"+ qName +"_max_age:"+ queueStats.getMaxAge() +":int16:ms");
// System.out.println(metricNamePrefix +"queue_"+ qName +"_avg_age:"+ queueStats.getAvgAge() +":int16:ms");
// System.out.println(metricNamePrefix +"queue_"+ qName +"_offer_operations:"+ queueStats.getOfferOperationCount() +":int16:count");
// System.out.println(metricNamePrefix +"queue_"+ qName +"_rejected_offer_operations:"+ queueStats.getRejectedOfferOperationCount() +":int16:count");
// System.out.println(metricNamePrefix +"queue_"+ qName +"_poll_offer_operations:"+ queueStats.getPollOperationCount() +":int16:count");
// System.out.println(metricNamePrefix +"queue_"+ qName +"_empty_poll_offer_operations:"+ queueStats.getEmptyPollOperationCount() +":int16:count");
// System.out.println(metricNamePrefix +"queue_"+ qName +"_other_offer_operations:"+ queueStats.getOtherOperationsCount() +":int16:count");
// System.out.println(metricNamePrefix +"queue_"+ qName +"_event_operations:"+ queueStats.getEventOperationCount() +":int16:count");
}
private static void showMapStats(String mapName){
final IMap map = client.getMap(mapName);
System.out.println(metricNamePrefix +"map_"+ mapName +"_size:"+ map.size() +":int16:count");
// Get the stats of the map on the local Member of the cluster
final LocalMapStats mapStatistics = map.getLocalMapStats();
System.out.println( metricNamePrefix +"map_"+ mapName +"_owned_entries:" + mapStatistics.getOwnedEntryCount() +":int16:count");
System.out.println( metricNamePrefix +"map_"+ mapName +"_owned_backup_entries:" + mapStatistics.getBackupEntryCount() +":int16:count");
System.out.println( metricNamePrefix +"map_"+ mapName +"_backup_entries:" + mapStatistics.getBackupCount() +":int16:count");
System.out.println( metricNamePrefix +"map_"+ mapName +"_owned_entries_memory:" + mapStatistics.getOwnedEntryMemoryCost() +":int16:bytes");
System.out.println( metricNamePrefix +"map_"+ mapName +"_owned_backup_entries_memory:" + mapStatistics.getBackupEntryMemoryCost() +":int16:bytes");
System.out.println( metricNamePrefix +"map_"+ mapName +"_creation_time:" + mapStatistics.getCreationTime() +":int16:ms");
System.out.println( metricNamePrefix +"map_"+ mapName +"_owned_last_access_time:" + mapStatistics.getLastAccessTime() +":int16:ms");
System.out.println( metricNamePrefix +"map_"+ mapName +"_owned_entries_reads:" + mapStatistics.getHits() +":int16:count");
System.out.println( metricNamePrefix +"map_"+ mapName +"_owned_entries_locked_keys:" + mapStatistics.getLockedEntryCount() +":int16:count");
System.out.println( metricNamePrefix +"map_"+ mapName +"_owned_entries_dirty_keys:" + mapStatistics.getDirtyEntryCount() +":int16:count");
System.out.println( metricNamePrefix +"map_"+ mapName +"_put_operations:" + mapStatistics.getPutOperationCount() +":int16:count");
System.out.println( metricNamePrefix +"map_"+ mapName +"_get_operations:" + mapStatistics.getGetOperationCount() +":int16:count");
System.out.println( metricNamePrefix +"map_"+ mapName +"_remove_operations:" + mapStatistics.getRemoveOperationCount() +":int16:count");
System.out.println( metricNamePrefix +"map_"+ mapName +"_avg_put_latency:" + mapStatistics.getTotalPutLatency() +":int16:ms");
System.out.println( metricNamePrefix +"map_"+ mapName +"_avg_get_latency:" + mapStatistics.getTotalGetLatency() +":int16:ms");
System.out.println( metricNamePrefix +"map_"+ mapName +"_avg_remove_latency:" + mapStatistics.getTotalRemoveLatency() +":int16:ms");
System.out.println( metricNamePrefix +"map_"+ mapName +"_max_put_latency:" + mapStatistics.getMaxPutLatency() +":int16:ms");
System.out.println( metricNamePrefix +"map_"+ mapName +"_max_get_latency:" + mapStatistics.getMaxGetLatency() +":int16:ms");
System.out.println( metricNamePrefix +"map_"+ mapName +"_max_remove_latency:" + mapStatistics.getMaxRemoveLatency() +":int16:ms");
System.out.println( metricNamePrefix +"map_"+ mapName +"_events_received:" + mapStatistics.getEventOperationCount() +":int16:count");
System.out.println( metricNamePrefix +"map_"+ mapName +"_other_operations:" + mapStatistics.getOtherOperationCount() +":int16:count");
System.out.println( metricNamePrefix +"map_"+ mapName +"_total_operations:" + mapStatistics.total() +":int16:count");
System.out.println( metricNamePrefix +"map_"+ mapName +"_heap_cost:" + mapStatistics.getHeapCost() +":int16:bytes");
// statistics related to the Near Cache
/* Not available in hazelcast 3.2.5 */
// final NearCacheStats mapCache = map.getNearCacheStats();
// System.out.println( metricNamePrefix +"map_"+ mapName +"_near_cache_creation_time:" + mapCache.getCreationTime() +":int16:ms");
// System.out.println( metricNamePrefix +"map_"+ mapName +"_near_cache_owned_entried:" + mapCache.getOwnedEntryCount() +":int16:count");
// System.out.println( metricNamePrefix +"map_"+ mapName +"_near_cache_owned_entried_memory:" + mapCache.getOwnedEntryMemoryCost() +":int16:bytes");
// System.out.println( metricNamePrefix +"map_"+ mapName +"_near_cache_reads:" + mapCache.getHits() +":int16:count");
// System.out.println( metricNamePrefix +"map_"+ mapName +"_near_cache_misses:" + mapCache.getMisses() +":int16:count");
// System.out.println( metricNamePrefix +"map_"+ mapName +"_near_cache_ratio:" + mapCache.getRatio() +":int16:count");
}
private static void showExecutorStats(String eName){
final IExecutorService execService = client.getExecutorService( eName );
/*
Getting this error at execution time on Hazelcast 3.2.5:
Exception in thread "main" java.lang.UnsupportedOperationException: Locality is ambiguous for client!!!
at com.hazelcast.client.proxy.ClientExecutorServiceProxy.getLocalExecutorStats(ClientExecutorServiceProxy.java:307)
at HazelcastMetrics.showExecutorStats(HazelcastMetrics.java:96)
at HazelcastMetrics.main(HazelcastMetrics.java:133)
*/
// LocalExecutorStats eStatistics = execService.getLocalExecutorStats();
// System.out.println( metricNamePrefix +"executor_"+ eName +"_pending_tasks:" + eStatistics.getPendingTaskCount() +":int16:count");
// System.out.println( metricNamePrefix +"executor_"+ eName +"_started_tasks:" + eStatistics.getStartedTaskCount() +":int16:count");
// System.out.println( metricNamePrefix +"executor_"+ eName +"_completed_tasks:" + eStatistics.getCompletedTaskCount() +":int16:count");
// System.out.println( metricNamePrefix +"executor_"+ eName +"_cancelled_tasks:" + eStatistics.getCancelledTaskCount() +":int16:count");
// System.out.println( metricNamePrefix +"executor_"+ eName +"_total_start_latency:" + eStatistics.getTotalStartLatency() +":int16:ms");
// System.out.println( metricNamePrefix +"executor_"+ eName +"_total_execution_latency:" + eStatistics.getTotalExecutionLatency() +":int16:ms");
}
private static void showClientStats(){
/*
Getting this error at execution time with Hazelcast 3.2.5 lib:
Exception in thread "main" java.lang.UnsupportedOperationException
at com.hazelcast.client.HazelcastClient.getClientService(HazelcastClient.java:375)
at com.hazelcast.client.HazelcastClientProxy.getClientService(HazelcastClientProxy.java:200)
at HazelcastMetrics.showClientStats(HazelcastMetrics.java:118)
at HazelcastMetrics.main(HazelcastMetrics.java:163)
*/
/*
final ClientService clientService = client.getClientService();
final Collection<Client> connectedClients = clientService.getConnectedClients();
final HashSet<String> uuids = new HashSet<String>();
for (Client connectedClient : connectedClients) {
uuids.add(connectedClient.getUuid());
}
System.out.println(uuids);
*/
}
public static void main(String[] args) {
// benchmarking
long refTime = System.nanoTime();
ClientConfig clientConfig = new ClientConfig();
clientConfig.getNetworkConfig().addAddress(clientIP +":"+ clientPort);
client = HazelcastClient.newHazelcastClient(clientConfig);
System.out.println(metricNamePrefix +"connect:" + (System.nanoTime() - refTime) / 1000000 + ":int16:ms");
//gmetric --name temperature --value `cputemp` --type int16 --units Celcius
refTime = System.nanoTime();
for(String q: queueList){
showQueueStats(q);
System.out.println(metricNamePrefix +"time_to_get_queue_stats_"+ q +":" + (System.nanoTime() - refTime) / 1000000 + ":int16:ms");
refTime = System.nanoTime();
}
for(String m: mapList){
showMapStats(m);
System.out.println(metricNamePrefix +"time_to_get_map_stats_"+ m +":" + (System.nanoTime() - refTime) / 1000000 + ":int16:ms");
refTime = System.nanoTime();
}
for(String e: execList){
showExecutorStats(e);
System.out.println(metricNamePrefix +"time_to_get_executor_stats_"+ e +":" + (System.nanoTime() - refTime) / 1000000 + ":int16:ms");
refTime = System.nanoTime();
}
showClientStats();
// Yes, it's scarry to write that isn't it? :)
HazelcastClient.shutdownAll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment