Histogram function to write tab-separated values.
authorPhilip Pronin <philipp@fb.com>
Sat, 1 Sep 2012 08:03:07 +0000 (01:03 -0700)
committerJordan DeLong <jdelong@fb.com>
Mon, 17 Sep 2012 01:30:48 +0000 (18:30 -0700)
Summary: Export histograms in a tab-separated format.

Test Plan: Used it.

Reviewed By: lucian@fb.com

folly/Histogram-inl.h
folly/Histogram.h

index 97d2bffd64d21dbce486d11b4721c17d61d69f8e..33ec289473cf73b78123b8670e68036ca9465208 100644 (file)
@@ -242,15 +242,28 @@ std::string Histogram<T>::debugString() const {
       ", bucketSize: ", buckets_.getBucketSize(),
       ", min: ", buckets_.getMin(), ", max: ", buckets_.getMax(), "\n");
 
-  for (unsigned int n = 0; n < buckets_.getNumBuckets(); ++n) {
-    folly::toAppend("  ", buckets_.getBucketMin(n), ": ",
-                    buckets_.getByIndex(n).count, "\n",
+  for (unsigned int i = 0; i < buckets_.getNumBuckets(); ++i) {
+    folly::toAppend("  ", buckets_.getBucketMin(i), ": ",
+                    buckets_.getByIndex(i).count, "\n",
                     &ret);
   }
 
   return ret;
 }
 
+template <typename T>
+void Histogram<T>::toTSV(std::ostream& out, bool skipEmptyBuckets) const {
+  for (unsigned int i = 0; i < buckets_.getNumBuckets(); ++i) {
+    // Do not output empty buckets in order to reduce data file size.
+    if (skipEmptyBuckets && getBucketByIndex(i).count == 0) {
+      continue;
+    }
+    const auto& bucket = getBucketByIndex(i);
+    out << getBucketMin(i) << '\t' << getBucketMax(i) << '\t'
+        << bucket.count << '\t' << bucket.sum << '\n';
+  }
+}
+
 } // folly
 
 #endif // FOLLY_HISTOGRAM_INL_H_
index 302739944487baf43c288e4200ccae1dd879e6c2..fb72145047558f86f0e0b694d79b4ee3ee3208a6 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <cstddef>
 #include <limits>
+#include <ostream>
 #include <string>
 #include <vector>
 #include <stdexcept>
@@ -367,6 +368,12 @@ class Histogram {
    */
   std::string debugString() const;
 
+  /*
+   * Write the histogram contents in tab-separated values (TSV) format.
+   * Format is "min max count sum".
+   */
+  void toTSV(std::ostream& out, bool skipEmptyBuckets = true) const;
+
  private:
   struct CountFromBucket {
     uint64_t operator()(const Bucket& bucket) const {