histogram.py
Go to the documentation of this file.
1 # Copyright 2016 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 math
16 import threading
17 
18 from src.proto.grpc.testing import stats_pb2
19 
20 
21 class Histogram(object):
22  """Histogram class used for recording performance testing data.
23 
24  This class is thread safe.
25  """
26 
27  def __init__(self, resolution, max_possible):
28  self._lock = threading.Lock()
29  self._resolution = resolution
30  self._max_possible = max_possible
31  self._sum = 0
32  self._sum_of_squares = 0
33  self.multiplier = 1.0 + self._resolution
34  self._count = 0
35  self._min = self._max_possible
36  self._max = 0
37  self._buckets = [0] * (self._bucket_for(self._max_possible) + 1)
38 
39  def reset(self):
40  with self._lock:
41  self._sum = 0
42  self._sum_of_squares = 0
43  self._count = 0
44  self._min = self._max_possible
45  self._max = 0
46  self._buckets = [0] * (self._bucket_for(self._max_possible) + 1)
47 
48  def add(self, val):
49  with self._lock:
50  self._sum += val
51  self._sum_of_squares += val * val
52  self._count += 1
53  self._min = min(self._min, val)
54  self._max = max(self._max, val)
55  self._buckets[self._bucket_for(val)] += 1
56 
57  def get_data(self):
58  with self._lock:
59  data = stats_pb2.HistogramData()
60  data.bucket.extend(self._buckets)
61  data.min_seen = self._min
62  data.max_seen = self._max
63  data.sum = self._sum
64  data.sum_of_squares = self._sum_of_squares
65  data.count = self._count
66  return data
67 
68  def merge(self, another_data):
69  with self._lock:
70  for i in range(len(self._buckets)):
71  self._buckets[i] += another_data.bucket[i]
72  self._min = min(self._min, another_data.min_seen)
73  self._max = max(self._max, another_data.max_seen)
74  self._sum += another_data.sum
75  self._sum_of_squares += another_data.sum_of_squares
76  self._count += another_data.count
77 
78  def _bucket_for(self, val):
79  val = min(val, self._max_possible)
80  return int(math.log(val, self.multiplier))
capstone.range
range
Definition: third_party/bloaty/third_party/capstone/bindings/python/capstone/__init__.py:6
tests.qps.histogram.Histogram
Definition: histogram.py:21
tests.qps.histogram.Histogram._lock
_lock
Definition: histogram.py:28
tests.qps.histogram.Histogram._max
_max
Definition: histogram.py:36
tests.qps.histogram.Histogram.multiplier
multiplier
Definition: histogram.py:33
tests.qps.histogram.Histogram.merge
def merge(self, another_data)
Definition: histogram.py:68
tests.qps.histogram.Histogram._sum
_sum
Definition: histogram.py:31
xds_interop_client.int
int
Definition: xds_interop_client.py:113
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
tests.qps.histogram.Histogram._buckets
_buckets
Definition: histogram.py:37
tests.qps.histogram.Histogram._count
_count
Definition: histogram.py:34
tests.qps.histogram.Histogram.__init__
def __init__(self, resolution, max_possible)
Definition: histogram.py:27
tests.qps.histogram.Histogram._resolution
_resolution
Definition: histogram.py:29
tests.qps.histogram.Histogram.reset
def reset(self)
Definition: histogram.py:39
min
#define min(a, b)
Definition: qsort.h:83
tests.qps.histogram.Histogram._sum_of_squares
_sum_of_squares
Definition: histogram.py:32
tests.qps.histogram.Histogram.add
def add(self, val)
Definition: histogram.py:48
tests.qps.histogram.Histogram.get_data
def get_data(self)
Definition: histogram.py:57
tests.qps.histogram.Histogram._bucket_for
def _bucket_for(self, val)
Definition: histogram.py:78
tests.qps.histogram.Histogram._min
_min
Definition: histogram.py:35
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
tests.qps.histogram.Histogram._max_possible
_max_possible
Definition: histogram.py:30


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:12