From 51770c299644199ac24b406f222488e3548e7b72 Mon Sep 17 00:00:00 2001 From: Aleksey Tomin Date: Thu, 27 Oct 2016 12:13:41 +0400 Subject: [PATCH] Add calculated columns in jconsole plugins: - total profit, seconds - assessment of memory consumption --- .../mxcache/jconsoleplugin/Attribute.java | 33 ++++++++++++++----- .../jconsoleplugin/MxCacheJConsolePlugin.java | 5 +-- .../maxifier/mxcache/jconsoleplugin/Time.java | 4 +++ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/mxcache-jmx/src/main/java/com/maxifier/mxcache/jconsoleplugin/Attribute.java b/mxcache-jmx/src/main/java/com/maxifier/mxcache/jconsoleplugin/Attribute.java index 4067a109..0a93e55b 100644 --- a/mxcache-jmx/src/main/java/com/maxifier/mxcache/jconsoleplugin/Attribute.java +++ b/mxcache-jmx/src/main/java/com/maxifier/mxcache/jconsoleplugin/Attribute.java @@ -3,6 +3,7 @@ */ package com.maxifier.mxcache.jconsoleplugin; +import javax.management.openmbean.CompositeData; import java.util.Comparator; import java.util.regex.Pattern; @@ -81,9 +82,13 @@ public Object transform(Object o) { public Object transform(Object o) { return new Rate((Double) o); } - }; + }, + MEMORY("Total memory, parrots", "totalMemory", false, false), + PROFIT("Total profit, seconds", "totalProfit", false, false); private static final Pattern SHORTCUT_PATTERN = Pattern.compile("([\\w_$][\\w\\d_$]*\\.)*([\\w_$][\\w\\d_$]*)([^\\.])"); + private static final int NS_IN_SECOND = 1000000000; + private static final int MAX_GET_FROM_CACHE_TIME_NS = 2000; public static String shortcutClassNames(Object s) { return s == null ? "" : SHORTCUT_PATTERN.matcher(s.toString()).replaceAll("$2$3"); @@ -124,19 +129,29 @@ public String getName() { return name; } - public String getKey() { - return key; - } - public boolean isSearchable() { return searchable; } - public boolean isShortcutable() { - return shortcutable; - } - public Comparator getComparator() { return comparator; } + + public Object getValueFromCache(CompositeData cache) { + if (this == PROFIT) { + Integer totalHits = (Integer) TOTAL_HITS.getValueFromCache(cache); + double perHintProfitNs = ((Time) AVERAGE_CALCULATION.getValueFromCache(cache)).getValue() - MAX_GET_FROM_CACHE_TIME_NS; + return Math.round(totalHits * perHintProfitNs / NS_IN_SECOND); + } else if (this == MEMORY) { + Integer instanceCount = (Integer) INSTANCES.getValueFromCache(cache); + Integer elementCount = (Integer) ELEMENTS.getValueFromCache(cache); + return instanceCount * 2 + elementCount; // in parrots! + } else { + Object value = cache.get(key); + value = value == null ? "" : transform(value); + // todo add posibility to switch shortcutting off + return shortcutable ? shortcutClassNames(value) : value; + } + } + } diff --git a/mxcache-jmx/src/main/java/com/maxifier/mxcache/jconsoleplugin/MxCacheJConsolePlugin.java b/mxcache-jmx/src/main/java/com/maxifier/mxcache/jconsoleplugin/MxCacheJConsolePlugin.java index 678c126a..3f18ad1e 100644 --- a/mxcache-jmx/src/main/java/com/maxifier/mxcache/jconsoleplugin/MxCacheJConsolePlugin.java +++ b/mxcache-jmx/src/main/java/com/maxifier/mxcache/jconsoleplugin/MxCacheJConsolePlugin.java @@ -369,10 +369,7 @@ protected Object doInBackground() throws Exception { private void fillRow(CompositeData cache, Object[] row) { for (com.maxifier.mxcache.jconsoleplugin.Attribute attr : com.maxifier.mxcache.jconsoleplugin.Attribute.values()) { try { - Object v = cache.get(attr.getKey()); - Object s = v == null ? "" : attr.transform(v); - // todo add posibility to switch shortcutting off - row[attr.ordinal()] = attr.isShortcutable() ? com.maxifier.mxcache.jconsoleplugin.Attribute.shortcutClassNames(s) : s; + row[attr.ordinal()] = attr.getValueFromCache(cache); } catch (InvalidKeyException e) { // some attributes were added lately, so they may be missing row[attr.ordinal()] = ""; diff --git a/mxcache-jmx/src/main/java/com/maxifier/mxcache/jconsoleplugin/Time.java b/mxcache-jmx/src/main/java/com/maxifier/mxcache/jconsoleplugin/Time.java index a90fa85f..6ca62a7c 100644 --- a/mxcache-jmx/src/main/java/com/maxifier/mxcache/jconsoleplugin/Time.java +++ b/mxcache-jmx/src/main/java/com/maxifier/mxcache/jconsoleplugin/Time.java @@ -48,4 +48,8 @@ public String toString() { } return (int)(value/1000000) + "ms"; } + + public double getValue() { + return value; + } }