1 /* Copyright 2017 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 
16 #include "tensorflow/core/lib/monitoring/gauge.h"
17 
18 #include "tensorflow/core/platform/test.h"
19 
20 namespace tensorflow {
21 namespace monitoring {
22 namespace {
23 
24 auto* gauge_with_labels = Gauge<int64_t, 1>::New(
25     "/tensorflow/test/gauge_with_labels", "Gauge with one label.", "MyLabel");
26 
TEST(LabeledGaugeTest,InitializedWithZero)27 TEST(LabeledGaugeTest, InitializedWithZero) {
28   EXPECT_EQ(0, gauge_with_labels->GetCell("Empty")->value());
29 }
30 
TEST(LabeledGaugeTest,GetCell)31 TEST(LabeledGaugeTest, GetCell) {
32   auto* cell = gauge_with_labels->GetCell("GetCellOp");
33   EXPECT_EQ(0, cell->value());
34 
35   cell->Set(1);
36   EXPECT_EQ(1, cell->value());
37 
38   auto* same_cell = gauge_with_labels->GetCell("GetCellOp");
39   EXPECT_EQ(1, same_cell->value());
40 
41   same_cell->Set(10);
42   EXPECT_EQ(10, cell->value());
43   EXPECT_EQ(10, same_cell->value());
44 }
45 
46 auto* gauge_without_labels = Gauge<int64_t, 0>::New(
47     "/tensorflow/test/gauge_without_labels", "Gauge without any labels.");
48 
TEST(UnlabeledGaugeTest,InitializedWithZero)49 TEST(UnlabeledGaugeTest, InitializedWithZero) {
50   EXPECT_EQ(0, gauge_without_labels->GetCell()->value());
51 }
52 
TEST(UnlabeledGaugeTest,GetCell)53 TEST(UnlabeledGaugeTest, GetCell) {
54   auto* cell = gauge_without_labels->GetCell();
55   EXPECT_EQ(0, cell->value());
56 
57   cell->Set(1);
58   EXPECT_EQ(1, cell->value());
59 
60   auto* same_cell = gauge_without_labels->GetCell();
61   EXPECT_EQ(1, same_cell->value());
62 
63   same_cell->Set(10);
64   EXPECT_EQ(10, cell->value());
65   EXPECT_EQ(10, same_cell->value());
66 }
67 
68 auto* string_gauge = Gauge<string, 0>::New("/tensorflow/test/string_gauge",
69                                            "Gauge of string value.");
70 
TEST(GaugeOfStringValue,InitializedWithEmptyString)71 TEST(GaugeOfStringValue, InitializedWithEmptyString) {
72   EXPECT_EQ("", string_gauge->GetCell()->value());
73 }
74 
TEST(GaugeOfStringValue,GetCell)75 TEST(GaugeOfStringValue, GetCell) {
76   auto* cell = string_gauge->GetCell();
77   EXPECT_EQ("", cell->value());
78 
79   cell->Set("foo");
80   EXPECT_EQ("foo", cell->value());
81 
82   auto* same_cell = string_gauge->GetCell();
83   EXPECT_EQ("foo", cell->value());
84 
85   same_cell->Set("bar");
86   EXPECT_EQ("bar", cell->value());
87   EXPECT_EQ("bar", same_cell->value());
88 }
89 
90 auto* bool_gauge =
91     Gauge<bool, 0>::New("/tensorflow/test/bool_gauge", "Gauge of bool value.");
92 
TEST(GaugeOfBoolValue,InitializedWithFalseValue)93 TEST(GaugeOfBoolValue, InitializedWithFalseValue) {
94   EXPECT_EQ(false, bool_gauge->GetCell()->value());
95 }
96 
TEST(GaugeOfBoolValue,GetCell)97 TEST(GaugeOfBoolValue, GetCell) {
98   auto* cell = bool_gauge->GetCell();
99   EXPECT_EQ(false, cell->value());
100 
101   cell->Set(true);
102   EXPECT_EQ(true, cell->value());
103 
104   auto* same_cell = bool_gauge->GetCell();
105   EXPECT_EQ(true, cell->value());
106 
107   same_cell->Set(false);
108   EXPECT_EQ(false, cell->value());
109   EXPECT_EQ(false, same_cell->value());
110 }
111 
TEST(LabeledGaugeTest,SameName)112 TEST(LabeledGaugeTest, SameName) {
113   auto* same_gauge = Gauge<int64_t, 1>::New(
114       "/tensorflow/test/gauge_with_labels", "Gauge with one label.", "MyLabel");
115   EXPECT_TRUE(gauge_with_labels->GetStatus().ok());
116   EXPECT_FALSE(same_gauge->GetStatus().ok());
117   delete same_gauge;
118 }
119 
120 }  // namespace
121 }  // namespace monitoring
122 }  // namespace tensorflow
123