You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bm...@apache.org on 2014/05/02 00:59:57 UTC

[2/3] git commit: Used Duration subclasses to allow Timer users to specify units.

Used Duration subclasses to allow Timer users to specify units.

Review: https://reviews.apache.org/r/20923


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/88c235a8
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/88c235a8
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/88c235a8

Branch: refs/heads/master
Commit: 88c235a842c335f0f904d57798808106803faf23
Parents: 00f7091
Author: Dominic Hamon <dh...@twopensource.com>
Authored: Thu May 1 15:55:59 2014 -0700
Committer: Benjamin Mahler <bm...@twitter.com>
Committed: Thu May 1 15:55:59 2014 -0700

----------------------------------------------------------------------
 .../libprocess/include/process/metrics/timer.hpp  | 18 +++++++++---------
 3rdparty/libprocess/src/tests/metrics_tests.cpp   |  7 +++++--
 2 files changed, 14 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/88c235a8/3rdparty/libprocess/include/process/metrics/timer.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/metrics/timer.hpp b/3rdparty/libprocess/include/process/metrics/timer.hpp
index 26f997c..9236954 100644
--- a/3rdparty/libprocess/include/process/metrics/timer.hpp
+++ b/3rdparty/libprocess/include/process/metrics/timer.hpp
@@ -17,16 +17,18 @@
 namespace process {
 namespace metrics {
 
-// A Metric that represents a timed event in milliseconds.
+// A Metric that represents a timed event. It is templated on a Duration
+// subclass that specifies the unit to use for the Timer.
 // TODO(dhamon): Allow the user to choose the unit of duration.
 // We could do this by adding methods on Duration subclasses to return
 // the double value and unit string directly.
+template<class T>
 class Timer : public Metric
 {
 public:
-  // The Timer name will have "_ms" as an implicit unit suffix.
+  // The Timer name will have a unit suffix added automatically.
   Timer(const std::string& name, const Option<Duration>& window = None())
-    : Metric(name + "_ms", window),
+    : Metric(name + "_" + T::units(), window),
       data(new Data()) {}
 
   Future<double> value() const
@@ -65,8 +67,7 @@ public:
     {
       data->stopwatch.stop();
 
-      // Assume milliseconds for now.
-      data->lastValue = data->stopwatch.elapsed().ms();
+      data->lastValue = T(data->stopwatch.elapsed()).value();
 
       value = data->lastValue.get();
     }
@@ -76,8 +77,8 @@ public:
   }
 
   // Time an asynchronous event.
-  template<typename T>
-  Future<T> time(const Future<T>& future)
+  template<typename U>
+  Future<U> time(const Future<U>& future)
   {
     Stopwatch stopwatch;
     stopwatch.start();
@@ -107,8 +108,7 @@ private:
 
     process::internal::acquire(&that.data->lock);
     {
-      // Assume milliseconds for now.
-      that.data->lastValue = stopwatch.elapsed().ms();
+      that.data->lastValue = T(stopwatch.elapsed()).value();
       value = that.data->lastValue.get();
     }
     process::internal::release(&that.data->lock);

http://git-wip-us.apache.org/repos/asf/mesos/blob/88c235a8/3rdparty/libprocess/src/tests/metrics_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/tests/metrics_tests.cpp b/3rdparty/libprocess/src/tests/metrics_tests.cpp
index 75f1f0e..9f04b9b 100644
--- a/3rdparty/libprocess/src/tests/metrics_tests.cpp
+++ b/3rdparty/libprocess/src/tests/metrics_tests.cpp
@@ -240,7 +240,8 @@ TEST(Metrics, SnapshotStatistics)
 
 TEST(Metrics, Timer)
 {
-  metrics::Timer timer("test/timer");
+  metrics::Timer<Nanoseconds> timer("test/timer");
+  EXPECT_EQ("test/timer_ns", timer.name());
 
   AWAIT_READY(metrics::add(timer));
 
@@ -267,7 +268,9 @@ TEST(Metrics, Timer)
 // is correctly timing futures.
 TEST(Metrics, AsyncTimer)
 {
-  metrics::Timer t("test/timer");
+  metrics::Timer<Microseconds> t("test/timer");
+  EXPECT_EQ("test/timer_us", t.name());
+
   AWAIT_READY(metrics::add(t));
 
   Future<int> result = 42;