github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/metrics/execution_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 # pytype: skip-file 19 20 import unittest 21 22 from apache_beam.metrics.execution import MetricKey 23 from apache_beam.metrics.execution import MetricsContainer 24 from apache_beam.metrics.metricbase import MetricName 25 26 27 class TestMetricKey(unittest.TestCase): 28 def test_equality_for_key_with_labels(self): 29 test_labels = {'label1', 'value1'} 30 test_object = MetricKey( 31 'step', MetricName('namespace', 'name'), labels=test_labels) 32 same_labels = MetricKey( 33 'step', MetricName('namespace', 'name'), labels={'label1', 'value1'}) 34 same_label_reference = MetricKey( 35 'step', MetricName('namespace', 'name'), labels=test_labels) 36 self.assertEqual(test_object, same_labels) 37 self.assertEqual(test_object, same_label_reference) 38 self.assertEqual(hash(test_object), hash(same_labels)) 39 self.assertEqual(hash(test_object), hash(same_label_reference)) 40 41 def test_inequality_for_key_with_labels(self): 42 test_labels = {'label1', 'value1'} 43 test_object = MetricKey( 44 'step', MetricName('namespace', 'name'), labels=test_labels) 45 no_labels = MetricKey('step', MetricName('namespace', 'name')) 46 diff_label_key = MetricKey( 47 'step', MetricName('namespace', 'name'), labels={'l1_diff', 'value1'}) 48 diff_label_value = MetricKey( 49 'step', MetricName('namespace', 'name'), labels={'label1', 'v1_diff'}) 50 self.assertNotEqual(test_object, no_labels) 51 self.assertNotEqual(test_object, diff_label_key) 52 self.assertNotEqual(test_object, diff_label_value) 53 self.assertNotEqual(hash(test_object), hash(no_labels)) 54 self.assertNotEqual(hash(test_object), hash(diff_label_key)) 55 self.assertNotEqual(hash(test_object), hash(diff_label_value)) 56 57 def test_equality_for_key_with_no_labels(self): 58 test_object = MetricKey('step', MetricName('namespace', 'name')) 59 same = MetricKey('step', MetricName('namespace', 'name')) 60 self.assertEqual(test_object, same) 61 self.assertEqual(hash(test_object), hash(same)) 62 63 diff_step = MetricKey('step_diff', MetricName('namespace', 'name')) 64 diff_namespace = MetricKey('step', MetricName('namespace_diff', 'name')) 65 diff_name = MetricKey('step', MetricName('namespace', 'name_diff')) 66 self.assertNotEqual(test_object, diff_step) 67 self.assertNotEqual(test_object, diff_namespace) 68 self.assertNotEqual(test_object, diff_name) 69 self.assertNotEqual(hash(test_object), hash(diff_step)) 70 self.assertNotEqual(hash(test_object), hash(diff_namespace)) 71 self.assertNotEqual(hash(test_object), hash(diff_name)) 72 73 74 class TestMetricsContainer(unittest.TestCase): 75 def test_add_to_counter(self): 76 mc = MetricsContainer('astep') 77 counter = mc.get_counter(MetricName('namespace', 'name')) 78 counter.inc() 79 counter = mc.get_counter(MetricName('namespace', 'name')) 80 self.assertEqual(counter.value, 1) 81 82 def test_get_cumulative_or_updates(self): 83 mc = MetricsContainer('astep') 84 85 all_values = [] 86 for i in range(1, 11): 87 counter = mc.get_counter(MetricName('namespace', 'name{}'.format(i))) 88 distribution = mc.get_distribution( 89 MetricName('namespace', 'name{}'.format(i))) 90 gauge = mc.get_gauge(MetricName('namespace', 'name{}'.format(i))) 91 92 counter.inc(i) 93 distribution.update(i) 94 gauge.set(i) 95 all_values.append(i) 96 97 # Retrieve ALL updates. 98 cumulative = mc.get_cumulative() 99 self.assertEqual(len(cumulative.counters), 10) 100 self.assertEqual(len(cumulative.distributions), 10) 101 self.assertEqual(len(cumulative.gauges), 10) 102 103 self.assertEqual( 104 set(all_values), {v 105 for _, v in cumulative.counters.items()}) 106 self.assertEqual( 107 set(all_values), {v.value 108 for _, v in cumulative.gauges.items()}) 109 110 111 if __name__ == '__main__': 112 unittest.main()