--- jsr166/src/test/loops/StringMapLoops.java 2011/03/15 19:47:06 1.7 +++ jsr166/src/test/loops/StringMapLoops.java 2015/10/12 20:16:24 1.14 @@ -4,13 +4,15 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ +import java.io.*; +import java.math.*; import java.util.*; import java.util.concurrent.*; public class StringMapLoops { - static int nkeys = 75000; + static int nkeys = 147456; static int pinsert = 60; - static int premove = 2; + static int premove = 5; static int maxThreads = 100; static int nops = 8000000; static int removesPerMaxRandom; @@ -20,7 +22,7 @@ public class StringMapLoops { public static void main(String[] args) throws Exception { - Class mapClass = null; + Class mapClass = null; if (args.length > 0) { try { mapClass = Class.forName(args[0]); @@ -58,11 +60,12 @@ public class StringMapLoops { System.out.print(" ops: " + nops); System.out.println(); - String[] key = makeKeys(nkeys); + String[] key = new String[nkeys]; + initStringKeys(key, nkeys); + // String[] key = makeKeys(nkeys); - int k = 1; int warmups = 2; - for (int i = 1; i <= maxThreads;) { + for (int k = 1, i = 1; i <= maxThreads;) { Thread.sleep(100); test(i, nkeys, key, mapClass); shuffleKeys(key); @@ -87,28 +90,6 @@ public class StringMapLoops { pool.shutdown(); } - static String[] makeKeys(int n) { - LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); - String[] key = new String[n]; - for (int i = 0; i < key.length; ++i) { - int k = 0; - int len = 1 + (rng.next() & 0xf); - char[] c = new char[len * 4]; - for (int j = 0; j < len; ++j) { - int r = rng.next(); - c[k++] = (char) (' ' + (r & 0x7f)); - r >>>= 8; - c[k++] = (char) (' ' + (r & 0x7f)); - r >>>= 8; - c[k++] = (char) (' ' + (r & 0x7f)); - r >>>= 8; - c[k++] = (char) (' ' + (r & 0x7f)); - } - key[i] = new String(c); - } - return key; - } - static void shuffleKeys(String[] key) { Random rng = new Random(); for (int i = key.length; i > 1; --i) { @@ -119,7 +100,7 @@ public class StringMapLoops { } } - static void test(int i, int nkeys, String[] key, Class mapClass) throws Exception { + static void test(int i, int nkeys, String[] key, Class mapClass) throws Exception { System.out.print("Threads: " + i + "\t:"); Map map = (Map)mapClass.newInstance(); // Uncomment to start with a non-empty table @@ -158,7 +139,7 @@ public class StringMapLoops { } int step() { - // random-walk around key positions, bunching accesses + // random-walk around key positions, bunching accesses int r = rng.next(); position += (r & 7) - 3; while (position >= key.length) position -= key.length; @@ -177,6 +158,7 @@ public class StringMapLoops { } else if (r < insertsPerMaxRandom) { ++position; + // map.putIfAbsent(k, k); map.put(k, k); return 2; } @@ -198,4 +180,43 @@ public class StringMapLoops { } } } + + static final String wordFile = "testwords.txt"; + + // Read in String keys from file if possible + static void initStringKeys(String[] keys, int n) throws Exception { + FileInputStream fr = null; + try { + fr = new FileInputStream(wordFile); + } catch (IOException ex) { + System.out.println("No word file. Using String.valueOf(i)"); + for (int i = 0; i < n; i++) + keys[i] = String.valueOf(i); + return; + } + + BufferedInputStream in = new BufferedInputStream(fr); + int k = 0; + outer:while (k < n) { + StringBuffer sb = new StringBuffer(); + for (;;) { + int c = in.read(); + if (c < 0) + break outer; + char ch = (char) c; + if (ch == '\n') { + keys[k++] = sb.toString(); + break; + } + if (!Character.isWhitespace(ch)) + sb.append(ch); + } + } + in.close(); + + // fill up remaining keys with path-like compounds of previous pairs + int j = 0; + while (k < n) + keys[k++] = keys[j++] + "/" + keys[j]; + } }