[FELIX-4942] Improve the big resolution test to help testing for performance

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1690696 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/resolver/src/test/java/org/apache/felix/resolver/test/BigResolutionTest.java b/resolver/src/test/java/org/apache/felix/resolver/test/BigResolutionTest.java
index da48e43..7e11748 100644
--- a/resolver/src/test/java/org/apache/felix/resolver/test/BigResolutionTest.java
+++ b/resolver/src/test/java/org/apache/felix/resolver/test/BigResolutionTest.java
@@ -53,21 +53,49 @@
 import org.osgi.service.resolver.HostedCapability;
 import org.osgi.service.resolver.ResolveContext;
 
+import static org.junit.Assert.assertEquals;
+
 public class BigResolutionTest {
 
     @Test
     @Ignore
     public void testResolutionSpeed() throws Exception {
+        ResolverImpl resolver = new ResolverImpl(new Logger(Logger.LOG_INFO));
+
         ResolveContext rc = buildResolutionContext();
 
-        ResolverImpl resolver = new ResolverImpl(new Logger(Logger.LOG_INFO));
-        for (int i = 0; i < 10; i++) {
+        System.out.println("Warming up...");
+        Map<Resource, List<Wire>> wires = resolver.resolve(rc);
+        resolver.resolve(rc);
+
+        System.out.println("Running...");
+        RunningStat stats = new RunningStat();
+        for (int i = 1; i <= 100; i++) {
             System.gc();
-            long t0 = System.currentTimeMillis();
-            resolver.resolve(rc);
-            long t1 = System.currentTimeMillis();
-            System.out.println("Resolver took " + (t1 - t0) + " ms");
+            Thread.sleep(100);
+            System.gc();
+            Thread.sleep(100);
+            long t0 = System.nanoTime();
+            Map<Resource, List<Wire>> newWires = resolver.resolve(rc);
+            long t1 = System.nanoTime();
+            double dt = (t1 - t0) * 1E-6;
+            System.out.println("Resolver took " + String.format("%7.2f", dt) + " ms");
+            stats.put(dt);
+            assertEquals(wires, newWires);
+
+            if (i % 10 == 0) {
+                System.out.println();
+                System.out.println("Summary");
+                System.out.println("    Min:    " + String.format("%7.2f", stats.getMin()) + " ms");
+                System.out.println("    Max:    " + String.format("%7.2f", stats.getMax()) + " ms");
+                System.out.println("    Avg:    " + String.format("%7.2f", stats.getAverage()) + " ms");
+                System.out.println("    StdDev: " + String.format("%7" +
+                        ".2f", stats.getStdDev() / stats.getAverage() * 100.0) + " %");
+                System.out.println();
+                stats = new RunningStat();
+            }
         }
+
     }
 
     @Test
@@ -372,4 +400,48 @@
         }
     }
 
+    public static class RunningStat {
+
+        private int count = 0;
+        private double min = Double.MAX_VALUE;
+        private double max = 0.0;
+        private double average = 0.0;
+        private double pwrSumAvg = 0.0;
+        private double stdDev = 0.0;
+
+        /**
+         * Incoming new values used to calculate the running statistics
+         *
+         * @param value the new value
+         */
+        public void put(double value) {
+
+            count++;
+            average += (value - average) / count;
+            pwrSumAvg += (value * value - pwrSumAvg) / count;
+            stdDev = Math.sqrt((pwrSumAvg * count - count * average * average) / (count - 1));
+            min = Math.min(min, value);
+            max = Math.max(max, value);
+
+        }
+
+        public double getMin() {
+            return min;
+        }
+
+        public double getMax() {
+            return max;
+        }
+
+        public double getAverage() {
+
+            return average;
+        }
+
+        public double getStdDev() {
+
+            return Double.isNaN(stdDev) ? 0.0 : stdDev;
+        }
+
+    }
 }