001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.util; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertNotNull; 022import static org.junit.jupiter.api.Assertions.assertNull; 023 024import java.lang.management.GarbageCollectorMXBean; 025import java.lang.management.ManagementFactory; 026import java.util.Hashtable; 027import java.util.List; 028import java.util.Map; 029import javax.management.MalformedObjectNameException; 030import javax.management.ObjectName; 031import javax.management.openmbean.CompositeData; 032import org.apache.hadoop.hbase.testclassification.MiscTests; 033import org.apache.hadoop.hbase.testclassification.SmallTests; 034import org.junit.jupiter.api.Tag; 035import org.junit.jupiter.api.Test; 036import org.slf4j.Logger; 037import org.slf4j.LoggerFactory; 038 039@Tag(MiscTests.TAG) 040@Tag(SmallTests.TAG) 041public class TestJSONMetricUtil { 042 043 private static final Logger LOG = LoggerFactory.getLogger(TestJSONMetricUtil.class); 044 045 @Test 046 public void testBuildHashtable() { 047 String[] keys = { "type", "name" }; 048 String[] emptyKey = {}; 049 String[] values = { "MemoryPool", "Par Eden Space" }; 050 String[] values2 = { "MemoryPool", "Par Eden Space", "Test" }; 051 String[] emptyValue = {}; 052 Map<String, String> properties = JSONMetricUtil.buldKeyValueTable(keys, values); 053 assertEquals(values[0], properties.get("type")); 054 assertEquals(values[1], properties.get("name")); 055 056 assertNull(JSONMetricUtil.buldKeyValueTable(keys, values2)); 057 assertNull(JSONMetricUtil.buldKeyValueTable(keys, emptyValue)); 058 assertNull(JSONMetricUtil.buldKeyValueTable(emptyKey, values2)); 059 assertNull(JSONMetricUtil.buldKeyValueTable(emptyKey, emptyValue)); 060 } 061 062 @Test 063 public void testBuildObjectName() throws MalformedObjectNameException { 064 String[] keys = { "type", "name" }; 065 String[] values = { "MemoryPool", "Par Eden Space" }; 066 Hashtable<String, String> properties = JSONMetricUtil.buldKeyValueTable(keys, values); 067 ObjectName testObject = 068 JSONMetricUtil.buildObjectName(JSONMetricUtil.JAVA_LANG_DOMAIN, properties); 069 assertEquals(JSONMetricUtil.JAVA_LANG_DOMAIN, testObject.getDomain()); 070 assertEquals(testObject.getKeyPropertyList(), properties); 071 } 072 073 @Test 074 public void testGetLastGCInfo() { 075 List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); 076 for (GarbageCollectorMXBean bean : gcBeans) { 077 ObjectName on = bean.getObjectName(); 078 Object value = JSONMetricUtil.getValueFromMBean(on, "LastGcInfo"); 079 LOG.info("Collector Info: " + value); 080 if (value != null && value instanceof CompositeData) { 081 CompositeData cds = (CompositeData) value; 082 assertNotNull(cds.get("duration")); 083 } 084 } 085 } 086}