GEODE-2883: Fix GFSH gc heap size output
Project: http://git-wip-us.apache.org/repos/asf/geode/repo Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/68935cae Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/68935cae Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/68935cae Branch: refs/heads/feature/GEODE-2900 Commit: 68935caea1d158375d6bf94c73c8f685eda01211 Parents: 61f676b Author: Jared Stewart <[email protected]> Authored: Mon May 8 10:28:21 2017 -0700 Committer: Jared Stewart <[email protected]> Committed: Thu May 11 09:50:51 2017 -0700 ---------------------------------------------------------------------- .../functions/GarbageCollectionFunction.java | 35 ++---- .../internal/cli/util/BytesToString.java | 50 ++++++++ .../internal/cli/util/BytesToStringTest.java | 115 +++++++++++++++++++ 3 files changed, 176 insertions(+), 24 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geode/blob/68935cae/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/GarbageCollectionFunction.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/GarbageCollectionFunction.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/GarbageCollectionFunction.java index 354d353..46588eb 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/GarbageCollectionFunction.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/GarbageCollectionFunction.java @@ -23,6 +23,7 @@ import org.apache.geode.cache.execute.FunctionContext; import org.apache.geode.distributed.DistributedMember; import org.apache.geode.internal.InternalEntity; import org.apache.geode.management.internal.cli.CliUtil; +import org.apache.geode.management.internal.cli.util.BytesToString; /** * @@ -34,37 +35,34 @@ import org.apache.geode.management.internal.cli.CliUtil; public class GarbageCollectionFunction implements Function, InternalEntity { public static final String ID = GarbageCollectionFunction.class.getName(); - private static final long serialVersionUID = 1L; @Override public void execute(FunctionContext context) { - StringBuilder str1 = new StringBuilder(); + BytesToString bytesToString = new BytesToString(); + Map<String, String> resultMap = null; try { Cache cache = CacheFactory.getAnyInstance(); DistributedMember member = cache.getDistributedSystem().getDistributedMember(); - long freeMemoeryBeforeGC = Runtime.getRuntime().freeMemory(); + long freeMemoryBeforeGC = Runtime.getRuntime().freeMemory(); long totalMemoryBeforeGC = Runtime.getRuntime().totalMemory(); long timeBeforeGC = System.currentTimeMillis(); Runtime.getRuntime().gc(); - long freeMemoeryAfterGC = Runtime.getRuntime().freeMemory(); + long freeMemoryAfterGC = Runtime.getRuntime().freeMemory(); long totalMemoryAfterGC = Runtime.getRuntime().totalMemory(); long timeAfterGC = System.currentTimeMillis(); - long megaBytes = 131072; - resultMap = new HashMap<String, String>(); + resultMap = new HashMap<>(); resultMap.put("MemberId", member.getId()); - resultMap.put("HeapSizeBeforeGC", - String.valueOf((totalMemoryBeforeGC - freeMemoeryBeforeGC) / megaBytes)); - resultMap.put("HeapSizeAfterGC", - String.valueOf((totalMemoryAfterGC - freeMemoeryAfterGC) / megaBytes)); + resultMap.put("HeapSizeBeforeGC", bytesToString.of(totalMemoryBeforeGC - freeMemoryBeforeGC)); + resultMap.put("HeapSizeAfterGC", bytesToString.of(totalMemoryAfterGC - freeMemoryAfterGC)); resultMap.put("TimeSpentInGC", String.valueOf(timeAfterGC - timeBeforeGC)); } catch (Exception ex) { - str1.append( - "Exception in GC:" + ex.getMessage() + CliUtil.stackTraceAsString((Throwable) ex)); - context.getResultSender().lastResult(str1.toString()); + String message = "Exception in GC:" + ex.getMessage() + CliUtil.stackTraceAsString(ex); + + context.getResultSender().lastResult(message); } context.getResultSender().lastResult(resultMap); } @@ -75,17 +73,6 @@ public class GarbageCollectionFunction implements Function, InternalEntity { } @Override - public boolean hasResult() { - return true; - } - - @Override - public boolean optimizeForWrite() { - // no need of optimization since read-only. - return false; - } - - @Override public boolean isHA() { return false; } http://git-wip-us.apache.org/repos/asf/geode/blob/68935cae/geode-core/src/main/java/org/apache/geode/management/internal/cli/util/BytesToString.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/util/BytesToString.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/util/BytesToString.java new file mode 100644 index 0000000..d0405d4 --- /dev/null +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/util/BytesToString.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.management.internal.cli.util; + +import java.text.DecimalFormat; +import java.text.NumberFormat; + +public class BytesToString { + private static final double BYTES_PER_KB = 1024; + private static final double BYTES_PER_MB = 1024 * BYTES_PER_KB; + private static final double BYTES_PER_GB = 1024 * BYTES_PER_MB; + private static final double BYTES_PER_TB = 1024 * BYTES_PER_GB; + + /** + * Returns a String that concisely displays the given number of bytes, e.g. "1 KB" or "2.49 MB". + */ + public String of(long sizeInBytes) { + final NumberFormat numberFormat = new DecimalFormat(); + numberFormat.setMaximumFractionDigits(2); + + try { + if (sizeInBytes < BYTES_PER_KB) { + return numberFormat.format(sizeInBytes) + " Byte(s)"; + } else if (sizeInBytes < BYTES_PER_MB) { + return numberFormat.format(sizeInBytes / BYTES_PER_KB) + " KB"; + } else if (sizeInBytes < BYTES_PER_GB) { + return numberFormat.format(sizeInBytes / BYTES_PER_MB) + " MB"; + } else if (sizeInBytes < BYTES_PER_TB) { + return numberFormat.format(sizeInBytes / BYTES_PER_GB) + " GB"; + } else { + return numberFormat.format(sizeInBytes / BYTES_PER_TB) + " TB"; + } + } catch (Exception e) { + return sizeInBytes + " Byte(s)"; + } + } + +} http://git-wip-us.apache.org/repos/asf/geode/blob/68935cae/geode-core/src/test/java/org/apache/geode/management/internal/cli/util/BytesToStringTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/util/BytesToStringTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/util/BytesToStringTest.java new file mode 100644 index 0000000..addc263 --- /dev/null +++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/util/BytesToStringTest.java @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.management.internal.cli.util; + +import static org.assertj.core.api.Assertions.*; + +import org.apache.geode.test.junit.categories.UnitTest; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category(UnitTest.class) +public class BytesToStringTest { + private static long KB = 1024; + private static long MB = 1024 * KB; + private static long GB = 1024 * MB; + private static long TB = 1024 * GB; + + private BytesToString bytesToString; + + @Before + public void before() { + bytesToString = new BytesToString(); + } + + @Test + public void testFiveHundredBytes() { + String value = bytesToString.of(500); + assertThat(value).isEqualTo("500 Byte(s)"); + } + + @Test + public void testOneKb() { + String value = bytesToString.of(KB); + assertThat(value).isEqualTo("1 KB"); + } + + @Test + public void testThreeKb() { + String value = bytesToString.of(3 * KB); + assertThat(value).isEqualTo("3 KB"); + } + + @Test + public void testFractionalKB() { + String value = bytesToString.of(KB + 500); + assertThat(value).isEqualTo("1.49 KB"); + } + + @Test + public void testOneMB() { + String value = bytesToString.of(MB); + assertThat(value).isEqualTo("1 MB"); + } + + @Test + public void testThreeMB() { + String value = bytesToString.of(3 * MB); + assertThat(value).isEqualTo("3 MB"); + } + + @Test + public void testFractionalMB() { + String value = bytesToString.of(MB + 500 * KB); + assertThat(value).isEqualTo("1.49 MB"); + } + + @Test + public void testOneGB() { + String value = bytesToString.of(GB); + assertThat(value).isEqualTo("1 GB"); + } + + @Test + public void testThreeGB() { + String value = bytesToString.of(3 * GB); + assertThat(value).isEqualTo("3 GB"); + } + + @Test + public void testFractionalGB() { + String value = bytesToString.of(GB + 500 * MB); + assertThat(value).isEqualTo("1.49 GB"); + } + + @Test + public void testOneTB() { + String value = bytesToString.of(TB); + assertThat(value).isEqualTo("1 TB"); + } + + @Test + public void testThreeTB() { + String value = bytesToString.of(3 * GB); + assertThat(value).isEqualTo("3 GB"); + } + + @Test + public void testFractionalTB() { + String value = bytesToString.of(TB + 500 * GB); + assertThat(value).isEqualTo("1.49 TB"); + } +}
