Histogram function to write tab-separated values.
[folly.git] / folly / Histogram-inl.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_