1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 3Licensed under the Apache License, Version 2.0 (the "License"); 4you may not use this file except in compliance with the License. 5You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9Unless required by applicable law or agreed to in writing, software 10distributed under the License is distributed on an "AS IS" BASIS, 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12See the License for the specific language governing permissions and 13limitations under the License. 14==============================================================================*/ 15 16#include "tensorflow/core/lib/histogram/histogram.h" 17#include <float.h> 18#include "tensorflow/core/framework/summary.pb.h" 19#include "tensorflow/core/platform/logging.h" 20#include "tensorflow/core/platform/test.h" 21 22namespace tensorflow { 23namespace histogram { 24 25static void Validate(const Histogram& h) { 26 string s1 = h.ToString(); 27 LOG(ERROR) << s1; 28 29 HistogramProto proto_with_zeroes; 30 h.EncodeToProto(&proto_with_zeroes, true); 31 Histogram h2; 32 EXPECT_TRUE(h2.DecodeFromProto(proto_with_zeroes)); 33 string s2 = h2.ToString(); 34 LOG(ERROR) << s2; 35 36 EXPECT_EQ(s1, s2); 37 38 HistogramProto proto_no_zeroes; 39 h.EncodeToProto(&proto_no_zeroes, false); 40 LOG(ERROR) << proto_no_zeroes.DebugString(); 41 Histogram h3; 42 EXPECT_TRUE(h3.DecodeFromProto(proto_no_zeroes)); 43 string s3 = h3.ToString(); 44 LOG(ERROR) << s3; 45 46 EXPECT_EQ(s1, s3); 47} 48 49TEST(Histogram, Empty) { 50 Histogram h; 51 Validate(h); 52} 53 54TEST(Histogram, SingleValue) { 55 Histogram h; 56 h.Add(-3.0); 57 Validate(h); 58} 59 60TEST(Histogram, CustomBuckets) { 61 Histogram h({-10, -5, 0, 5, 10, 100, 1000, 10000, DBL_MAX}); 62 h.Add(-3.0); 63 h.Add(4.99); 64 h.Add(5.0); 65 h.Add(1000.0); 66 Validate(h); 67} 68 69TEST(Histogram, Median) { 70 Histogram h({0, 10, 100, DBL_MAX}); 71 h.Add(-2); 72 h.Add(-2); 73 h.Add(0); 74 double median = h.Median(); 75 EXPECT_EQ(median, -0.5); 76} 77 78TEST(Histogram, Percentile) { 79 // 10%, 30%, 40%, 20% 80 Histogram h({1, 2, 3, 4}); 81 // 10% first bucket 82 h.Add(-1.0); 83 // 30% second bucket 84 h.Add(1.5); 85 h.Add(1.5); 86 h.Add(1.5); 87 // 40% third bucket 88 h.Add(2.5); 89 h.Add(2.5); 90 h.Add(2.5); 91 h.Add(2.5); 92 // 20% fourth bucket 93 h.Add(3.5); 94 h.Add(3.9); 95 96 EXPECT_EQ(h.Percentile(0), -1.0); // -1.0 = histo.min_ 97 EXPECT_EQ(h.Percentile(25), 1.5); // 1.5 = remap(25, 10, 40, 1, 2) 98 EXPECT_EQ(h.Percentile(50), 2.25); // 2.25 = remap(50, 40, 80, 2, 3) 99 EXPECT_EQ(h.Percentile(75), 2.875); // 2.875 = remap(75, 40, 80, 2, 3) 100 EXPECT_EQ(h.Percentile(90), 3.45); // 3.45 = remap(90, 80, 100, 3, 3.9) 101 EXPECT_EQ(h.Percentile(100), 3.9); // 3.9 = histo.max_ 102} 103 104TEST(Histogram, Basic) { 105 Histogram h; 106 for (int i = 0; i < 100; i++) { 107 h.Add(i); 108 } 109 for (int i = 1000; i < 100000; i += 1000) { 110 h.Add(i); 111 } 112 Validate(h); 113} 114 115TEST(ThreadSafeHistogram, Basic) { 116 // Fill a normal histogram. 117 Histogram h; 118 for (int i = 0; i < 100; i++) { 119 h.Add(i); 120 } 121 122 // Fill a thread-safe histogram with the same values. 123 ThreadSafeHistogram tsh; 124 for (int i = 0; i < 100; i++) { 125 tsh.Add(i); 126 } 127 128 for (int i = 0; i < 2; ++i) { 129 bool preserve_zero_buckets = (i == 0); 130 HistogramProto h_proto; 131 h.EncodeToProto(&h_proto, preserve_zero_buckets); 132 HistogramProto tsh_proto; 133 tsh.EncodeToProto(&tsh_proto, preserve_zero_buckets); 134 135 // Let's decode from the proto of the other histogram type. 136 Histogram h2; 137 EXPECT_TRUE(h2.DecodeFromProto(tsh_proto)); 138 ThreadSafeHistogram tsh2; 139 EXPECT_TRUE(tsh2.DecodeFromProto(h_proto)); 140 141 // Now let's reencode and check they match. 142 EXPECT_EQ(h2.ToString(), tsh2.ToString()); 143 } 144 145 EXPECT_EQ(h.Median(), tsh.Median()); 146 EXPECT_EQ(h.Percentile(40.0), tsh.Percentile(40.0)); 147 EXPECT_EQ(h.Average(), tsh.Average()); 148 EXPECT_EQ(h.StandardDeviation(), tsh.StandardDeviation()); 149 EXPECT_EQ(h.ToString(), tsh.ToString()); 150} 151 152} // namespace histogram 153} // namespace tensorflow 154