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/tsl/util/reporter.h" |
17 | |
18 | #include "tensorflow/tsl/platform/errors.h" |
19 | #include "tensorflow/tsl/platform/mutex.h" |
20 | #include "tensorflow/tsl/platform/str_util.h" |
21 | |
22 | namespace tsl { |
23 | |
24 | TestReportFile::TestReportFile(const string& fname, const string& test_name) |
25 | : closed_(true), fname_(fname), test_name_(test_name) {} |
26 | |
27 | Status TestReportFile::Append(const string& content) { |
28 | if (closed_) return OkStatus(); |
29 | return log_file_->Append(content); |
30 | } |
31 | |
32 | Status TestReportFile::Close() { |
33 | if (closed_) return OkStatus(); |
34 | closed_ = true; |
35 | return log_file_->Close(); |
36 | } |
37 | |
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 | |
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 | |
61 | Status TestReporter::Close() { |
62 | if (report_file_.IsClosed()) return OkStatus(); |
63 | |
64 | tensorflow::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 | |
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 | |
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 | |
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 | |
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 | |
102 | Status TestReporter::Initialize() { return report_file_.Initialize(); } |
103 | |
104 | } // namespace tsl |
105 | |