ihji commented on a change in pull request #12754: URL: https://github.com/apache/beam/pull/12754#discussion_r483472796
########## File path: sdks/python/apache_beam/utils/histogram.py ########## @@ -0,0 +1,176 @@ +# +# 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. +# + +from __future__ import absolute_import +from __future__ import division + +import logging +import math +import threading + +_LOGGER = logging.getLogger(__name__) + + +class Histogram(object): + """A histogram that supports estimated percentile with linear interpolation. + + This class is considered experimental and may break or receive backwards- + incompatible changes in future versions of the Apache Beam SDK. + """ + def __init__(self, bucket_type): + self._lock = threading.Lock() + self._bucket_type = bucket_type + self._buckets = {} + self._num_records = 0 + self._num_top_records = 0 + self._num_bot_records = 0 + + def clear(self): + with self._lock: + self._buckets = {} + self._num_records = 0 + self._num_top_records = 0 + self._num_bot_records = 0 + + def record(self, *args): + for arg in args: + self._record(arg) + + def _record(self, value): + range_from = self._bucket_type.range_from() + range_to = self._bucket_type.range_to() + with self._lock: + if value >= range_to: + _LOGGER.warning('record is out of upper bound %s: %s', range_to, value) + self._num_top_records += 1 + elif value < range_from: + _LOGGER.warning( + 'record is out of lower bound %s: %s', range_from, value) + self._num_bot_records += 1 + else: + index = self._bucket_type.bucket_index(value) + self._buckets[index] = self._buckets.get(index, 0) + 1 + self._num_records += 1 + + def total_count(self): + return self._num_records + self._num_top_records + self._num_bot_records + + def p99(self): + return self.get_linear_interpolation(0.99) + + def p90(self): + return self.get_linear_interpolation(0.90) + + def p50(self): + return self.get_linear_interpolation(0.50) + + def get_linear_interpolation(self, percentile): + """Calculate percentile estimation based on linear interpolation. + + It first finds the bucket which includes the target percentile and + projects the estimated point in the bucket by assuming all the elements + in the bucket are uniformly distributed. + """ + with self._lock: + total_num_records = self.total_count() + if total_num_records == 0: + raise RuntimeError('histogram has no record.') + + index = 0 + record_sum = self._num_bot_records + if record_sum / total_num_records >= percentile: + return float('-inf') 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]
