WeightedSnapshot extends AbstractSnapshot { @Override public double getStdDev() { if (values.length <= 1) { return 0; } final double mean = getMean(); double variance = 0; for (int i = 0; i < values.length; i++) { final double diff = values[i] - mean; variance += normWeights[i] * diff*diff; } return Math.sqrt(variance); } WeightedSnapshot(Collection<WeightedSample> values); @Override double getValue(double quantile); @Override int size(); @Override long[] getValues(); @Override long getMax(); @Override long getMin(); @Override double getMean(); @Override double getStdDev(); @Override void dump(OutputStream output);  }
@Test public void calculatesTheStdDev() throws Exception { assertThat(snapshot.getStdDev()) .isEqualTo(1.2688, offset(0.0001)); }
@Test public void calculatesAStdDevOfZeroForAnEmptySnapshot() throws Exception { final Snapshot emptySnapshot = new WeightedSnapshot( WeightedArray(new long[]{}, new double[]{}) ); assertThat(emptySnapshot.getStdDev()) .isZero(); }
@Test public void calculatesAStdDevOfZeroForASingletonSnapshot() throws Exception { final Snapshot singleItemSnapshot = new WeightedSnapshot( WeightedArray(new long[]{ 1 }, new double[]{ 1.0 }) ); assertThat(singleItemSnapshot.getStdDev()) .isZero(); }