diff options
author | Vinu Rajashekhar <vinuraja@google.com> | 2016-08-03 17:23:14 -0800 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2016-08-03 18:33:36 -0700 |
commit | 44cd6a6b8983512e155f53db3bac13cdbdaeb90b (patch) | |
tree | 7fc1845868aa0c735594ace5550b409efb6831b9 /tensorflow/core/lib/monitoring | |
parent | 6e20d1e656e5b05b3be1d3813f07a40b31a2c620 (diff) |
Sets up static factory construction for the Counter class.
- So they can be statically initialized.
Change: 129283444
Diffstat (limited to 'tensorflow/core/lib/monitoring')
-rw-r--r-- | tensorflow/core/lib/monitoring/counter.h | 20 | ||||
-rw-r--r-- | tensorflow/core/lib/monitoring/counter_test.cc | 60 |
2 files changed, 43 insertions, 37 deletions
diff --git a/tensorflow/core/lib/monitoring/counter.h b/tensorflow/core/lib/monitoring/counter.h index 652a78acfd..0fcbe90ea8 100644 --- a/tensorflow/core/lib/monitoring/counter.h +++ b/tensorflow/core/lib/monitoring/counter.h @@ -79,11 +79,9 @@ class Counter { registration_handle_.reset(); } - explicit Counter( - const MetricDef<MetricKind::CUMULATIVE, int64, NumLabels>& metric_def) - : metric_def_(metric_def), - registration_handle_( - ExportRegistry::Default()->Register(&metric_def_)) {} + // Creates the metric based on the metric-definition. + static Counter* New( + const MetricDef<MetricKind::CUMULATIVE, int64, NumLabels>& metric_def); // Retrieves the cell for the specified labels, creating it on demand if // not already present. @@ -91,6 +89,12 @@ class Counter { CounterCell* GetCell(const Labels&... labels) LOCKS_EXCLUDED(mu_); private: + explicit Counter( + const MetricDef<MetricKind::CUMULATIVE, int64, NumLabels>& metric_def) + : metric_def_(metric_def), + registration_handle_( + ExportRegistry::Default()->Register(&metric_def_)) {} + mutable mutex mu_; // The metric definition. This will be used to identify the metric when we @@ -109,6 +113,12 @@ class Counter { // Implementation details follow. API readers may skip. //// +template <int NumLabels> +Counter<NumLabels>* Counter<NumLabels>::New( + const MetricDef<MetricKind::CUMULATIVE, int64, NumLabels>& metric_def) { + return new Counter<NumLabels>(metric_def); +} + inline void CounterCell::IncrementBy(const int64 step) { DCHECK_LE(0, step) << "Must not decrement cumulative metrics."; value_ += step; diff --git a/tensorflow/core/lib/monitoring/counter_test.cc b/tensorflow/core/lib/monitoring/counter_test.cc index d0d1b79c5b..2bf361a534 100644 --- a/tensorflow/core/lib/monitoring/counter_test.cc +++ b/tensorflow/core/lib/monitoring/counter_test.cc @@ -21,26 +21,22 @@ namespace tensorflow { namespace monitoring { namespace { -class LabeledCounterTest : public ::testing::Test { - protected: - LabeledCounterTest() {} +auto* counter_with_labels = + Counter<1>::New({"/tensorflow/test/counter_with_labels", + "Counter with one label.", "One label"}); - Counter<1> counter_with_labels_{{"/tensorflow/test/counter_with_labels", - "Counter with one label.", "One label"}}; -}; - -TEST_F(LabeledCounterTest, InitializedWithZero) { - EXPECT_EQ(0, counter_with_labels_.GetCell("Empty")->value()); +TEST(LabeledCounterTest, InitializedWithZero) { + EXPECT_EQ(0, counter_with_labels->GetCell("Empty")->value()); } -TEST_F(LabeledCounterTest, GetCell) { - auto* cell = counter_with_labels_.GetCell("GetCellOp"); +TEST(LabeledCounterTest, GetCell) { + auto* cell = counter_with_labels->GetCell("GetCellOp"); EXPECT_EQ(0, cell->value()); cell->IncrementBy(42); EXPECT_EQ(42, cell->value()); - auto* same_cell = counter_with_labels_.GetCell("GetCellOp"); + auto* same_cell = counter_with_labels->GetCell("GetCellOp"); EXPECT_EQ(42, same_cell->value()); same_cell->IncrementBy(58); @@ -48,34 +44,31 @@ TEST_F(LabeledCounterTest, GetCell) { EXPECT_EQ(100, same_cell->value()); } -using LabeledCounterDeathTest = LabeledCounterTest; - -TEST_F(LabeledCounterDeathTest, DiesOnDecrement) { +TEST(LabeledCounterDeathTest, DiesOnDecrement) { EXPECT_DEBUG_DEATH( - { counter_with_labels_.GetCell("DyingOp")->IncrementBy(-1); }, + { counter_with_labels->GetCell("DyingOp")->IncrementBy(-1); }, "decrement"); } -class UnlabeledCounterTest : public ::testing::Test { - protected: - UnlabeledCounterTest() {} - - Counter<0> counter_without_labels_{{"/tensorflow/test/counter_without_labels", - "Counter without any labels."}}; -}; +auto* init_counter_without_labels = Counter<0>::New( + {"/tensorflow/test/init_counter_without_labels", + "Counter without any labels to check if it is initialized as 0."}); -TEST_F(UnlabeledCounterTest, InitializedWithZero) { - EXPECT_EQ(0, counter_without_labels_.GetCell()->value()); +TEST(UnlabeledCounterTest, InitializedWithZero) { + EXPECT_EQ(0, init_counter_without_labels->GetCell()->value()); } -TEST_F(UnlabeledCounterTest, GetCell) { - auto* cell = counter_without_labels_.GetCell(); +auto* counter_without_labels = Counter<0>::New( + {"/tensorflow/test/counter_without_labels", "Counter without any labels."}); + +TEST(UnlabeledCounterTest, GetCell) { + auto* cell = counter_without_labels->GetCell(); EXPECT_EQ(0, cell->value()); cell->IncrementBy(42); EXPECT_EQ(42, cell->value()); - auto* same_cell = counter_without_labels_.GetCell(); + auto* same_cell = counter_without_labels->GetCell(); EXPECT_EQ(42, same_cell->value()); same_cell->IncrementBy(58); @@ -83,11 +76,14 @@ TEST_F(UnlabeledCounterTest, GetCell) { EXPECT_EQ(100, same_cell->value()); } -using UnlabeledCounterDeathTest = UnlabeledCounterTest; +auto* dead_counter_without_labels = Counter<0>::New( + {"/tensorflow/test/dead_counter_without_labels", + "Counter without any labels which goes on to die on decrement."}); -TEST_F(UnlabeledCounterDeathTest, DiesOnDecrement) { - EXPECT_DEBUG_DEATH({ counter_without_labels_.GetCell()->IncrementBy(-1); }, - "decrement"); +TEST(UnlabeledCounterDeathTest, DiesOnDecrement) { + EXPECT_DEBUG_DEATH( + { dead_counter_without_labels->GetCell()->IncrementBy(-1); }, + "decrement"); } } // namespace |