• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 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/sampler.h"
17 
18 #include "tensorflow/core/platform/test.h"
19 
20 namespace tensorflow {
21 namespace monitoring {
22 namespace {
23 
24 using histogram::Histogram;
25 
EqHistograms(const Histogram & expected,const HistogramProto & actual_proto)26 void EqHistograms(const Histogram& expected,
27                   const HistogramProto& actual_proto) {
28   Histogram actual;
29   ASSERT_TRUE(actual.DecodeFromProto(actual_proto));
30 
31   EXPECT_EQ(expected.ToString(), actual.ToString());
32 }
33 
34 auto* sampler_with_labels =
35     Sampler<1>::New({"/tensorflow/test/sampler_with_labels",
36                      "Sampler with one label.", "MyLabel"},
37                     Buckets::Explicit({10.0, 20.0}));
38 
TEST(LabeledSamplerTest,InitializedEmpty)39 TEST(LabeledSamplerTest, InitializedEmpty) {
40   Histogram empty;
41   EqHistograms(empty, sampler_with_labels->GetCell("Empty")->value());
42 }
43 
TEST(LabeledSamplerTest,ExplicitBucketBoundaries)44 TEST(LabeledSamplerTest, ExplicitBucketBoundaries) {
45   // Sampler automatically adds DBL_MAX to the list of buckets.
46   Histogram expected({10.0, 20.0, DBL_MAX});
47   auto* cell = sampler_with_labels->GetCell("BucketBoundaries");
48   sampler_with_labels->GetCell("AddedToCheckPreviousCellValidity");
49   cell->Add(-1.0);
50   expected.Add(-1.0);
51   cell->Add(10.0);
52   expected.Add(10.0);
53   cell->Add(20.0);
54   expected.Add(20.0);
55   cell->Add(31.0);
56   expected.Add(31.0);
57 
58   EqHistograms(expected, cell->value());
59 }
60 
61 auto* init_sampler_without_labels =
62     Sampler<0>::New({"/tensorflow/test/init_sampler_without_labels",
63                      "Sampler without labels initialized as empty."},
64                     Buckets::Explicit(std::vector<double>{1.5, 2.8}));
65 
TEST(UnlabeledSamplerTest,InitializedEmpty)66 TEST(UnlabeledSamplerTest, InitializedEmpty) {
67   Histogram empty;
68   EqHistograms(empty, init_sampler_without_labels->GetCell()->value());
69 }
70 
71 auto* sampler_without_labels =
72     Sampler<0>::New({"/tensorflow/test/sampler_without_labels",
73                      "Sampler without labels initialized as empty."},
74                     Buckets::Explicit({1.5, 2.8}));
75 
TEST(UnlabeledSamplerTest,ExplicitBucketBoundaries)76 TEST(UnlabeledSamplerTest, ExplicitBucketBoundaries) {
77   // Sampler automatically adds DBL_MAX to the list of buckets.
78   Histogram expected({1.5, 2.8, DBL_MAX});
79   auto* cell = sampler_without_labels->GetCell();
80   cell->Add(-1.0);
81   expected.Add(-1.0);
82   cell->Add(2.0);
83   expected.Add(2.0);
84   cell->Add(31.0);
85   expected.Add(31.0);
86 
87   EqHistograms(expected, cell->value());
88 }
89 
90 auto* sampler_with_exponential =
91     Sampler<1>::New({"/tensorflow/test/sampler_with_exponential",
92                      "Sampler with exponential buckets.", "MyLabel"},
93                     // So limits are {1, 2, 4}.
94                     Buckets::Exponential(1, 2, 3));
95 
TEST(ExponentialSamplerTest,ExponentialBucketBoundaries)96 TEST(ExponentialSamplerTest, ExponentialBucketBoundaries) {
97   // Sampler automatically adds DBL_MAX to the list of buckets.
98   Histogram expected({1.0, 2.0, 4.0, DBL_MAX});
99   auto* cell = sampler_with_exponential->GetCell("BucketBoundaries");
100   sampler_with_exponential->GetCell("AddedToCheckPreviousCellValidity");
101   cell->Add(-1.0);
102   expected.Add(-1.0);
103   cell->Add(0.5);
104   expected.Add(0.5);
105   cell->Add(1.001);
106   expected.Add(1.001);
107   cell->Add(3.999);
108   expected.Add(3.999);
109   cell->Add(6.0);
110   expected.Add(6.0);
111 
112   EqHistograms(expected, cell->value());
113 }
114 
TEST(ExplicitSamplerTest,SameName)115 TEST(ExplicitSamplerTest, SameName) {
116   auto* same_sampler = Sampler<1>::New({"/tensorflow/test/sampler_with_labels",
117                                         "Sampler with one label.", "MyLabel"},
118                                        Buckets::Explicit({10.0, 20.0}));
119   EXPECT_TRUE(sampler_with_labels->GetStatus().ok());
120   EXPECT_FALSE(same_sampler->GetStatus().ok());
121   delete same_sampler;
122 }
123 
124 }  // namespace
125 }  // namespace monitoring
126 }  // namespace tensorflow
127