github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/utils/histogram_test.py (about) 1 # 2 # Licensed to the Apache Software Foundation (ASF) under one or more 3 # contributor license agreements. See the NOTICE file distributed with 4 # this work for additional information regarding copyright ownership. 5 # The ASF licenses this file to You under the Apache License, Version 2.0 6 # (the "License"); you may not use this file except in compliance with 7 # the License. You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # 17 18 """Unit tests for Histogram.""" 19 20 import unittest 21 22 from mock import patch 23 24 from apache_beam.utils.histogram import Histogram 25 from apache_beam.utils.histogram import LinearBucket 26 27 28 class HistogramTest(unittest.TestCase): 29 @patch('apache_beam.utils.histogram._LOGGER') 30 def test_out_of_range(self, mock_logger): 31 histogram = Histogram(LinearBucket(0, 20, 5)) 32 histogram.record(100) 33 mock_logger.warning.assert_called_with( 34 'record is out of upper bound %s: %s', 100, 100) 35 self.assertEqual(histogram.total_count(), 1) 36 37 def test_boundary_buckets(self): 38 histogram = Histogram(LinearBucket(0, 20, 5)) 39 histogram.record(0) 40 histogram.record(99.9) 41 self.assertEqual(histogram._buckets[0], 1) 42 self.assertEqual(histogram._buckets[4], 1) 43 44 def test_fractional_buckets(self): 45 histogram1 = Histogram(LinearBucket(0, 10 / 3, 3)) 46 histogram1.record(3.33) 47 histogram1.record(6.66) 48 self.assertEqual(histogram1._buckets[0], 1) 49 self.assertEqual(histogram1._buckets[1], 1) 50 51 histogram2 = Histogram(LinearBucket(0, 10 / 3, 3)) 52 histogram2.record(3.34) 53 histogram2.record(6.67) 54 self.assertEqual(histogram2._buckets[1], 1) 55 self.assertEqual(histogram2._buckets[2], 1) 56 57 def test_p50(self): 58 histogram1 = Histogram(LinearBucket(0, 0.2, 50)) 59 histogram1.record(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) 60 self.assertEqual(histogram1.p50(), 4.2) 61 62 histogram2 = Histogram(LinearBucket(0, 0.02, 50)) 63 histogram2.record(0, 0, 0) 64 self.assertEqual(histogram2.p50(), 0.01) 65 66 def test_p90(self): 67 histogram1 = Histogram(LinearBucket(0, 0.2, 50)) 68 histogram1.record(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) 69 self.assertEqual(histogram1.p90(), 8.2) 70 71 histogram2 = Histogram(LinearBucket(0, 0.02, 50)) 72 histogram2.record(0, 0, 0) 73 self.assertAlmostEqual(histogram2.p90(), 0.018, places=3) 74 75 def test_p99(self): 76 histogram1 = Histogram(LinearBucket(0, 0.2, 50)) 77 histogram1.record(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) 78 self.assertEqual(histogram1.p99(), 9.18) 79 80 histogram2 = Histogram(LinearBucket(0, 0.02, 50)) 81 histogram2.record(0, 0, 0) 82 self.assertAlmostEqual(histogram2.p99(), 0.02, places=3) 83 84 def test_p90_negative(self): 85 histogram1 = Histogram(LinearBucket(-10, 0.2, 50)) 86 histogram1.record(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10) 87 self.assertEqual(histogram1.p90(), -1.8) 88 89 histogram2 = Histogram(LinearBucket(-1, 0.02, 50)) 90 histogram2.record(-1, -1, -1) 91 self.assertEqual(histogram2.p90(), -0.982) 92 93 def test_p90_negative_to_positive(self): 94 histogram1 = Histogram(LinearBucket(-5, 0.2, 50)) 95 histogram1.record(-1, -2, -3, -4, -5, 0, 1, 2, 3, 4) 96 self.assertEqual(histogram1.p90(), 3.2) 97 98 histogram2 = Histogram(LinearBucket(-0.5, 0.02, 50)) 99 histogram2.record(-0.5, -0.5, -0.5) 100 self.assertEqual(histogram2.p90(), -0.482) 101 102 def test_p50_negative_infinity(self): 103 histogram = Histogram(LinearBucket(0, 0.2, 50)) 104 histogram.record(-1, -2, -3, -4, -5, 0, 1, 2, 3, 4) 105 self.assertEqual(histogram.p50(), float('-inf')) 106 self.assertIn('P50: <0', histogram.get_percentile_info()) 107 108 def test_p50_positive_infinity(self): 109 histogram = Histogram(LinearBucket(0, 0.2, 50)) 110 histogram.record(6, 7, 8, 9, 10, 11, 12, 13, 14, 15) 111 self.assertEqual(histogram.p50(), float('inf')) 112 self.assertIn('P50: >=10', histogram.get_percentile_info()) 113 114 def test_empty_p99(self): 115 histogram = Histogram(LinearBucket(0, 0.2, 50)) 116 with self.assertRaises(RuntimeError) as cm: 117 histogram.p99() 118 self.assertEqual(str(cm.exception), 'histogram has no record.') 119 120 def test_clear(self): 121 histogram = Histogram(LinearBucket(0, 0.2, 50)) 122 histogram.record(-1, 1, 2, 3) 123 self.assertEqual(histogram.total_count(), 4) 124 self.assertEqual(histogram._buckets[5], 1) 125 histogram.clear() 126 self.assertEqual(histogram.total_count(), 0) 127 self.assertEqual(histogram._buckets.get(5, 0), 0) 128 129 def test_equal(self): 130 histogram1 = Histogram(LinearBucket(0, 0.2, 50)) 131 histogram2 = Histogram(LinearBucket(0, 0.2, 50)) 132 self.assertEqual(histogram1, histogram2) 133 self.assertEqual(hash(histogram1), hash(histogram2)) 134 135 histogram1 = Histogram(LinearBucket(0, 0.2, 5)) 136 histogram2 = Histogram(LinearBucket(0, 0.2, 50)) 137 self.assertNotEqual(histogram1, histogram2) 138 139 histogram1 = Histogram(LinearBucket(0, 0.2, 50)) 140 histogram2 = Histogram(LinearBucket(0, 0.2, 50)) 141 histogram1.record(1) 142 histogram2.record(1) 143 self.assertEqual(histogram1, histogram2) 144 self.assertEqual(hash(histogram1), hash(histogram2)) 145 146 histogram1 = Histogram(LinearBucket(0, 0.2, 50)) 147 histogram2 = Histogram(LinearBucket(0, 0.2, 50)) 148 histogram1.record(1) 149 histogram2.record(10) 150 self.assertNotEqual(histogram1, histogram2) 151 152 def test_copy(self): 153 histogram1 = Histogram(LinearBucket(0, 0.2, 50)) 154 histogram1.record(0, 1, 2, 3, 4) 155 histogram2 = histogram1.copy() 156 self.assertEqual(histogram1, histogram2) 157 158 histogram1.record(5, 6, 7, 8, 9) 159 self.assertNotEqual(histogram1, histogram2) 160 161 def test_combine(self): 162 histogram1 = Histogram(LinearBucket(0, 0.2, 50)) 163 histogram1.record(0, 1, 2, 3, 4) 164 histogram2 = Histogram(LinearBucket(0, 0.2, 50)) 165 histogram2.record(5, 6, 7, 8, 9) 166 167 histogram = Histogram(LinearBucket(0, 0.2, 50)) 168 histogram.record(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) 169 170 self.assertEqual(histogram1.combine(histogram2), histogram) 171 172 173 if __name__ == '__main__': 174 unittest.main()