changeset 377:4659c9e09268

Statistics should be Comparable<Statistics> via the proper inference.
author shade
date Tue, 28 Jan 2014 12:52:58 +0400
parents 61a59c1c9453
children c0050f0615ed
files jmh-core/src/main/java/org/openjdk/jmh/util/internal/AbstractStatistics.java jmh-core/src/main/java/org/openjdk/jmh/util/internal/Statistics.java jmh-core/src/test/java/org/openjdk/jmh/util/TestListStatistics.java jmh-core/src/test/java/org/openjdk/jmh/util/TestMultisetStatistics.java
diffstat 4 files changed, 54 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/internal/AbstractStatistics.java	Tue Jan 28 00:59:02 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/internal/AbstractStatistics.java	Tue Jan 28 12:52:58 2014 +0400
@@ -87,4 +87,19 @@
         return Math.sqrt(getVariance());
     }
 
+    @Override
+    public int compareTo(Statistics other, double confidence) {
+        if (isDifferent(other, confidence)) {
+            double t = getMean();
+            double o = other.getMean();
+            return (t > o) ? -1 : 1;
+        } else {
+            return 0;
+        }
+    }
+
+    @Override
+    public int compareTo(Statistics other) {
+        return compareTo(other, 0.99);
+    }
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/internal/Statistics.java	Tue Jan 28 00:59:02 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/internal/Statistics.java	Tue Jan 28 12:52:58 2014 +0400
@@ -28,7 +28,7 @@
 
 import java.io.Serializable;
 
-public interface Statistics extends Serializable, StatisticalSummary {
+public interface Statistics extends Serializable, StatisticalSummary, Comparable<Statistics> {
 
     /**
      * Gets the confidence interval at given confidence level.
@@ -53,6 +53,28 @@
      */
     boolean isDifferent(Statistics other, double confidence);
 
+    /**
+     * Compares this statistics to another one.
+     * Follows the contract of {@link Comparable}.
+     *
+     * @param other statistics to compare against
+     * @return a negative integer, zero, or a positive integer as this statistics
+     *          is less than, equal to, or greater than the specified statistics.
+     */
+    int compareTo(Statistics other);
+
+    /**
+     * Compares this statistics to another one.
+     * Follows the contract of {@link Comparable}.
+     *
+     * @param other statistics to compare against
+     * @param confidence confidence level (e.g. 0.99)
+     * @return a negative integer, zero, or a positive integer as this statistics
+     *          is less than, equal to, or greater than the specified statistics.
+     */
+
+    int compareTo(Statistics other, double confidence);
+
     double getStandardDeviation();
 
     double getMax();
--- a/jmh-core/src/test/java/org/openjdk/jmh/util/TestListStatistics.java	Tue Jan 28 00:59:02 2014 +0400
+++ b/jmh-core/src/test/java/org/openjdk/jmh/util/TestListStatistics.java	Tue Jan 28 12:52:58 2014 +0400
@@ -162,6 +162,7 @@
 
         for (double conf : new double[] {0.5, 0.9, 0.99, 0.999, 0.9999, 0.99999}) {
             Assert.assertTrue("Diff significant at " + conf, s1.isDifferent(s2, conf));
+            Assert.assertEquals(1, s1.compareTo(s2, conf));
         }
     }
 
@@ -179,6 +180,7 @@
 
         for (double conf : new double[] {0.5, 0.9, 0.99, 0.999, 0.9999, 0.99999}) {
             Assert.assertFalse("Diff not significant at " + conf, s1.isDifferent(s2, conf));
+            Assert.assertEquals(0, s1.compareTo(s2, conf));
         }
     }
 
@@ -199,6 +201,12 @@
         Assert.assertFalse("Diff not significant at 0.99", s1.isDifferent(s2, 0.99));
         Assert.assertFalse("Diff not significant at 0.999", s1.isDifferent(s2, 0.999));
         Assert.assertFalse("Diff not significant at 0.9999", s1.isDifferent(s2, 0.9999));
+
+        Assert.assertEquals("compareTo at 0.5", 1, s1.compareTo(s2, 0.5));
+        Assert.assertEquals("compareTo at 0.9", 1, s1.compareTo(s2, 0.9));
+        Assert.assertEquals("compareTo at 0.99", 0, s1.compareTo(s2, 0.99));
+        Assert.assertEquals("compareTo at 0.999", 0, s1.compareTo(s2, 0.999));
+        Assert.assertEquals("compareTo at 0.9999", 0, s1.compareTo(s2, 0.9999));
     }
 
 }
--- a/jmh-core/src/test/java/org/openjdk/jmh/util/TestMultisetStatistics.java	Tue Jan 28 00:59:02 2014 +0400
+++ b/jmh-core/src/test/java/org/openjdk/jmh/util/TestMultisetStatistics.java	Tue Jan 28 12:52:58 2014 +0400
@@ -160,6 +160,7 @@
 
         for (double conf : new double[] {0.5, 0.9, 0.99, 0.999, 0.9999, 0.99999}) {
             Assert.assertTrue("Diff significant at " + conf, s1.isDifferent(s2, conf));
+            Assert.assertEquals(1, s1.compareTo(s2, conf));
         }
     }
 
@@ -175,6 +176,7 @@
 
         for (double conf : new double[] {0.5, 0.9, 0.99, 0.999, 0.9999, 0.99999}) {
             Assert.assertFalse("Diff not significant at " + conf, s1.isDifferent(s2, conf));
+            Assert.assertEquals(0, s1.compareTo(s2, conf));
         }
     }
 
@@ -193,6 +195,12 @@
         Assert.assertFalse("Diff not significant at 0.99", s1.isDifferent(s2, 0.99));
         Assert.assertFalse("Diff not significant at 0.999", s1.isDifferent(s2, 0.999));
         Assert.assertFalse("Diff not significant at 0.9999", s1.isDifferent(s2, 0.9999));
+
+        Assert.assertEquals("compareTo at 0.5", 1, s1.compareTo(s2, 0.5));
+        Assert.assertEquals("compareTo at 0.9", 1, s1.compareTo(s2, 0.9));
+        Assert.assertEquals("compareTo at 0.99", 0, s1.compareTo(s2, 0.99));
+        Assert.assertEquals("compareTo at 0.999", 0, s1.compareTo(s2, 0.999));
+        Assert.assertEquals("compareTo at 0.9999", 0, s1.compareTo(s2, 0.9999));
     }
 
 }