MetricsManager { public void addTime(BigDecimal elapsedTime) { if (elapsedTime.compareTo(BigDecimal.ZERO) <= 0) { throw new IllegalArgumentException("Elapsed time is negative or zero"); } mapValues.put(longAdder.longValue(), elapsedTime); longAdder.increment(); final BigDecimal count = new BigDecimal(metrics.getCount()); final BigDecimal averageTime = metrics.getAverageTime(); final BigDecimal minTime = metrics.getMinTime(); final BigDecimal maxTime = metrics.getMaxTime(); if (elapsedTime.compareTo(maxTime) == 1) { metrics.setMaxTime(elapsedTime); } if (elapsedTime.compareTo(minTime) == -1 || count.compareTo(BigDecimal.ZERO) == 0) { metrics.setMinTime(elapsedTime); } metrics.incrementCount(); metrics.setAverageTime((averageTime.multiply(count).add(elapsedTime)).divide(count.add(BigDecimal.ONE), BigDecimal.ROUND_HALF_UP)); } MetricsManager(); void addTime(BigDecimal elapsedTime); long getCount(); BigDecimal getAverageTime(); BigDecimal getMinTime(); BigDecimal getMaxTime(); BigDecimal get90Percentile(); BigDecimal get95Percentile(); BigDecimal get99Percentile(); List<BigDecimal> getValues(); String toJson(); @Override String toString();  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testZeroTime() { metricsManager.addTime(BigDecimal.ZERO); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNegativeTime() { metricsManager.addTime(getRandomTime().multiply(BigDecimal.valueOf(-1))); }