From dc1dedb073f390a5cb98bcd1b57358900d69867c Mon Sep 17 00:00:00 2001 From: zhangduo Date: Wed, 12 Sep 2018 22:16:27 +0800 Subject: [PATCH] HBASE-21188 Print heap and gc informations in our junit ResourceChecker --- .../hbase/ResourceCheckerJUnitListener.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/ResourceCheckerJUnitListener.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/ResourceCheckerJUnitListener.java index 225d94f42b4..d8df1379252 100644 --- a/hbase-common/src/test/java/org/apache/hadoop/hbase/ResourceCheckerJUnitListener.java +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/ResourceCheckerJUnitListener.java @@ -18,12 +18,15 @@ package org.apache.hadoop.hbase; +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryUsage; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; import org.apache.hadoop.hbase.ResourceChecker.Phase; import org.apache.hadoop.hbase.util.JVM; import org.junit.runner.notification.RunListener; @@ -139,6 +142,41 @@ public class ResourceCheckerJUnitListener extends RunListener { } } + static class MaxHeapMemoryMBResourceAnalyzer extends ResourceChecker.ResourceAnalyzer { + + @Override + public int getVal(Phase phase) { + MemoryUsage usage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); + return (int) (usage.getMax() / (1024 * 1024)); + } + } + + static class UsedHeapMemoryMBResourceAnalyzer extends ResourceChecker.ResourceAnalyzer { + + @Override + public int getVal(Phase phase) { + MemoryUsage usage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); + return (int) (usage.getUsed() / (1024 * 1024)); + } + } + + static class GCCountResourceAnalyzer extends ResourceChecker.ResourceAnalyzer { + + @Override + public int getVal(Phase phase) { + return Math.toIntExact(ManagementFactory.getGarbageCollectorMXBeans().stream() + .mapToLong(b -> b.getCollectionCount()).sum()); + } + } + + static class GCTimeSecondResourceAnalyzer extends ResourceChecker.ResourceAnalyzer { + + @Override + public int getVal(Phase phase) { + return Math.toIntExact(TimeUnit.MILLISECONDS.toSeconds(ManagementFactory + .getGarbageCollectorMXBeans().stream().mapToLong(b -> b.getCollectionTime()).sum())); + } + } /** * To be implemented by sub classes if they want to add specific ResourceAnalyzer. @@ -155,6 +193,10 @@ public class ResourceCheckerJUnitListener extends RunListener { rc.addResourceAnalyzer(new SystemLoadAverageResourceAnalyzer()); rc.addResourceAnalyzer(new ProcessCountResourceAnalyzer()); rc.addResourceAnalyzer(new AvailableMemoryMBResourceAnalyzer()); + rc.addResourceAnalyzer(new MaxHeapMemoryMBResourceAnalyzer()); + rc.addResourceAnalyzer(new UsedHeapMemoryMBResourceAnalyzer()); + rc.addResourceAnalyzer(new GCCountResourceAnalyzer()); + rc.addResourceAnalyzer(new GCTimeSecondResourceAnalyzer()); addResourceAnalyzer(rc);