From dbfc3d5d364a61dcf8b2867aee6afd6dc387b34b Mon Sep 17 00:00:00 2001 From: Shashi Shekhar Date: Fri, 6 Apr 2018 16:58:18 -0700 Subject: [PATCH] Add methods to TestReporter to log extras for benchmarks. PiperOrigin-RevId: 191960433 --- tensorflow/core/util/reporter.cc | 12 ++++++++++++ tensorflow/core/util/reporter.h | 10 +++++++++- tensorflow/core/util/reporter_test.cc | 23 +++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/tensorflow/core/util/reporter.cc b/tensorflow/core/util/reporter.cc index ee38f81..a595c95 100644 --- a/tensorflow/core/util/reporter.cc +++ b/tensorflow/core/util/reporter.cc @@ -47,6 +47,18 @@ Status TestReporter::Benchmark(int64 iters, double cpu_time, double wall_time, return Status::OK(); } +Status TestReporter::SetProperty(const string& name, const string& value) { + if (closed_) return Status::OK(); + (*benchmark_entry_.mutable_extras())[name].set_string_value(value); + return Status::OK(); +} + +Status TestReporter::SetProperty(const string& name, double value) { + if (closed_) return Status::OK(); + (*benchmark_entry_.mutable_extras())[name].set_double_value(value); + return Status::OK(); +} + Status TestReporter::Initialize() { if (fname_.empty()) { return Status::OK(); diff --git a/tensorflow/core/util/reporter.h b/tensorflow/core/util/reporter.h index bcae122..e551e2e 100644 --- a/tensorflow/core/util/reporter.h +++ b/tensorflow/core/util/reporter.h @@ -34,11 +34,13 @@ namespace tensorflow { // // If this environment variable is not defined, no logging is performed. // -// The intended use is via the following 4 lines: +// The intended use is via the following lines: // // TestReporter reporter(test_name); // TF_CHECK_OK(reporter.Initialize())); // TF_CHECK_OK(reporter.Benchmark(iters, cpu_time, wall_time, throughput)); +// TF_CHECK_OK(reporter.SetProperty("some_string_property", "some_value"); +// TF_CHECK_OK(reporter.SetProperty("some_double_property", double_value); // TF_CHECK_OK(reporter.Close()); // // For example, if the environment variable @@ -75,6 +77,12 @@ class TestReporter { Status Benchmark(int64 iters, double cpu_time, double wall_time, double throughput); + // Set property on Benchmark to the given value. + Status SetProperty(const string& name, double value); + + // Set property on Benchmark to the given value. + Status SetProperty(const string& name, const string& value); + // TODO(b/32704451): Don't just ignore the ::tensorflow::Status object! ~TestReporter() { Close().IgnoreError(); } // Autoclose in destructor. diff --git a/tensorflow/core/util/reporter_test.cc b/tensorflow/core/util/reporter_test.cc index 90ea098..0972b86 100644 --- a/tensorflow/core/util/reporter_test.cc +++ b/tensorflow/core/util/reporter_test.cc @@ -115,5 +115,28 @@ TEST(TestReporter, Benchmark) { EXPECT_EQ(benchmark_entry.throughput(), 3.0); } +TEST(TestReporter, SetProperties) { + string fname = + strings::StrCat(testing::TmpDir(), "/test_reporter_benchmarks_"); + TestReporter test_reporter(fname, "b2/3/4"); + TF_EXPECT_OK(test_reporter.Initialize()); + TF_EXPECT_OK(test_reporter.SetProperty("string_prop", "abc")); + TF_EXPECT_OK(test_reporter.SetProperty("double_prop", 4.0)); + + TF_EXPECT_OK(test_reporter.Close()); + string expected_fname = strings::StrCat(fname, "b2__3__4"); + string read; + TF_EXPECT_OK(ReadFileToString(Env::Default(), expected_fname, &read)); + + BenchmarkEntries benchmark_entries; + ASSERT_TRUE(benchmark_entries.ParseFromString(read)); + ASSERT_EQ(1, benchmark_entries.entry_size()); + const BenchmarkEntry& benchmark_entry = benchmark_entries.entry(0); + const auto& extras = benchmark_entry.extras(); + ASSERT_EQ(2, extras.size()); + EXPECT_EQ("abc", extras.at("string_prop").string_value()); + EXPECT_EQ(4.0, extras.at("double_prop").double_value()); +} + } // namespace } // namespace tensorflow -- 2.7.4