Add a method to add metric to the benchmark entry.

PiperOrigin-RevId: 325728292
Change-Id: I1829091880d54a4e6692c0fd3d39b3f128a3a1a6
This commit is contained in:
Chao Mei 2020-08-09 19:33:03 -07:00 committed by TensorFlower Gardener
parent 5f75a84642
commit 4e7127d73f
3 changed files with 36 additions and 0 deletions

View File

@ -91,6 +91,14 @@ Status TestReporter::SetProperty(const string& name, double value) {
return Status::OK();
}
Status TestReporter::AddMetric(const string& name, double value) {
if (report_file_.IsClosed()) return Status::OK();
auto* metric = benchmark_entry_.add_metrics();
metric->set_name(name);
metric->set_value(value);
return Status::OK();
}
Status TestReporter::Initialize() { return report_file_.Initialize(); }
} // namespace tensorflow

View File

@ -111,6 +111,9 @@ class TestReporter {
// Set property on Benchmark to the given value.
Status SetProperty(const string& name, const string& value);
// Add the given value to the metrics on the Benchmark.
Status AddMetric(const string& name, double value);
// TODO(b/32704451): Don't just ignore the ::tensorflow::Status object!
~TestReporter() { Close().IgnoreError(); } // Autoclose in destructor.

View File

@ -138,5 +138,30 @@ TEST(TestReporter, SetProperties) {
EXPECT_EQ(4.0, extras.at("double_prop").double_value());
}
TEST(TestReporter, AddMetrics) {
string fname =
strings::StrCat(testing::TmpDir(), "/test_reporter_benchmarks_");
TestReporter test_reporter(fname, "b3/4/5");
TF_EXPECT_OK(test_reporter.Initialize());
TF_EXPECT_OK(test_reporter.AddMetric("metric1", 2.0));
TF_EXPECT_OK(test_reporter.AddMetric("metric2", 3.0));
TF_EXPECT_OK(test_reporter.Close());
string expected_fname = strings::StrCat(fname, "b3__4__5");
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& metrics = benchmark_entry.metrics();
ASSERT_EQ(2, metrics.size());
EXPECT_EQ("metric1", metrics.at(0).name());
EXPECT_EQ(2.0, metrics.at(0).value());
EXPECT_EQ("metric2", metrics.at(1).name());
EXPECT_EQ(3.0, metrics.at(1).value());
}
} // namespace
} // namespace tensorflow