Craig Tiller | f11a40d | 2017-09-12 10:54:20 -0700 | [diff] [blame] | 1 | # Copyright 2017 gRPC authors. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import collections |
| 16 | |
ncteisen | eb12815 | 2017-12-11 17:48:24 -0800 | [diff] [blame] | 17 | |
Craig Tiller | 5d61560 | 2017-09-12 13:15:58 -0700 | [diff] [blame] | 18 | def _threshold_for_count_below(buckets, boundaries, count_below): |
ncteisen | eb12815 | 2017-12-11 17:48:24 -0800 | [diff] [blame] | 19 | count_so_far = 0 |
| 20 | for lower_idx in range(0, len(buckets)): |
| 21 | count_so_far += buckets[lower_idx] |
| 22 | if count_so_far >= count_below: |
| 23 | break |
| 24 | if count_so_far == count_below: |
| 25 | # this bucket hits the threshold exactly... we should be midway through |
| 26 | # any run of zero values following the bucket |
| 27 | for upper_idx in range(lower_idx + 1, len(buckets)): |
| 28 | if buckets[upper_idx] != 0: |
| 29 | break |
| 30 | return (boundaries[lower_idx] + boundaries[upper_idx]) / 2.0 |
| 31 | else: |
| 32 | # treat values as uniform throughout the bucket, and find where this value |
| 33 | # should lie |
| 34 | lower_bound = boundaries[lower_idx] |
| 35 | upper_bound = boundaries[lower_idx + 1] |
| 36 | return (upper_bound - (upper_bound - lower_bound) * |
| 37 | (count_so_far - count_below) / float(buckets[lower_idx])) |
| 38 | |
Craig Tiller | f11a40d | 2017-09-12 10:54:20 -0700 | [diff] [blame] | 39 | |
| 40 | def percentile(buckets, pctl, boundaries): |
ncteisen | eb12815 | 2017-12-11 17:48:24 -0800 | [diff] [blame] | 41 | return _threshold_for_count_below(buckets, boundaries, |
| 42 | sum(buckets) * pctl / 100.0) |
| 43 | |
Craig Tiller | f11a40d | 2017-09-12 10:54:20 -0700 | [diff] [blame] | 44 | |
| 45 | def counter(core_stats, name): |
ncteisen | eb12815 | 2017-12-11 17:48:24 -0800 | [diff] [blame] | 46 | for stat in core_stats['metrics']: |
| 47 | if stat['name'] == name: |
| 48 | return int(stat.get('count', 0)) |
| 49 | |
Craig Tiller | f11a40d | 2017-09-12 10:54:20 -0700 | [diff] [blame] | 50 | |
| 51 | Histogram = collections.namedtuple('Histogram', 'buckets boundaries') |
ncteisen | eb12815 | 2017-12-11 17:48:24 -0800 | [diff] [blame] | 52 | |
| 53 | |
Craig Tiller | f11a40d | 2017-09-12 10:54:20 -0700 | [diff] [blame] | 54 | def histogram(core_stats, name): |
ncteisen | eb12815 | 2017-12-11 17:48:24 -0800 | [diff] [blame] | 55 | for stat in core_stats['metrics']: |
| 56 | if stat['name'] == name: |
| 57 | buckets = [] |
| 58 | boundaries = [] |
| 59 | for b in stat['histogram']['buckets']: |
| 60 | buckets.append(int(b.get('count', 0))) |
| 61 | boundaries.append(int(b.get('start', 0))) |
| 62 | return Histogram(buckets=buckets, boundaries=boundaries) |