move rateHelper() to detail/Stats.h
authorAdam Simpkins <simpkins@fb.com>
Wed, 17 Apr 2013 03:41:29 +0000 (20:41 -0700)
committerSara Golemon <sgolemon@fb.com>
Mon, 20 May 2013 18:01:26 +0000 (11:01 -0700)
Summary:
The BucketedTimeSeries::rateHelper() function is generic, and not
specific to the BucketedTimeSeries class.  This diff moves it to
folly/detail/Stats.h, so other parts of the stats code can access it as
well.

Test Plan: Ran the folly unit tests.

Reviewed By: delong.j@fb.com

FB internal diff: D778115

folly/detail/Stats.h
folly/stats/BucketedTimeSeries.h

index 3e5ef06ffc5b7aa2bf1e1c5dc42aad4885c1277b..4729eeed4716b136bee7ede199520733dfbdc0c9 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef FOLLY_DETAIL_STATS_H_
 #define FOLLY_DETAIL_STATS_H_
 
+#include <chrono>
 #include <cstdint>
 #include <type_traits>
 
@@ -49,6 +50,36 @@ avgHelper(ValueType sum, uint64_t count) {
   return static_cast<ReturnType>(sumf / countf);
 }
 
+/*
+ * Helper function to compute the rate per Interval,
+ * given the specified count recorded over the elapsed time period.
+ */
+template <typename ReturnType=double,
+          typename TimeType=std::chrono::seconds,
+          typename Interval=TimeType>
+ReturnType rateHelper(ReturnType count, TimeType elapsed) {
+  if (elapsed == TimeType(0)) {
+    return 0;
+  }
+
+  // Use std::chrono::duration_cast to convert between the native
+  // duration and the desired interval.  However, convert the rates,
+  // rather than just converting the elapsed duration.  Converting the
+  // elapsed time first may collapse it down to 0 if the elapsed interval
+  // is less than the desired interval, which will incorrectly result in
+  // an infinite rate.
+  typedef std::chrono::duration<
+      ReturnType, std::ratio<TimeType::period::den,
+                             TimeType::period::num>> NativeRate;
+  typedef std::chrono::duration<
+      ReturnType, std::ratio<Interval::period::den,
+                             Interval::period::num>> DesiredRate;
+
+  NativeRate native(count / elapsed.count());
+  DesiredRate desired = std::chrono::duration_cast<DesiredRate>(native);
+  return desired.count();
+}
+
 
 template<typename T>
 struct Bucket {
index 1c8a4ab09a75b407215e924c6130f85e579f133d..22d8421dca8796a0c9a8150bce8478412b4917a8 100644 (file)
@@ -372,26 +372,8 @@ class BucketedTimeSeries {
  private:
   template <typename ReturnType=double, typename Interval=TimeType>
   ReturnType rateHelper(ReturnType numerator, TimeType elapsed) const {
-    if (elapsed == TimeType(0)) {
-      return 0;
-    }
-
-    // Use std::chrono::duration_cast to convert between the native
-    // duration and the desired interval.  However, convert the rates,
-    // rather than just converting the elapsed duration.  Converting the
-    // elapsed time first may collapse it down to 0 if the elapsed interval
-    // is less than the desired interval, which will incorrectly result in
-    // an infinite rate.
-    typedef std::chrono::duration<
-        ReturnType, std::ratio<TimeType::period::den,
-                               TimeType::period::num>> NativeRate;
-    typedef std::chrono::duration<
-        ReturnType, std::ratio<Interval::period::den,
-                               Interval::period::num>> DesiredRate;
-
-    NativeRate native(numerator / elapsed.count());
-    DesiredRate desired = std::chrono::duration_cast<DesiredRate>(native);
-    return desired.count();
+    return detail::rateHelper<ReturnType, TimeType, Interval>(numerator,
+                                                              elapsed);
   }
 
   ValueType rangeAdjust(TimeType bucketStart, TimeType nextBucketStart,