• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 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 #define _XOPEN_SOURCE  // for setenv, unsetenv
17 #include <cstdlib>
18 
19 #include "tensorflow/core/util/reporter.h"
20 
21 #include "tensorflow/core/lib/core/status_test_util.h"
22 #include "tensorflow/core/lib/strings/str_util.h"
23 #include "tensorflow/core/lib/strings/strcat.h"
24 #include "tensorflow/core/platform/protobuf.h"
25 #include "tensorflow/core/platform/test.h"
26 
27 namespace tensorflow {
28 namespace {
29 
30 // Tests of all the error paths in log_reader.cc follow:
ExpectHasSubstr(StringPiece s,StringPiece expected)31 static void ExpectHasSubstr(StringPiece s, StringPiece expected) {
32   EXPECT_TRUE(str_util::StrContains(s, expected))
33       << s << " does not contain " << expected;
34 }
35 
TEST(TestReporter,NoLogging)36 TEST(TestReporter, NoLogging) {
37   TestReporter test_reporter("b1");
38   TF_EXPECT_OK(test_reporter.Initialize());
39   TF_EXPECT_OK(test_reporter.Close());
40 }
41 
TEST(TestReporter,UsesEnv)42 TEST(TestReporter, UsesEnv) {
43   const char* old_env = std::getenv(TestReporter::kTestReporterEnv);
44 
45   // Set a file we can't possibly create, check for failure
46   setenv(TestReporter::kTestReporterEnv, "/cant/find/me:!", 1);
47   CHECK_EQ(string(std::getenv(TestReporter::kTestReporterEnv)),
48            string("/cant/find/me:!"));
49   TestReporter test_reporter("b1");
50   Status s = test_reporter.Initialize();
51   ExpectHasSubstr(s.ToString(), "/cant/find/me");
52 
53   // Remove the env variable, no logging is performed
54   unsetenv(TestReporter::kTestReporterEnv);
55   CHECK_EQ(std::getenv(TestReporter::kTestReporterEnv), nullptr);
56   TestReporter test_reporter_empty("b1");
57   s = test_reporter_empty.Initialize();
58   TF_EXPECT_OK(s);
59   s = test_reporter_empty.Close();
60   TF_EXPECT_OK(s);
61 
62   if (old_env == nullptr) {
63     unsetenv(TestReporter::kTestReporterEnv);
64   } else {
65     setenv(TestReporter::kTestReporterEnv, old_env, 1);
66   }
67 }
68 
TEST(TestReporter,CreateTwiceFails)69 TEST(TestReporter, CreateTwiceFails) {
70   {
71     TestReporter test_reporter(
72         strings::StrCat(testing::TmpDir(), "/test_reporter_dupe"), "t1");
73     TF_EXPECT_OK(test_reporter.Initialize());
74   }
75   {
76     TestReporter test_reporter(
77         strings::StrCat(testing::TmpDir(), "/test_reporter_dupe"), "t1");
78     Status s = test_reporter.Initialize();
79     ExpectHasSubstr(s.ToString(), "file exists:");
80   }
81 }
82 
TEST(TestReporter,CreateCloseCreateAgainSkipsSecond)83 TEST(TestReporter, CreateCloseCreateAgainSkipsSecond) {
84   TestReporter test_reporter(
85       strings::StrCat(testing::TmpDir(), "/test_reporter_create_close"), "t1");
86   TF_EXPECT_OK(test_reporter.Initialize());
87   TF_EXPECT_OK(test_reporter.Close());
88   TF_EXPECT_OK(test_reporter.Benchmark(1, 1.0, 2.0, 3.0));  // No-op, closed
89   TF_EXPECT_OK(test_reporter.Close());                      // No-op, closed
90   Status s = test_reporter.Initialize();  // Try to reinitialize
91   ExpectHasSubstr(s.ToString(), "file exists:");
92 }
93 
TEST(TestReporter,Benchmark)94 TEST(TestReporter, Benchmark) {
95   string fname =
96       strings::StrCat(testing::TmpDir(), "/test_reporter_benchmarks_");
97   TestReporter test_reporter(fname, "b1/2/3");
98   TF_EXPECT_OK(test_reporter.Initialize());
99   TF_EXPECT_OK(test_reporter.Benchmark(1, 1.0, 2.0, 3.0));
100   TF_EXPECT_OK(test_reporter.Close());
101 
102   string expected_fname = strings::StrCat(fname, "b1__2__3");
103   string read;
104   TF_EXPECT_OK(ReadFileToString(Env::Default(), expected_fname, &read));
105 
106   BenchmarkEntries benchmark_entries;
107   ASSERT_TRUE(benchmark_entries.ParseFromString(read));
108   ASSERT_EQ(1, benchmark_entries.entry_size());
109   const BenchmarkEntry& benchmark_entry = benchmark_entries.entry(0);
110 
111   EXPECT_EQ(benchmark_entry.name(), "b1/2/3");
112   EXPECT_EQ(benchmark_entry.iters(), 1);
113   EXPECT_EQ(benchmark_entry.cpu_time(), 1.0);
114   EXPECT_EQ(benchmark_entry.wall_time(), 2.0);
115   EXPECT_EQ(benchmark_entry.throughput(), 3.0);
116 }
117 
TEST(TestReporter,SetProperties)118 TEST(TestReporter, SetProperties) {
119   string fname =
120       strings::StrCat(testing::TmpDir(), "/test_reporter_benchmarks_");
121   TestReporter test_reporter(fname, "b2/3/4");
122   TF_EXPECT_OK(test_reporter.Initialize());
123   TF_EXPECT_OK(test_reporter.SetProperty("string_prop", "abc"));
124   TF_EXPECT_OK(test_reporter.SetProperty("double_prop", 4.0));
125 
126   TF_EXPECT_OK(test_reporter.Close());
127   string expected_fname = strings::StrCat(fname, "b2__3__4");
128   string read;
129   TF_EXPECT_OK(ReadFileToString(Env::Default(), expected_fname, &read));
130 
131   BenchmarkEntries benchmark_entries;
132   ASSERT_TRUE(benchmark_entries.ParseFromString(read));
133   ASSERT_EQ(1, benchmark_entries.entry_size());
134   const BenchmarkEntry& benchmark_entry = benchmark_entries.entry(0);
135   const auto& extras = benchmark_entry.extras();
136   ASSERT_EQ(2, extras.size());
137   EXPECT_EQ("abc", extras.at("string_prop").string_value());
138   EXPECT_EQ(4.0, extras.at("double_prop").double_value());
139 }
140 
141 }  // namespace
142 }  // namespace tensorflow
143