-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmonitoring_test.py
161 lines (136 loc) · 6.25 KB
/
monitoring_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed 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.
# ==============================================================================
"""Tests for monitoring."""
import time
from tensorflow.python.eager import monitoring
from tensorflow.python.eager import test
from tensorflow.python.framework import errors
from tensorflow.python.framework import test_util
class MonitoringTest(test_util.TensorFlowTestCase):
def test_counter(self):
counter = monitoring.Counter('test/counter', 'test counter')
counter.get_cell().increase_by(1)
self.assertEqual(counter.get_cell().value(), 1)
counter.get_cell().increase_by(5)
self.assertEqual(counter.get_cell().value(), 6)
def test_multiple_counters(self):
counter1 = monitoring.Counter('test/counter1', 'test counter', 'label1')
counter1.get_cell('foo').increase_by(1)
self.assertEqual(counter1.get_cell('foo').value(), 1)
counter2 = monitoring.Counter('test/counter2', 'test counter', 'label1',
'label2')
counter2.get_cell('foo', 'bar').increase_by(5)
self.assertEqual(counter2.get_cell('foo', 'bar').value(), 5)
def test_same_counter(self):
counter1 = monitoring.Counter('test/same_counter', 'test counter') # pylint: disable=unused-variable
with self.assertRaises(errors.AlreadyExistsError):
counter2 = monitoring.Counter('test/same_counter', 'test counter') # pylint: disable=unused-variable
def test_int_gauge(self):
gauge = monitoring.IntGauge('test/gauge', 'test gauge')
gauge.get_cell().set(1)
self.assertEqual(gauge.get_cell().value(), 1)
gauge.get_cell().set(5)
self.assertEqual(gauge.get_cell().value(), 5)
gauge1 = monitoring.IntGauge('test/gauge1', 'test gauge1', 'label1')
gauge1.get_cell('foo').set(2)
self.assertEqual(gauge1.get_cell('foo').value(), 2)
def test_string_gauge(self):
gauge = monitoring.StringGauge('test/gauge', 'test gauge')
gauge.get_cell().set('left')
self.assertEqual(gauge.get_cell().value(), 'left')
gauge.get_cell().set('right')
self.assertEqual(gauge.get_cell().value(), 'right')
gauge1 = monitoring.StringGauge('test/gauge1', 'test gauge1', 'label1')
gauge1.get_cell('foo').set('start')
self.assertEqual(gauge1.get_cell('foo').value(), 'start')
def test_bool_gauge(self):
gauge = monitoring.BoolGauge('test/gauge', 'test gauge')
gauge.get_cell().set(True)
self.assertTrue(gauge.get_cell().value())
gauge.get_cell().set(False)
self.assertFalse(gauge.get_cell().value())
gauge1 = monitoring.BoolGauge('test/gauge1', 'test gauge1', 'label1')
gauge1.get_cell('foo').set(True)
self.assertTrue(gauge1.get_cell('foo').value())
def test_sampler(self):
buckets = monitoring.ExponentialBuckets(1.0, 2.0, 2)
sampler = monitoring.Sampler('test/sampler', buckets, 'test sampler')
sampler.get_cell().add(1.0)
sampler.get_cell().add(5.0)
histogram_proto = sampler.get_cell().value()
self.assertEqual(histogram_proto.min, 1.0)
self.assertEqual(histogram_proto.num, 2.0)
self.assertEqual(histogram_proto.sum, 6.0)
sampler1 = monitoring.Sampler('test/sampler1', buckets, 'test sampler',
'label1')
sampler1.get_cell('foo').add(2.0)
sampler1.get_cell('foo').add(4.0)
sampler1.get_cell('bar').add(8.0)
histogram_proto1 = sampler1.get_cell('foo').value()
self.assertEqual(histogram_proto1.max, 4.0)
self.assertEqual(histogram_proto1.num, 2.0)
self.assertEqual(histogram_proto1.sum, 6.0)
def test_context_manager(self):
counter = monitoring.Counter('test/ctxmgr', 'test context manager', 'slot')
with monitoring.MonitoredTimer(counter.get_cell('long')):
time.sleep(0.01)
with monitoring.MonitoredTimer(counter.get_cell('short')):
time.sleep(0.01)
self.assertGreater(
counter.get_cell('long').value(), counter.get_cell('short').value()
)
def test_monitored_timer_tracker(self):
counter = monitoring.Counter('test/ctxmgr', 'test context manager', 'slot')
counter2 = monitoring.Counter('test/ctxmgr2', 'slot')
with monitoring.MonitoredTimer(counter.get_cell('long'), 'counter'):
time.sleep(0.01)
self.assertIn('counter', monitoring.MonitoredTimerSections)
with monitoring.MonitoredTimer(counter2.get_cell(), 'counter2'):
time.sleep(0.01)
self.assertIn('counter', monitoring.MonitoredTimerSections)
self.assertIn('counter2', monitoring.MonitoredTimerSections)
with monitoring.MonitoredTimer(counter.get_cell('long'), 'counter'):
time.sleep(0.01)
self.assertNotIn('counter2', monitoring.MonitoredTimerSections)
self.assertGreater(
counter.get_cell('long').value(), counter.get_cell('short').value()
)
self.assertGreater(counter2.get_cell().value(), 0)
def test_repetitive_monitored_timer(self):
counter = monitoring.Counter('test/ctxmgr', 'test context manager')
with monitoring.MonitoredTimer(
counter.get_cell(),
monitored_section_name='action1',
avoid_repetitive_counting=True,
):
time.sleep(1)
with monitoring.MonitoredTimer(
counter.get_cell(),
monitored_section_name='action1',
avoid_repetitive_counting=True,
):
time.sleep(1)
# The inner section is not timed.
self.assertEqual(counter.get_cell().value(), 0)
self.assertGreater(counter.get_cell().value(), 0)
def test_function_decorator(self):
counter = monitoring.Counter('test/funcdecorator', 'test func decorator')
@monitoring.monitored_timer(counter.get_cell())
def timed_function(seconds):
time.sleep(seconds)
timed_function(0.001)
self.assertGreater(counter.get_cell().value(), 1000)
if __name__ == '__main__':
test.main()