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 #ifndef TENSORFLOW_CORE_UTIL_REPORTER_H_ 17 #define TENSORFLOW_CORE_UTIL_REPORTER_H_ 18 19 #include <cstdlib> 20 #include <memory> 21 #include <string> 22 #include <unordered_set> 23 24 #include "tensorflow/core/platform/env.h" 25 #include "tensorflow/core/platform/macros.h" 26 #include "tensorflow/core/platform/mutex.h" 27 #include "tensorflow/core/platform/types.h" 28 #include "tensorflow/core/util/test_log.pb.h" 29 30 namespace tensorflow { 31 32 // The TestReporter writes test / benchmark output to binary Protobuf files when 33 // the environment variable "TEST_REPORT_FILE_PREFIX" is defined. 34 // 35 // If this environment variable is not defined, no logging is performed. 36 // 37 // The intended use is via the following lines: 38 // 39 // TestReporter reporter(test_name); 40 // TF_CHECK_OK(reporter.Initialize())); 41 // TF_CHECK_OK(reporter.Benchmark(iters, cpu_time, wall_time, throughput)); 42 // TF_CHECK_OK(reporter.SetProperty("some_string_property", "some_value"); 43 // TF_CHECK_OK(reporter.SetProperty("some_double_property", double_value); 44 // TF_CHECK_OK(reporter.Close()); 45 // 46 // For example, if the environment variable 47 // TEST_REPORT_FILE_PREFIX="/tmp/run_" 48 // is set, and test_name is "BM_Foo/1/2", then a BenchmarkEntries pb 49 // with a single entry is written to file: 50 // /tmp/run_BM_Foo__1__2 51 // 52 class TestReporter { 53 public: 54 static constexpr const char* kTestReporterEnv = "TEST_REPORT_FILE_PREFIX"; 55 56 // Create a TestReporter with the test name 'test_name'. TestReporter(const string & test_name)57 explicit TestReporter(const string& test_name) 58 : TestReporter(GetLogEnv(), test_name) {} 59 60 // Provide a prefix filename, mostly used for testing this class. 61 TestReporter(const string& fname, const string& test_name); 62 63 // Initialize the TestReporter. If the reporting env flag is set, 64 // try to create the reporting file. Fails if the file already exists. 65 Status Initialize(); 66 67 // Finalize the report. If the reporting env flag is set, 68 // flush the reporting file and close it. 69 // Once Close is called, no other methods should be called other 70 // than Close and the destructor. 71 Status Close(); 72 73 // Set the report to be a Benchmark and log the given parameters. 74 // Only does something if the reporting env flag is set. 75 // Does not guarantee the report is written. Use Close() to 76 // enforce I/O operations. 77 Status Benchmark(int64 iters, double cpu_time, double wall_time, 78 double throughput); 79 80 // Set property on Benchmark to the given value. 81 Status SetProperty(const string& name, double value); 82 83 // Set property on Benchmark to the given value. 84 Status SetProperty(const string& name, const string& value); 85 86 // TODO(b/32704451): Don't just ignore the ::tensorflow::Status object! ~TestReporter()87 ~TestReporter() { Close().IgnoreError(); } // Autoclose in destructor. 88 89 private: GetLogEnv()90 static string GetLogEnv() { 91 const char* fname_ptr = getenv(kTestReporterEnv); 92 return (fname_ptr != nullptr) ? fname_ptr : ""; 93 } 94 bool closed_; 95 string fname_; 96 string test_name_; 97 std::unique_ptr<WritableFile> log_file_; 98 BenchmarkEntry benchmark_entry_; 99 TF_DISALLOW_COPY_AND_ASSIGN(TestReporter); 100 }; 101 102 } // namespace tensorflow 103 104 #endif // TENSORFLOW_CORE_UTIL_REPORTER_H_ 105