Repository: brooklyn-server Updated Branches: refs/heads/master 459e892af -> fe84df9db
more performance monitoring util routines Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/330f89ee Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/330f89ee Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/330f89ee Branch: refs/heads/master Commit: 330f89ee34ee450f00eb6760721d87d5a5466b25 Parents: 36de666 Author: Alex Heneveld <[email protected]> Authored: Mon Sep 11 11:03:52 2017 +0100 Committer: Alex Heneveld <[email protected]> Committed: Tue Sep 12 17:07:29 2017 +0100 ---------------------------------------------------------------------- .../test/performance/PerformanceTestUtils.java | 50 +++++++++++++++----- 1 file changed, 37 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/330f89ee/test-support/src/main/java/org/apache/brooklyn/test/performance/PerformanceTestUtils.java ---------------------------------------------------------------------- diff --git a/test-support/src/main/java/org/apache/brooklyn/test/performance/PerformanceTestUtils.java b/test-support/src/main/java/org/apache/brooklyn/test/performance/PerformanceTestUtils.java index c747e8b..7d108f0 100644 --- a/test-support/src/main/java/org/apache/brooklyn/test/performance/PerformanceTestUtils.java +++ b/test-support/src/main/java/org/apache/brooklyn/test/performance/PerformanceTestUtils.java @@ -29,6 +29,7 @@ import java.util.concurrent.TimeUnit; import javax.management.MBeanServer; import javax.management.ObjectName; +import org.apache.brooklyn.util.exceptions.RuntimeInterruptedException; import org.apache.brooklyn.util.time.Duration; import org.apache.brooklyn.util.time.Time; import org.slf4j.Logger; @@ -41,6 +42,7 @@ public class PerformanceTestUtils { private static final Logger LOG = LoggerFactory.getLogger(PerformanceTestUtils.class); private static boolean hasLoggedProcessCpuTimeUnavailable; + private static boolean hasLoggedProcessCpuLoadUnavailable; public static long getProcessCpuTime() { try { @@ -50,7 +52,35 @@ public class PerformanceTestUtils { } catch (Exception e) { if (!hasLoggedProcessCpuTimeUnavailable) { hasLoggedProcessCpuTimeUnavailable = true; - LOG.warn("ProcessCPuTime not available in local JVM MXBean "+ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME+" (only available in sun JVM?)"); + LOG.warn("ProcessCuuTime not available in local JVM MXBean "+ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME+" (only available in sun JVM?)"); + } + return -1; + } + } + + public static double getProcessCpuTime(Duration period) { + Stopwatch stopwatch = Stopwatch.createStarted(); + long prevCpuTime = getProcessCpuTime(); + if (prevCpuTime==-1) { + return -1; + } + Time.sleep(period); + long currentCpuTime = getProcessCpuTime(); + + long elapsedTime = stopwatch.elapsed(TimeUnit.MILLISECONDS); + return (elapsedTime > 0) ? ((double)currentCpuTime-prevCpuTime) / TimeUnit.MILLISECONDS.toNanos(elapsedTime) : -1; + } + + /** Not very fine-grained so not very useful; use {@link #getProcessCpuTime(Duration)} */ + public static double getProcessCpuAverage() { + try { + MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); + ObjectName osMBeanName = ObjectName.getInstance(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME); + return (Double) mbeanServer.getAttribute(osMBeanName, "ProcessCpuLoad"); + } catch (Exception e) { + if (!hasLoggedProcessCpuLoadUnavailable) { + hasLoggedProcessCpuLoadUnavailable = true; + LOG.warn("ProcessCpuLoad not available in local JVM MXBean "+ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME+" (only available in sun JVM?)"); } return -1; } @@ -75,28 +105,22 @@ public class PerformanceTestUtils { Future<?> future = executor.submit(new Runnable() { @Override public void run() { try { - long prevCpuTime = getProcessCpuTime(); - if (prevCpuTime == -1) { + if (getProcessCpuTime() == -1) { LOG.warn("ProcessCPuTime not available; cannot sample; aborting"); return; } while (true) { - Stopwatch stopwatch = Stopwatch.createStarted(); - Thread.sleep(period.toMilliseconds()); - long currentCpuTime = getProcessCpuTime(); - - long elapsedTime = stopwatch.elapsed(TimeUnit.MILLISECONDS); - double fractionCpu = (elapsedTime > 0) ? ((double)currentCpuTime-prevCpuTime) / TimeUnit.MILLISECONDS.toNanos(elapsedTime) : -1; - prevCpuTime = currentCpuTime; - + Stopwatch timerForReporting = Stopwatch.createStarted(); + double fractionCpu = getProcessCpuTime(period); + long elapsedTime = timerForReporting.elapsed(TimeUnit.MILLISECONDS); LOG.info("CPU fraction over last {} was {} ({})", new Object[] { - Time.makeTimeStringRounded(elapsedTime), fractionCpu, loggingContext}); + Time.makeTimeStringRounded(elapsedTime), ((int)(1000*fractionCpu))/1000.0, loggingContext}); if (cpuFractions != null) { cpuFractions.add(fractionCpu); } } - } catch (InterruptedException e) { + } catch (RuntimeInterruptedException e) { return; // graceful termination } finally { executor.shutdownNow();
