[slf4j-dev] svn commit: r1210 - slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers

ceki at slf4j.org ceki at slf4j.org
Thu Oct 23 18:54:03 CEST 2008


Author: ceki
Date: Thu Oct 23 18:54:02 2008
New Revision: 1210

Added:
   slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/BubbleSort.java
   slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/BubbleSortTest.java
   slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MyRandom.java
Modified:
   slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/BogoPerf.java
   slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MessageFormatterPerfTest.java

Log:
- BogoPerf now uses its own random number generator as well as
  bubble sort algorithm

Modified: slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/BogoPerf.java
==============================================================================
--- slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/BogoPerf.java	(original)
+++ slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/BogoPerf.java	Thu Oct 23 18:54:02 2008
@@ -1,10 +1,39 @@
-package org.slf4j.helpers;
+/*
+ * Copyright (c) 2004-2008 QOS.ch
+ * All rights reserved.
+ * 
+ * Permission is hereby granted, free  of charge, to any person obtaining
+ * a  copy  of this  software  and  associated  documentation files  (the
+ * "Software"), to  deal in  the Software without  restriction, including
+ * without limitation  the rights to  use, copy, modify,  merge, publish,
+ * distribute,  sublicense, and/or sell  copies of  the Software,  and to
+ * permit persons to whom the Software  is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The  above  copyright  notice  and  this permission  notice  shall  be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
+ * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
+ * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
 
-import java.util.Arrays;
-import java.util.Random;
+package org.slf4j.helpers;
 
 import junit.framework.AssertionFailedError;
 
+/**
+ * BogoPerf is used to check that the time required to perform a certain
+ * operation does not deteriorate over time. BogoPerf adjusts to the CPU speed
+ * and capabilities of the host.
+ * 
+ * @author Ceki Gülcü
+ * 
+ */
 public class BogoPerf {
 
   private static long NANOS_IN_ONE_SECOND = 1000 * 1000 * 1000;
@@ -16,14 +45,13 @@
     // let the JIT warm up
     computeBogoIPS(INITIAL_N);
     double bogo_ips = computeBogoIPS(INITIAL_N);
-    System.out.println("Host runs at "+bogo_ips + " BIPS");
+    System.out.println("Host runs at " + bogo_ips + " BIPS");
   }
-  
-  
+
   /**
    * Compute bogoInstructions per second
    * <p>
-   * on a 3.2 Ghz Pentium D CPU (around 2007), we obtain about 10'000 bogoIPS.
+   * on a 3.2 Ghz Pentium D CPU (around 2007), we obtain about 9'000 bogoIPS.
    * 
    * @param N
    *                number of bogoInstructions to average over in order to
@@ -52,13 +80,15 @@
   }
 
   private static void bogoInstruction() {
-    Random random = new Random(100);
-    int len = 500;
+    // use our own random number generator, independent of the host JDK
+    MyRandom myRandom = new MyRandom(100);
+    int len = 150;
     int[] intArray = new int[len];
     for (int i = 0; i < len; i++) {
-      intArray[i] = random.nextInt();
+      intArray[i] = myRandom.nextInt();
     }
-    Arrays.sort(intArray);
+    // use our own sort algorithm, independent of the host JDK
+    BubbleSort.sort(intArray);
   }
 
   /**
@@ -69,14 +99,14 @@
   public static double currentBIPS() {
     return computeBogoIPS(LAST_N);
   }
-  
+
   static double min(double a, double b) {
     return (a <= b) ? a : b;
   }
 
   /**
-   * Assertion used for values that <b>decrease</b> with faster CPUs, 
-   * typically the time (duration) needed to perform a task.
+   * Assertion used for values that <b>decrease</b> with faster CPUs, typically
+   * the time (duration) needed to perform a task.
    * 
    * @param currentDuration
    * @param referenceDuration
@@ -84,17 +114,18 @@
    * @throws AssertionFailedError
    */
   public static void assertDuration(double currentDuration,
-      long referenceDuration, double referenceBIPS)
-      throws AssertionFailedError {
+      long referenceDuration, double referenceBIPS) throws AssertionFailedError {
     double ajustedDuration = adjustExpectedDuration(referenceDuration,
         referenceBIPS);
     if (currentDuration > ajustedDuration * SLACK_FACTOR) {
-      throw new AssertionFailedError("current duration "+ currentDuration + " exceeded expected "
-          + ajustedDuration + " (adjusted reference), " + referenceDuration + " (raw reference)");
+      throw new AssertionFailedError("current duration " + currentDuration
+          + " exceeded expected " + ajustedDuration + " (adjusted reference), "
+          + referenceDuration + " (raw reference)");
     }
   }
+
   /**
-   * Assertion used for values that <b>increase<b> with faster CPUs, typically 
+   * Assertion used for values that <b>increase<b> with faster CPUs, typically
    * the number of operations accomplished per unit of time.
    * 
    * @param currentPerformance
@@ -107,22 +138,22 @@
       throws AssertionFailedError {
     double ajustedPerf = adjustExpectedPerformance(referencePerformance,
         referenceBIPS);
-    if (currentPerformance*SLACK_FACTOR < ajustedPerf) {
+    if (currentPerformance * SLACK_FACTOR < ajustedPerf) {
       throw new AssertionFailedError(currentPerformance + " below expected "
           + ajustedPerf + " (adjusted), " + referencePerformance + " (raw)");
     }
   }
-  
+
   private static double adjustExpectedPerformance(long referenceDuration,
       double referenceBIPS) {
     double currentBIPS = currentBIPS();
-    return referenceDuration * (currentBIPS/referenceBIPS);
+    return referenceDuration * (currentBIPS / referenceBIPS);
   }
-  
+
   private static double adjustExpectedDuration(long referenceDuration,
       double referenceBIPS) {
     double currentBIPS = currentBIPS();
-    System.out.println("currentBIPS="+currentBIPS + " BIPS");
+    System.out.println("currentBIPS=" + currentBIPS + " BIPS");
     return referenceDuration * (referenceBIPS / currentBIPS);
   }
 }

Added: slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/BubbleSort.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/BubbleSort.java	Thu Oct 23 18:54:02 2008
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2004-2008 QOS.ch
+ * All rights reserved.
+ * 
+ * Permission is hereby granted, free  of charge, to any person obtaining
+ * a  copy  of this  software  and  associated  documentation files  (the
+ * "Software"), to  deal in  the Software without  restriction, including
+ * without limitation  the rights to  use, copy, modify,  merge, publish,
+ * distribute,  sublicense, and/or sell  copies of  the Software,  and to
+ * permit persons to whom the Software  is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The  above  copyright  notice  and  this permission  notice  shall  be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
+ * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
+ * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+package org.slf4j.helpers;
+
+/**
+ * This class is used internally by BogoPerf, hence the package private
+ * (default) access.
+ * 
+ * @author Ceki
+ */
+class BubbleSort {
+
+  static void sort(int[] a) {
+    int len = a.length;
+    for (int i = 0; i < len - 1; i++) {
+      for (int j = 0; j < len - 1 - i; j++) {
+        if (a[j] > a[j + 1]) {
+          swap(a, j, j + 1);
+        }
+      }
+    }
+  }
+  static void swap(int[] a, int i, int j) {
+    int t = a[i];
+    a[i] = a[j];
+    a[j] = t;
+  }
+}

Added: slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/BubbleSortTest.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/BubbleSortTest.java	Thu Oct 23 18:54:02 2008
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2004-2008 QOS.ch
+ * All rights reserved.
+ * 
+ * Permission is hereby granted, free  of charge, to any person obtaining
+ * a  copy  of this  software  and  associated  documentation files  (the
+ * "Software"), to  deal in  the Software without  restriction, including
+ * without limitation  the rights to  use, copy, modify,  merge, publish,
+ * distribute,  sublicense, and/or sell  copies of  the Software,  and to
+ * permit persons to whom the Software  is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The  above  copyright  notice  and  this permission  notice  shall  be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
+ * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
+ * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+package org.slf4j.helpers;
+
+import java.util.Arrays;
+import java.util.Random;
+
+import junit.framework.TestCase;
+
+/**
+ * Test that our BubbleSort algorithm is correctly implemented.
+ * 
+ * @author Ceki
+ *
+ */
+public class BubbleSortTest extends TestCase {
+  
+  public void testSmoke() {
+     int[] a = new int[] {5,3,2,7};
+     BubbleSort.sort(a);
+     int i = 0;
+     assertEquals(2, a[i++]);
+     assertEquals(3, a[i++]);
+     assertEquals(5, a[i++]);
+     assertEquals(7, a[i++]);
+  }
+  
+  public void testEmpty() {
+    int[] a = new int[] {};
+    BubbleSort.sort(a);
+  }
+  
+  public void testSorted() {
+    int[] a = new int[] {3,30,300,3000};
+    BubbleSort.sort(a);
+    int i = 0;
+    assertEquals(3, a[i++]);
+    assertEquals(30, a[i++]);
+    assertEquals(300, a[i++]);
+    assertEquals(3000, a[i++]);
+  }
+  
+  public void testInverted() {
+    int[] a = new int[] {3000,300,30,3};
+    BubbleSort.sort(a);
+    int i = 0;
+    assertEquals(3, a[i++]);
+    assertEquals(30, a[i++]);
+    assertEquals(300, a[i++]);
+    assertEquals(3000, a[i++]);
+  }
+
+  public void testWithSameEntry() {
+    int[] a = new int[] {10,20,10,20};
+    BubbleSort.sort(a);
+    int i = 0;
+    assertEquals(10, a[i++]);
+    assertEquals(10, a[i++]);
+    assertEquals(20, a[i++]);
+    assertEquals(20, a[i++]);
+  }
+
+  
+  public void testRandom() {
+    int len = 100;
+    Random random = new Random(156);
+    int[] a = new int[len];
+    int[] witness = new int[len];
+    for(int i = 0; i < len; i++) {
+      int r = random.nextInt();
+      a[i] = r;
+      witness[i] = r;
+    }
+   BubbleSort.sort(a);
+   Arrays.sort(witness);
+   assertTrue(Arrays.equals(witness, a));
+  }
+
+}

Modified: slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MessageFormatterPerfTest.java
==============================================================================
--- slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MessageFormatterPerfTest.java	(original)
+++ slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MessageFormatterPerfTest.java	Thu Oct 23 18:54:02 2008
@@ -8,7 +8,7 @@
 
   Integer i1 = new Integer(1);
   static long RUN_LENGTH = 100000;
-  static long REFERENCE_BIPS = 9629;
+  static long REFERENCE_BIPS = 9000;
   
   public MessageFormatterPerfTest(String name) {
     super(name);

Added: slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MyRandom.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-api/src/test/java/org/slf4j/helpers/MyRandom.java	Thu Oct 23 18:54:02 2008
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2004-2008 QOS.ch
+ * All rights reserved.
+ * 
+ * Permission is hereby granted, free  of charge, to any person obtaining
+ * a  copy  of this  software  and  associated  documentation files  (the
+ * "Software"), to  deal in  the Software without  restriction, including
+ * without limitation  the rights to  use, copy, modify,  merge, publish,
+ * distribute,  sublicense, and/or sell  copies of  the Software,  and to
+ * permit persons to whom the Software  is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The  above  copyright  notice  and  this permission  notice  shall  be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
+ * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
+ * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+package org.slf4j.helpers;
+
+
+class MyRandom  {
+
+  private static final long serialVersionUID = -907426287094698288L;
+
+  private final static long m = 200000000041L;  // a prime number
+  private final static long a = 2000000011L; // a prime number
+  
+  long y;
+  long unused;
+  int bits = 32;
+  
+  public MyRandom() { 
+    this(System.nanoTime()); 
+  }
+
+  
+  public MyRandom(long seed) { 
+    this.y = seed; 
+  }
+  
+  
+  int nextInt() {
+    // we don't really care about the randomness of this
+    // generator
+    y = (a*y + 1) % m;
+    unused = y >>> (48-bits); // just exercise the >>> operator
+    return (int)(y);
+  }
+}



More information about the slf4j-dev mailing list