JvmMetrics implements MetricsSource { @Override public void getMetrics(MetricsCollector collector, boolean all) { MetricsRecordBuilder rb = collector.addRecord(JvmMetrics) .setContext("jvm").tag(ProcessName, processName) .tag(SessionId, sessionId); getMemoryUsage(rb); getGcUsage(rb); if (threadMXBean != null) { getThreadUsage(rb); } else { getThreadUsageFromGroup(rb); } getEventCounters(rb); } @VisibleForTesting  JvmMetrics(String processName, String sessionId, boolean useThreadMXBean); @VisibleForTesting synchronized void registerIfNeeded(); void setPauseMonitor(final JvmPauseMonitor pauseMonitor); void setGcTimeMonitor(GcTimeMonitor gcTimeMonitor); static JvmMetrics create(String processName, String sessionId,
                                  MetricsSystem ms); static void reattach(MetricsSystem ms, JvmMetrics jvmMetrics); static JvmMetrics initSingleton(String processName, String sessionId); static void shutdownSingleton(); @Override void getMetrics(MetricsCollector collector, boolean all); static final float MEMORY_MAX_UNLIMITED_MB; }
@Test public void testGetMetricsPerf() { JvmMetrics jvmMetricsUseMXBean = new JvmMetrics("test", "test", true); JvmMetrics jvmMetrics = new JvmMetrics("test", "test", false); MetricsCollectorImpl collector = new MetricsCollectorImpl(); jvmMetrics.getMetrics(collector, true); jvmMetricsUseMXBean.getMetrics(collector, true); int[] numThreadsCases = {100, 200, 500, 1000, 2000, 3000}; List<TestThread> threads = new ArrayList(); for (int numThreads : numThreadsCases) { updateThreadsAndWait(threads, numThreads); long startNs = System.nanoTime(); jvmMetricsUseMXBean.getMetrics(collector, true); long processingNsFromMXBean = System.nanoTime() - startNs; startNs = System.nanoTime(); jvmMetrics.getMetrics(collector, true); long processingNsFromGroup = System.nanoTime() - startNs; System.out.println( "#Threads=" + numThreads + ", ThreadMXBean=" + processingNsFromMXBean + " ns, ThreadGroup=" + processingNsFromGroup + " ns, ratio: " + ( processingNsFromMXBean / processingNsFromGroup)); } updateThreadsAndWait(threads, 0); }