From 36ff72b10ca0e9b12abae12eb83c24f2d10d8e67 Mon Sep 17 00:00:00 2001
From: nixon <nixon@fb.com>
Date: Wed, 15 Aug 2012 15:02:46 -0700
Subject: [PATCH] Added variance calculation of histogram latency reporting.

---
 .../measurements/OneMeasurementHistogram.java | 47 ++++++++++++++++---
 1 file changed, 41 insertions(+), 6 deletions(-)

diff --git a/core/src/main/java/com/yahoo/ycsb/measurements/OneMeasurementHistogram.java b/core/src/main/java/com/yahoo/ycsb/measurements/OneMeasurementHistogram.java
index 2cba955a..4e8f5642 100644
--- a/core/src/main/java/com/yahoo/ycsb/measurements/OneMeasurementHistogram.java
+++ b/core/src/main/java/com/yahoo/ycsb/measurements/OneMeasurementHistogram.java
@@ -36,11 +36,39 @@ public class OneMeasurementHistogram extends OneMeasurement
 	public static final String BUCKETS="histogram.buckets";
 	public static final String BUCKETS_DEFAULT="1000";
 
+
+	/**
+	 * Specify the range of latencies to track in the histogram.
+	 */
 	int _buckets;
+
+	/**
+     * Groups operations in discrete blocks of 1ms width.
+     */
 	int[] histogram;
+	
+	/**
+	 * Counts all operations outside the histogram's range.
+	 */
 	int histogramoverflow;
+	
+	/**
+	 * The total number of reported operations.
+	 */
 	int operations;
-	long totallatency;
+	
+	/**
+	 * The sum of each latency measurement over all operations.
+	 * Calculated in ms.
+	 */
+	double totallatency;
+	
+	/**
+	 * The sum of each latency measurement squared over all operations. 
+	 * Used to calculate variance of latency.
+	 * Calculated in ms. 
+	 */
+	double totalsquaredlatency;
 	
 	//keep a windowed version of these stats for printing status
 	int windowoperations;
@@ -58,6 +86,7 @@ public class OneMeasurementHistogram extends OneMeasurement
 		histogramoverflow=0;
 		operations=0;
 		totallatency=0;
+		totalsquaredlatency=0;
 		windowoperations=0;
 		windowtotallatency=0;
 		min=-1;
@@ -86,6 +115,7 @@ public class OneMeasurementHistogram extends OneMeasurement
 	 */
 	public synchronized void measure(int latency)
 	{
+	    //latency reported in us and collected in bucket by ms.
 		if (latency/1000>=_buckets)
 		{
 			histogramoverflow++;
@@ -95,10 +125,11 @@ public class OneMeasurementHistogram extends OneMeasurement
 			histogram[latency/1000]++;
 		}
 		operations++;
-		totallatency+=latency;
+		totallatency += ((double)latency) / 1000;
+		totalsquaredlatency += ((double)latency) * ((double)latency) / 1000000;
 		windowoperations++;
-		windowtotallatency+=latency;
-
+		windowtotallatency += latency;
+		
 		if ( (min<0) || (latency<min) )
 		{
 			min=latency;
@@ -112,10 +143,14 @@ public class OneMeasurementHistogram extends OneMeasurement
 
 
   @Override
-  public void exportMeasurements(MeasurementsExporter exporter) throws IOException
+  public void exportMeasurements(MeasurementsExporter exporter) 
+          throws IOException
   {
+    double mean = totallatency/((double)operations);
+    double variance = totalsquaredlatency/((double)operations) - (mean * mean);
     exporter.write(getName(), "Operations", operations);
-    exporter.write(getName(), "AverageLatency(us)", (((double)totallatency)/((double)operations)));
+    exporter.write(getName(), "AverageLatency(ms)", mean);
+    exporter.write(getName(), "LatencyVariance(ms)", variance);
     exporter.write(getName(), "MinLatency(us)", min);
     exporter.write(getName(), "MaxLatency(us)", max);
     
-- 
GitLab