@Test public void four_processors_system_load_average_1_percent_results_cpu_load_0_25() { when(bean.getAvailableProcessors()).thenReturn(4); when(bean.getSystemLoadAverage()).thenReturn(1.0); assertEquals(0.25, monitorToTest.getCPULoadAverage(),0.01); }
public double getCPULoadAverage() { if (osMBean == null) { return -1; } synchronized (monitor) { if (cpuData.isCacheValid()) { return cpuData.getValue(); } double systemLoadAverage = osMBean.getSystemLoadAverage(); int availableProcessors = osMBean.getAvailableProcessors(); if (systemLoadAverage < 0) { cpuData.setValue(systemLoadAverage); } else { if (availableProcessors == 0) { cpuData.setValue(-2); } else { cpuData.setValue(systemLoadAverage / availableProcessors); } } StringBuilder sb = new StringBuilder(); sb.append("CPU load average:").append(cpuData.getValue()); sb.append(", system load average:").append(systemLoadAverage); sb.append(", available processors:").append(availableProcessors); cpuData.setAdditionalData(KEY_DESCRIPTION, sb.toString()); double result = cpuData.getValue(); LOG.trace("Checked cpu usage, value now:{}", result); return result; } }
CPUMonitor { public double getCPULoadAverage() { if (osMBean == null) { return -1; } synchronized (monitor) { if (cpuData.isCacheValid()) { return cpuData.getValue(); } double systemLoadAverage = osMBean.getSystemLoadAverage(); int availableProcessors = osMBean.getAvailableProcessors(); if (systemLoadAverage < 0) { cpuData.setValue(systemLoadAverage); } else { if (availableProcessors == 0) { cpuData.setValue(-2); } else { cpuData.setValue(systemLoadAverage / availableProcessors); } } StringBuilder sb = new StringBuilder(); sb.append("CPU load average:").append(cpuData.getValue()); sb.append(", system load average:").append(systemLoadAverage); sb.append(", available processors:").append(availableProcessors); cpuData.setAdditionalData(KEY_DESCRIPTION, sb.toString()); double result = cpuData.getValue(); LOG.trace("Checked cpu usage, value now:{}", result); return result; } } }
CPUMonitor { public double getCPULoadAverage() { if (osMBean == null) { return -1; } synchronized (monitor) { if (cpuData.isCacheValid()) { return cpuData.getValue(); } double systemLoadAverage = osMBean.getSystemLoadAverage(); int availableProcessors = osMBean.getAvailableProcessors(); if (systemLoadAverage < 0) { cpuData.setValue(systemLoadAverage); } else { if (availableProcessors == 0) { cpuData.setValue(-2); } else { cpuData.setValue(systemLoadAverage / availableProcessors); } } StringBuilder sb = new StringBuilder(); sb.append("CPU load average:").append(cpuData.getValue()); sb.append(", system load average:").append(systemLoadAverage); sb.append(", available processors:").append(availableProcessors); cpuData.setAdditionalData(KEY_DESCRIPTION, sb.toString()); double result = cpuData.getValue(); LOG.trace("Checked cpu usage, value now:{}", result); return result; } } CPUMonitor(OperatingSystemMXBean osMBean, long cacheTimeInMilliseconds); }
CPUMonitor { public double getCPULoadAverage() { if (osMBean == null) { return -1; } synchronized (monitor) { if (cpuData.isCacheValid()) { return cpuData.getValue(); } double systemLoadAverage = osMBean.getSystemLoadAverage(); int availableProcessors = osMBean.getAvailableProcessors(); if (systemLoadAverage < 0) { cpuData.setValue(systemLoadAverage); } else { if (availableProcessors == 0) { cpuData.setValue(-2); } else { cpuData.setValue(systemLoadAverage / availableProcessors); } } StringBuilder sb = new StringBuilder(); sb.append("CPU load average:").append(cpuData.getValue()); sb.append(", system load average:").append(systemLoadAverage); sb.append(", available processors:").append(availableProcessors); cpuData.setAdditionalData(KEY_DESCRIPTION, sb.toString()); double result = cpuData.getValue(); LOG.trace("Checked cpu usage, value now:{}", result); return result; } } CPUMonitor(OperatingSystemMXBean osMBean, long cacheTimeInMilliseconds); double getCPULoadAverage(); String getDescription(); void setCacheTimeInMillis(long cacheTimeInMilliseconds); }
CPUMonitor { public double getCPULoadAverage() { if (osMBean == null) { return -1; } synchronized (monitor) { if (cpuData.isCacheValid()) { return cpuData.getValue(); } double systemLoadAverage = osMBean.getSystemLoadAverage(); int availableProcessors = osMBean.getAvailableProcessors(); if (systemLoadAverage < 0) { cpuData.setValue(systemLoadAverage); } else { if (availableProcessors == 0) { cpuData.setValue(-2); } else { cpuData.setValue(systemLoadAverage / availableProcessors); } } StringBuilder sb = new StringBuilder(); sb.append("CPU load average:").append(cpuData.getValue()); sb.append(", system load average:").append(systemLoadAverage); sb.append(", available processors:").append(availableProcessors); cpuData.setAdditionalData(KEY_DESCRIPTION, sb.toString()); double result = cpuData.getValue(); LOG.trace("Checked cpu usage, value now:{}", result); return result; } } CPUMonitor(OperatingSystemMXBean osMBean, long cacheTimeInMilliseconds); double getCPULoadAverage(); String getDescription(); void setCacheTimeInMillis(long cacheTimeInMilliseconds);  }