diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/util/HistogramData.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/util/HistogramData.java index 28d4a38f5587..494cdf4499cc 100644 --- a/sdks/java/core/src/main/java/org/apache/beam/sdk/util/HistogramData.java +++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/util/HistogramData.java @@ -142,15 +142,8 @@ public synchronized long getTotalCount() { public synchronized String getPercentileString(String elemType, String unit) { return String.format( - "Total number of %s: %s, P99: %s%s, P90: %s%s, P50: %s%s", - elemType, - getTotalCount(), - DoubleMath.roundToInt(p99(), RoundingMode.HALF_UP), - unit, - DoubleMath.roundToInt(p90(), RoundingMode.HALF_UP), - unit, - DoubleMath.roundToInt(p50(), RoundingMode.HALF_UP), - unit); + "Total number of %s: %s, P99: %.0f %s, P90: %.0f %s, P50: %.0f %s", + elemType, getTotalCount(), p99(), unit, p90(), unit, p50(), unit); } /** @@ -196,7 +189,7 @@ public double p50() { private synchronized double getLinearInterpolation(double percentile) { long totalNumOfRecords = getTotalCount(); if (totalNumOfRecords == 0) { - throw new RuntimeException("histogram has no record."); + return Double.NaN; } int index; double recordSum = numBottomRecords; @@ -245,10 +238,11 @@ public abstract static class LinearBuckets implements BucketType { public static LinearBuckets of(double start, double width, int numBuckets) { if (width <= 0) { - throw new RuntimeException(String.format("width should be greater than zero: %f", width)); + throw new IllegalArgumentException( + String.format("width should be greater than zero: %f", width)); } if (numBuckets <= 0) { - throw new RuntimeException( + throw new IllegalArgumentException( String.format("numBuckets should be greater than zero: %d", numBuckets)); } return new AutoValue_HistogramData_LinearBuckets(start, width, numBuckets); diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/util/HistogramDataTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/util/HistogramDataTest.java index a9407783219b..b6e4d989a8f3 100644 --- a/sdks/java/core/src/test/java/org/apache/beam/sdk/util/HistogramDataTest.java +++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/util/HistogramDataTest.java @@ -19,7 +19,6 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThrows; import org.junit.Test; import org.junit.runner.RunWith; @@ -120,6 +119,9 @@ public void testP50NegativeInfinity() { HistogramData histogramData = HistogramData.linear(0, 0.2, 50); histogramData.record(-1, -2, -3, -4, -5, 0, 1, 2, 3, 4); assertThat(histogramData.p50(), equalTo(Double.NEGATIVE_INFINITY)); + assertThat( + histogramData.getPercentileString("meows", "cats"), + equalTo("Total number of meows: 10, P99: 4 cats, P90: 3 cats, P50: -Infinity cats")); } @Test @@ -127,12 +129,16 @@ public void testP50PositiveInfinity() { HistogramData histogramData = HistogramData.linear(0, 0.2, 50); histogramData.record(6, 7, 8, 9, 10, 11, 12, 13, 14, 15); assertThat(histogramData.p50(), equalTo(Double.POSITIVE_INFINITY)); + assertThat( + histogramData.getPercentileString("meows", "cats"), + equalTo( + "Total number of meows: 10, P99: Infinity cats, P90: Infinity cats, P50: Infinity cats")); } @Test public void testEmptyP99() { HistogramData histogramData = HistogramData.linear(0, 0.2, 50); - assertThrows(RuntimeException.class, histogramData::p99); + assertThat(histogramData.p99(), equalTo(Double.NaN)); } @Test