From 4606935538f025ca3b51c0c1c14a8e30dc6ffc2c Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Thu, 5 Dec 2024 04:31:34 +0900 Subject: [PATCH] Polish gh-5602 (#5732) --- .../binder/jvm/JvmGcMetricsTest.java | 33 ++++++------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jvm/JvmGcMetricsTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jvm/JvmGcMetricsTest.java index c5e60c0bd6..f3b3f06bf2 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jvm/JvmGcMetricsTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jvm/JvmGcMetricsTest.java @@ -127,8 +127,8 @@ else if (mbean.getName().contains("Cycles")) { // cause GC to record new metrics System.gc(); - checkPhaseCount(initialPauseCount, initialConcurrentCount); - checkCollectionTime(initialPauseTimeMs, initialConcurrentTimeMs); + checkPhaseCountAndCollectionTime(initialPauseCount, initialConcurrentCount, initialPauseTimeMs, + initialConcurrentTimeMs); } static boolean isPauseCyclesGc() { @@ -203,23 +203,30 @@ public void handleNotification(Notification notification, Object handback) { } - private void checkPhaseCount(long initialPauseCount, long initialConcurrentCount) { + private void checkPhaseCountAndCollectionTime(long initialPauseCount, long initialConcurrentCount, + long initialPauseTimeMs, long initialConcurrentTimeMs) { await().atMost(200, TimeUnit.MILLISECONDS).untilAsserted(() -> { long pauseCount = 0; long concurrentCount = 0; + long pauseTimeMs = 0; + long concurrentTimeMs = 0; // get metrics from JMX again to obtain the difference for (GarbageCollectorMXBean mbean : ManagementFactory.getGarbageCollectorMXBeans()) { if (mbean.getName().contains("Pauses")) { pauseCount += mbean.getCollectionCount(); + pauseTimeMs += mbean.getCollectionTime(); } else if (mbean.getName().contains("Cycles")) { concurrentCount += mbean.getCollectionCount(); + concurrentTimeMs += mbean.getCollectionTime(); } } long expectedPauseCount = pauseCount - initialPauseCount; long expectedConcurrentCount = concurrentCount - initialConcurrentCount; + long expectedPauseTimeMs = pauseTimeMs - initialPauseTimeMs; + long expectedConcurrentTimeMs = concurrentTimeMs - initialConcurrentTimeMs; long observedPauseCount = registry.find("jvm.gc.pause").timers().stream().mapToLong(Timer::count).sum(); long observedConcurrentCount = registry.find("jvm.gc.concurrent.phase.time") @@ -229,26 +236,6 @@ else if (mbean.getName().contains("Cycles")) { .sum(); assertThat(observedPauseCount).isEqualTo(expectedPauseCount); assertThat(observedConcurrentCount).isEqualTo(expectedConcurrentCount); - }); - } - - private void checkCollectionTime(long initialPauseTimeMs, long initialConcurrentTimeMs) { - await().atMost(200, TimeUnit.MILLISECONDS).untilAsserted(() -> { - long pauseTimeMs = 0; - long concurrentTimeMs = 0; - - // get metrics from JMX again to obtain the difference - for (GarbageCollectorMXBean mbean : ManagementFactory.getGarbageCollectorMXBeans()) { - if (mbean.getName().contains("Pauses")) { - pauseTimeMs += mbean.getCollectionTime(); - } - else if (mbean.getName().contains("Cycles")) { - concurrentTimeMs += mbean.getCollectionTime(); - } - } - - long expectedPauseTimeMs = pauseTimeMs - initialPauseTimeMs; - long expectedConcurrentTimeMs = concurrentTimeMs - initialConcurrentTimeMs; double observedPauseTimeMs = registry.find("jvm.gc.pause") .timers()