OsCpuUtil { CpuStats getCpuStatsFromLine(final String line) { try { final String[] cpuInfo = line.split("\\s+"); final long user = Long.parseLong(cpuInfo[1]); final long nice = Long.parseLong(cpuInfo[2]); final long system = Long.parseLong(cpuInfo[3]); final long idle = Long.parseLong(cpuInfo[4]); final long iowait = Long.parseLong(cpuInfo[5]); final long irq = Long.parseLong(cpuInfo[6]); final long softirq = Long.parseLong(cpuInfo[7]); final long steal = Long.parseLong(cpuInfo[8]); final long totalCpuTime = user + nice + system + idle + iowait + irq + softirq + steal; final long idleCpuTime = idle + iowait; return new CpuStats(totalCpuTime, idleCpuTime); } catch (final NumberFormatException | ArrayIndexOutOfBoundsException e) { final String errMsg = "Failed to parse cpu stats from line: " + line; logger.error(errMsg, e); } return null; } @Inject  OsCpuUtil(int numCpuStatsToCollect); double getCpuLoad();  }
@Test public void testGetCpuStatsFromLine() { final String line = "cpu 1 2 4 8 16 32 64 128 256 512 "; final CpuStats cpuStats = this.osCpuUtil.getCpuStatsFromLine(line); assertThat(cpuStats.getSysUptime()).isEqualTo(255); assertThat(cpuStats.getTimeCpuIdle()).isEqualTo(24); }
@Test public void testGetCpuStatsFromLineWithInvalidInput() { String line = "cpu 7 4 3 6 2"; Assert.assertNull(this.osCpuUtil.getCpuStatsFromLine(line)); line = "cpu 7.1 4.2 3 6 2 2 0 0 0"; Assert.assertNull(this.osCpuUtil.getCpuStatsFromLine(line)); }