xref: /aosp_15_r20/external/tensorflow/tensorflow/core/util/reporter.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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/util/reporter.h"
17 
18 #include "tensorflow/core/platform/errors.h"
19 #include "tensorflow/core/platform/mutex.h"
20 #include "tensorflow/core/platform/str_util.h"
21 
22 namespace tensorflow {
23 
TestReportFile(const string & fname,const string & test_name)24 TestReportFile::TestReportFile(const string& fname, const string& test_name)
25     : closed_(true), fname_(fname), test_name_(test_name) {}
26 
Append(const string & content)27 Status TestReportFile::Append(const string& content) {
28   if (closed_) return OkStatus();
29   return log_file_->Append(content);
30 }
31 
Close()32 Status TestReportFile::Close() {
33   if (closed_) return OkStatus();
34   closed_ = true;
35   return log_file_->Close();
36 }
37 
Initialize()38 Status TestReportFile::Initialize() {
39   if (fname_.empty()) {
40     return OkStatus();
41   }
42   string mangled_fname = strings::StrCat(
43       fname_, absl::StrJoin(str_util::Split(test_name_, '/'), "__"));
44   Env* env = Env::Default();
45   if (env->FileExists(mangled_fname).ok()) {
46     return errors::InvalidArgument(
47         "Cannot create TestReportFile, file exists: ", mangled_fname);
48   }
49   TF_RETURN_IF_ERROR(env->NewWritableFile(mangled_fname, &log_file_));
50   TF_RETURN_IF_ERROR(log_file_->Flush());
51 
52   closed_ = false;
53   return OkStatus();
54 }
55 
TestReporter(const string & fname,const string & test_name)56 TestReporter::TestReporter(const string& fname, const string& test_name)
57     : report_file_(fname, test_name) {
58   benchmark_entry_.set_name(test_name);
59 }
60 
Close()61 Status TestReporter::Close() {
62   if (report_file_.IsClosed()) return OkStatus();
63 
64   BenchmarkEntries entries;
65   *entries.add_entry() = benchmark_entry_;
66   TF_RETURN_IF_ERROR(report_file_.Append(entries.SerializeAsString()));
67   benchmark_entry_.Clear();
68 
69   return report_file_.Close();
70 }
71 
Benchmark(int64_t iters,double cpu_time,double wall_time,double throughput)72 Status TestReporter::Benchmark(int64_t iters, double cpu_time, double wall_time,
73                                double throughput) {
74   if (report_file_.IsClosed()) return OkStatus();
75   benchmark_entry_.set_iters(iters);
76   benchmark_entry_.set_cpu_time(cpu_time / iters);
77   benchmark_entry_.set_wall_time(wall_time / iters);
78   benchmark_entry_.set_throughput(throughput);
79   return OkStatus();
80 }
81 
SetProperty(const string & name,const string & value)82 Status TestReporter::SetProperty(const string& name, const string& value) {
83   if (report_file_.IsClosed()) return OkStatus();
84   (*benchmark_entry_.mutable_extras())[name].set_string_value(value);
85   return OkStatus();
86 }
87 
SetProperty(const string & name,double value)88 Status TestReporter::SetProperty(const string& name, double value) {
89   if (report_file_.IsClosed()) return OkStatus();
90   (*benchmark_entry_.mutable_extras())[name].set_double_value(value);
91   return OkStatus();
92 }
93 
AddMetric(const string & name,double value)94 Status TestReporter::AddMetric(const string& name, double value) {
95   if (report_file_.IsClosed()) return OkStatus();
96   auto* metric = benchmark_entry_.add_metrics();
97   metric->set_name(name);
98   metric->set_value(value);
99   return OkStatus();
100 }
101 
Initialize()102 Status TestReporter::Initialize() { return report_file_.Initialize(); }
103 
104 }  // namespace tensorflow
105