From: <tho...@us...> - 2011-04-19 14:13:00
|
Revision: 4414 http://bigdata.svn.sourceforge.net/bigdata/?rev=4414&view=rev Author: thompsonbry Date: 2011-04-19 14:12:54 +0000 (Tue, 19 Apr 2011) Log Message: ----------- Pulled XorShift out of two test classes and into its own class within the test suite. Added a main() routine which runs the generator and writes out some stats on its randomness. Added nextBoolean(), nextInt(), and nextFloat() methods to the XorShift generator for compatibility with Random. However, it still does not implement several method on Random that we use in a large number of unit tests, esp nextInt(n) and nextBytes(byte[]). Modified Paths: -------------- branches/QUADS_QUERY_BRANCH/bigdata/src/test/com/bigdata/cache/StressTestGlobalLRU.java branches/QUADS_QUERY_BRANCH/bigdata/src/test/com/bigdata/cache/TestHardReferenceQueueWithBatchingUpdates.java Added Paths: ----------- branches/QUADS_QUERY_BRANCH/bigdata/src/test/com/bigdata/XorShift.java Added: branches/QUADS_QUERY_BRANCH/bigdata/src/test/com/bigdata/XorShift.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata/src/test/com/bigdata/XorShift.java (rev 0) +++ branches/QUADS_QUERY_BRANCH/bigdata/src/test/com/bigdata/XorShift.java 2011-04-19 14:12:54 UTC (rev 4414) @@ -0,0 +1,124 @@ +package com.bigdata; + +import java.util.Random; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * XorShift - provides a pseudo random number generator without synchronization + * which is used for unit tests in which we do not want to introduce side + * effects from synchronization by the test harness on the object under test. + * + * @author Brian Goetz and Tim Peierls + * @author Bryan Thompson + */ +public class XorShift { + + private static final AtomicInteger seq = new AtomicInteger(8862213); + + private int x = -1831433054; + + public XorShift(final int seed) { + x = seed; + } + + public XorShift() { + this((int) System.nanoTime() + seq.getAndAdd(129)); + } + + public int next() { + x ^= x << 6; + x ^= x >>> 21; + x ^= (x << 7); + return x; + } + + /** For compatibility with {@link Random#nextInt()}. */ + public int nextInt() { + + return next(); + + } + + /** For compatibility with {@link Random#nextBoolean()}. */ + public boolean nextBoolean() { + + final int b = next(); + + /* + * mask a bit and test for non-zero. this uses bit ONE which tends to + * produce true and false with a uniform distribution as demonstrated by + * the main() routine. + */ + + return (b & 1/* mask */) != 0; + + } + + /** For compatibility with {@link Random#nextFloat()}. */ + public float nextFloat() { + + return Float.intBitsToFloat(next()); + + } + + /** + * Utility for looking at various distributions generated by + * {@link XorShift}. + * + * @param args + */ + public static void main(final String[] args) { + + final XorShift r = new XorShift(); +// final Random r = new Random(); + + final int ntrials = 1000; + + { + int ntrue = 0; + for (int i = 0; i < ntrials; i++) { + if (r.nextBoolean()) + ntrue++; + } + System.out.println("ntrials=" + ntrials + ", ntrue=" + ntrue); + } + + { + /* + * Generate random values and take their running sum. + * + * Note: This does not check for overflow, but it uses long for the + * sum and int for the random values. + */ + long sum = 0; + final int[] a = new int[ntrials]; + for (int i = 0; i < ntrials; i++) { + final int n = r.nextInt(); + a[i] = n; + sum += n; + } + + // The mean of those random values. + final double mean = sum / (double) ntrials; + + /* + * Compute the sum of squares of the difference between the random + * values and their mean. + */ + double sse = 0; // sum squared error. + final double[] diffs = new double[ntrials]; + for (int i = 0; i < ntrials; i++) { + double d = (mean - (double) a[i]); // difference from mean + d *= d;// squared + diffs[i] = d; + sse += d; + } + final double var = sse / ntrials; // variance. + final double stdev = Math.sqrt(var); // standard deviation. + System.out.println("ntrials=" + ntrials + ", sum=" + sum + ", sse=" + + sse + ", var=" + var + ", stdev=" + stdev); + } + + } + +} Property changes on: branches/QUADS_QUERY_BRANCH/bigdata/src/test/com/bigdata/XorShift.java ___________________________________________________________________ Added: svn:keywords + Id Date Revision Author HeadURL Modified: branches/QUADS_QUERY_BRANCH/bigdata/src/test/com/bigdata/cache/StressTestGlobalLRU.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata/src/test/com/bigdata/cache/StressTestGlobalLRU.java 2011-04-19 13:34:12 UTC (rev 4413) +++ branches/QUADS_QUERY_BRANCH/bigdata/src/test/com/bigdata/cache/StressTestGlobalLRU.java 2011-04-19 14:12:54 UTC (rev 4414) @@ -41,13 +41,13 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; import junit.framework.TestCase; import org.apache.log4j.Logger; import com.bigdata.LRUNexus; +import com.bigdata.XorShift; import com.bigdata.LRUNexus.AccessPolicyEnum; import com.bigdata.LRUNexus.CacheSettings; import com.bigdata.concurrent.TestLockManager; @@ -527,33 +527,6 @@ } /** - * XorShift - * - * @author Brian Goetz and Tim Peierls - */ - public static class XorShift { - - static final AtomicInteger seq = new AtomicInteger(8862213); - - int x = -1831433054; - - public XorShift(int seed) { - x = seed; - } - - public XorShift() { - this((int) System.nanoTime() + seq.getAndAdd(129)); - } - - public int next() { - x ^= x << 6; - x ^= x >>> 21; - x ^= (x << 7); - return x; - } - } - - /** * Helper class generates a random sequence of operation codes obeying the * probability distribution described in the constructor call. * Modified: branches/QUADS_QUERY_BRANCH/bigdata/src/test/com/bigdata/cache/TestHardReferenceQueueWithBatchingUpdates.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata/src/test/com/bigdata/cache/TestHardReferenceQueueWithBatchingUpdates.java 2011-04-19 13:34:12 UTC (rev 4413) +++ branches/QUADS_QUERY_BRANCH/bigdata/src/test/com/bigdata/cache/TestHardReferenceQueueWithBatchingUpdates.java 2011-04-19 14:12:54 UTC (rev 4414) @@ -44,11 +44,11 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import junit.framework.TestCase2; +import com.bigdata.XorShift; import com.bigdata.journal.ConcurrencyManager; import com.bigdata.test.ExperimentDriver; import com.bigdata.test.ExperimentDriver.IComparisonTest; @@ -139,33 +139,6 @@ } /** - * XorShift - * - * @author Brian Goetz and Tim Peierls - */ - public static class XorShift { - - static final AtomicInteger seq = new AtomicInteger(8862213); - - int x = -1831433054; - - public XorShift(int seed) { - x = seed; - } - - public XorShift() { - this((int) System.nanoTime() + seq.getAndAdd(129)); - } - - public int next() { - x ^= x << 6; - x ^= x >>> 21; - x ^= (x << 7); - return x; - } - } - - /** * A default configuration of a parameterized stress test. * * @throws ExecutionException This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |