• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
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"""Tests for monitoring."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21import time
22
23from tensorflow.python.eager import monitoring
24from tensorflow.python.eager import test
25from tensorflow.python.framework import errors
26from tensorflow.python.framework import test_util
27
28
29class MonitoringTest(test_util.TensorFlowTestCase):
30
31  def test_counter(self):
32    counter = monitoring.Counter('test/counter', 'test counter')
33    counter.get_cell().increase_by(1)
34    self.assertEqual(counter.get_cell().value(), 1)
35    counter.get_cell().increase_by(5)
36    self.assertEqual(counter.get_cell().value(), 6)
37
38  def test_multiple_counters(self):
39    counter1 = monitoring.Counter('test/counter1', 'test counter', 'label1')
40    counter1.get_cell('foo').increase_by(1)
41    self.assertEqual(counter1.get_cell('foo').value(), 1)
42    counter2 = monitoring.Counter('test/counter2', 'test counter', 'label1',
43                                  'label2')
44    counter2.get_cell('foo', 'bar').increase_by(5)
45    self.assertEqual(counter2.get_cell('foo', 'bar').value(), 5)
46
47  def test_same_counter(self):
48    counter1 = monitoring.Counter('test/same_counter', 'test counter')  # pylint: disable=unused-variable
49    with self.assertRaises(errors.AlreadyExistsError):
50      counter2 = monitoring.Counter('test/same_counter', 'test counter')  # pylint: disable=unused-variable
51
52  def test_int_gauge(self):
53    gauge = monitoring.IntGauge('test/gauge', 'test gauge')
54    gauge.get_cell().set(1)
55    self.assertEqual(gauge.get_cell().value(), 1)
56    gauge.get_cell().set(5)
57    self.assertEqual(gauge.get_cell().value(), 5)
58
59    gauge1 = monitoring.IntGauge('test/gauge1', 'test gauge1', 'label1')
60    gauge1.get_cell('foo').set(2)
61    self.assertEqual(gauge1.get_cell('foo').value(), 2)
62
63  def test_string_gauge(self):
64    gauge = monitoring.StringGauge('test/gauge', 'test gauge')
65    gauge.get_cell().set('left')
66    self.assertEqual(gauge.get_cell().value(), 'left')
67    gauge.get_cell().set('right')
68    self.assertEqual(gauge.get_cell().value(), 'right')
69
70    gauge1 = monitoring.StringGauge('test/gauge1', 'test gauge1', 'label1')
71    gauge1.get_cell('foo').set('start')
72    self.assertEqual(gauge1.get_cell('foo').value(), 'start')
73
74  def test_bool_gauge(self):
75    gauge = monitoring.BoolGauge('test/gauge', 'test gauge')
76    gauge.get_cell().set(True)
77    self.assertTrue(gauge.get_cell().value())
78    gauge.get_cell().set(False)
79    self.assertFalse(gauge.get_cell().value())
80
81    gauge1 = monitoring.BoolGauge('test/gauge1', 'test gauge1', 'label1')
82    gauge1.get_cell('foo').set(True)
83    self.assertTrue(gauge1.get_cell('foo').value())
84
85  def test_sampler(self):
86    buckets = monitoring.ExponentialBuckets(1.0, 2.0, 2)
87    sampler = monitoring.Sampler('test/sampler', buckets, 'test sampler')
88    sampler.get_cell().add(1.0)
89    sampler.get_cell().add(5.0)
90    histogram_proto = sampler.get_cell().value()
91    self.assertEqual(histogram_proto.min, 1.0)
92    self.assertEqual(histogram_proto.num, 2.0)
93    self.assertEqual(histogram_proto.sum, 6.0)
94
95    sampler1 = monitoring.Sampler('test/sampler1', buckets, 'test sampler',
96                                  'label1')
97    sampler1.get_cell('foo').add(2.0)
98    sampler1.get_cell('foo').add(4.0)
99    sampler1.get_cell('bar').add(8.0)
100    histogram_proto1 = sampler1.get_cell('foo').value()
101    self.assertEqual(histogram_proto1.max, 4.0)
102    self.assertEqual(histogram_proto1.num, 2.0)
103    self.assertEqual(histogram_proto1.sum, 6.0)
104
105  def test_context_manager(self):
106    counter = monitoring.Counter('test/ctxmgr', 'test context manager', 'slot')
107    with monitoring.MonitoredTimer(counter.get_cell('long')):
108      time.sleep(0.01)
109      with monitoring.MonitoredTimer(counter.get_cell('short')):
110        time.sleep(0.01)
111    self.assertGreater(
112        counter.get_cell('long').value(),
113        counter.get_cell('short').value())
114
115  def test_function_decorator(self):
116    counter = monitoring.Counter('test/funcdecorator', 'test func decorator')
117
118    @monitoring.monitored_timer(counter.get_cell())
119    def timed_function(seconds):
120      time.sleep(seconds)
121
122    timed_function(0.001)
123    self.assertGreater(counter.get_cell().value(), 1000)
124
125
126if __name__ == '__main__':
127  test.main()
128