/* * Copyright (C) 2012 Facebook, Inc. * * Licensed 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 com.facebook.stats.mx; import org.testng.Assert; import org.testng.annotations.Test; import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.TreeMap; import com.facebook.logging.Logger; import com.facebook.logging.LoggerImpl; /** * Test {@link JVMStatsExporter} */ public class TestJVMStatsExporter { private static final Logger LOG = LoggerImpl.getLogger(TestJVMStatsExporter.class); @Test(groups = "fast") public void testAllStats() throws Exception { Stats stats = new Stats(); JVMStatsExporter exporter = new JVMStatsExporter(stats); Map<String, Long> exportedStats = getExportedStats(stats); Assert.assertTrue(exportedStats.size() > 10); // Sort the stats for printing. exportedStats = new TreeMap<>(exportedStats); for (Map.Entry<String, Long> entry : exportedStats.entrySet()) { // Print the stats for visual examination LOG.info("%s = %s", entry.getKey(), entry.getValue()); } // Verify that top level numeric stat is available Assert.assertTrue(exportedStats.containsKey("jvm.Memory.ObjectPendingFinalizationCount")); // Verify that a numeric attribute of composite data is available Assert.assertTrue(exportedStats.containsKey("jvm.Memory.HeapMemoryUsage.committed")); } @Test(groups = "fast") public void testFilteredStats() throws Exception { Stats stats = new Stats(); // Chose an MBean that has a good chance of being there across different jvm versions JVMStatsExporter jvmStatsExporter = new JVMStatsExporter( stats, ".*(\\.UsageThresholdCount|PeakUsage.committed)", "java.lang:type=MemoryPool,name=Code Cache" ); Map<String, Long> exportedStats = getExportedStats(stats); Assert.assertEquals(2, exportedStats.size()); Assert.assertTrue(exportedStats.containsKey("jvm.MemoryPool.Code_Cache.UsageThresholdCount")); Assert.assertTrue(exportedStats.containsKey("jvm.MemoryPool.Code_Cache.PeakUsage.committed")); } @Test(groups = "fast") public void testStatNameReplacer() throws Exception { Stats stats = new Stats(); // Chose an MBean that has a good chance of being there across different jvm versions JVMStatsExporter jvmStatsExporter = new JVMStatsExporter( stats, (bean, attribute, key) -> { String name = "test"; if (attribute != null) { name += "." + attribute; } if (key != null) { name += "." + key; } return Optional.of(name); }, "java.lang:type=MemoryPool,name=Code Cache" ); Map<String, Long> exportedStats = getExportedStats(stats); Assert.assertTrue(exportedStats.containsKey("test.Usage.used")); Assert.assertTrue(exportedStats.containsKey("test.Usage.max")); } @Test(groups = "fast") public void testStatNameReplacerFilterAll() throws Exception { Stats stats = new Stats(); // Chose an MBean that has a good chance of being there across different jvm versions JVMStatsExporter jvmStatsExporter = new JVMStatsExporter( stats, (bean, attribute, key) -> Optional.empty(), "java.lang:type=MemoryPool,name=Code Cache" ); Map<String, Long> exportedStats = getExportedStats(stats); Assert.assertEquals(exportedStats.size(), 0); } private static Map<String, Long> getExportedStats(Stats stats) { Map<String, Long> statsMap = new HashMap<>(); stats.exportCounters(statsMap); return statsMap; } }