ihji commented on a change in pull request #12609: URL: https://github.com/apache/beam/pull/12609#discussion_r473769047
########## File path: sdks/java/core/src/main/java/org/apache/beam/sdk/util/Histogram.java ########## @@ -0,0 +1,151 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.sdk.util; + +import java.math.RoundingMode; +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.math.DoubleMath; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** A histogram that supports estimated percentile with linear interpolation. */ +public class Histogram { + private static final Logger LOG = LoggerFactory.getLogger(Histogram.class); + + private static final int DEFAULT_NUM_OF_BUCKETS = 50; + + private final double rangeFrom; + private final double rangeTo; + private final int numOfBuckets; + + private long[] buckets; + private final double bucketSize; + private long totalNumOfRecords; + + private final boolean ignoreOutOfRangeRecord; + + private Histogram( + double rangeFrom, double rangeTo, int numOfBuckets, boolean ignoreOutOfRangeRecord) { + if (rangeFrom < 0) { + throw new RuntimeException(String.format("only positive range allowed: %f", rangeFrom)); + } + if (rangeFrom >= rangeTo) { + throw new RuntimeException( + String.format("rangeTo should be larger than rangeFrom: [%f, %f)", rangeFrom, rangeTo)); + } + if (numOfBuckets <= 0) { + throw new RuntimeException( + String.format("numOfBuckets should be greater than zero: %d", numOfBuckets)); + } + this.rangeFrom = rangeFrom; + this.rangeTo = rangeTo; + this.numOfBuckets = numOfBuckets; + this.ignoreOutOfRangeRecord = ignoreOutOfRangeRecord; + this.buckets = new long[numOfBuckets]; + this.bucketSize = (rangeTo - rangeFrom) / numOfBuckets; + this.totalNumOfRecords = 0; + } + + /** + * Create a histogram. + * + * @param rangeFrom The minimum value that this histogram can record. Cannot be negative. Review comment: Done. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
