ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/MapLoops.java
(Generate patch)

Comparing jsr166/src/test/loops/MapLoops.java (file contents):
Revision 1.1 by dl, Mon May 2 19:19:38 2005 UTC vs.
Revision 1.10 by jsr166, Thu Dec 18 18:13:06 2014 UTC

# Line 1 | Line 1
1 /*
2 * @test
3 * @synopsis Exercise multithreaded maps, by default
4 * ConcurrentHashMap.  Each thread does a random walk though elements
5 * of "key" array. On each iteration, it checks if table includes key.
6 * If absent, with probablility pinsert it inserts it, and if present,
7 * with probablility premove it removes it.  (pinsert and premove are
8 * expressed as percentages to simplify parsing from command line.)
9 */
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3 < * Expert Group and released to the public domain. Use, modify, and
4 < * redistribute this code in any way without acknowledgement.
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
16
7   import java.util.*;
8   import java.util.concurrent.*;
9  
10   public class MapLoops {
11 <    static int nkeys       = 10000;
11 >    static int nkeys       = 1000;
12      static int pinsert     = 60;
13      static int premove     = 2;
14      static int maxThreads  = 100;
15 <    static int nops        = 8000000;
15 >    static int nops        = 1000000;
16      static int removesPerMaxRandom;
17      static int insertsPerMaxRandom;
18  
# Line 34 | Line 24 | public class MapLoops {
24          if (args.length > 0) {
25              try {
26                  mapClass = Class.forName(args[0]);
27 <            } catch(ClassNotFoundException e) {
27 >            } catch (ClassNotFoundException e) {
28                  throw new RuntimeException("Class " + args[0] + " not found.");
29              }
30          }
31 <        else
31 >        else
32              mapClass = java.util.concurrent.ConcurrentHashMap.class;
33  
34 <        if (args.length > 1)
34 >        if (args.length > 1)
35              maxThreads = Integer.parseInt(args[1]);
36  
37 <        if (args.length > 2)
37 >        if (args.length > 2)
38              nkeys = Integer.parseInt(args[2]);
39  
40 <        if (args.length > 3)
40 >        if (args.length > 3)
41              pinsert = Integer.parseInt(args[3]);
42  
43 <        if (args.length > 4)
43 >        if (args.length > 4)
44              premove = Integer.parseInt(args[4]);
45  
46 <        if (args.length > 5)
46 >        if (args.length > 5)
47              nops = Integer.parseInt(args[5]);
48  
49          // normalize probabilities wrt random number generator
50          removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL));
51          insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL));
52 <        
52 >
53          System.out.print("Class: " + mapClass.getName());
54          System.out.print(" threads: " + maxThreads);
55          System.out.print(" size: " + nkeys);
# Line 68 | Line 58 | public class MapLoops {
58          System.out.print(" ops: " + nops);
59          System.out.println();
60  
71        int k = 1;
61          int warmups = 2;
62 <        for (int i = 1; i <= maxThreads;) {
62 >        for (int k = 1, i = 1; i <= maxThreads;) {
63              Thread.sleep(100);
64              test(i, nkeys, mapClass);
65              if (warmups > 0)
# Line 78 | Line 67 | public class MapLoops {
67              else if (i == k) {
68                  k = i << 1;
69                  i = i + (i >>> 1);
70 <            }
70 >            }
71              else if (i == 1 && k == 2) {
72                  i = k;
73                  warmups = 1;
74              }
75 <            else
75 >            else
76                  i = k;
77          }
78          pool.shutdown();
# Line 92 | Line 81 | public class MapLoops {
81      static Integer[] makeKeys(int n) {
82          LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
83          Integer[] key = new Integer[n];
84 <        for (int i = 0; i < key.length; ++i)
84 >        for (int i = 0; i < key.length; ++i)
85              key[i] = new Integer(rng.next());
86          return key;
87      }
# Line 114 | Line 103 | public class MapLoops {
103          // Uncomment to start with a non-empty table
104          //        for (int j = 0; j < nkeys; j += 4) // start 1/4 occupied
105          //            map.put(key[j], key[j]);
106 +        shuffleKeys(key);
107          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
108          CyclicBarrier barrier = new CyclicBarrier(i+1, timer);
109 <        for (int t = 0; t < i; ++t)
110 <            pool.execute(new Runner(map, key, barrier));
109 >        for (int t = 0; t < i; ++t)
110 >            pool.execute(new Runner(t, map, key, barrier));
111          barrier.await();
112          barrier.await();
113          long time = timer.getTime();
114 <        long tpo = time / (i * (long)nops);
114 >        long tpo = time / (i * (long) nops);
115          System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
116 <        double secs = (double)(time) / 1000000000.0;
116 >        double secs = (double) time / 1000000000.0;
117          System.out.println("\t " + secs + "s run time");
118          map.clear();
119      }
# Line 131 | Line 121 | public class MapLoops {
121      static class Runner implements Runnable {
122          final Map<Integer,Integer> map;
123          final Integer[] key;
124 <        final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
124 >        final LoopHelpers.SimpleRandom rng;
125          final CyclicBarrier barrier;
126          int position;
127          int total;
128  
129 <        Runner(Map<Integer,Integer> map, Integer[] key,  CyclicBarrier barrier) {
130 <            this.map = map;
131 <            this.key = key;
129 >        Runner(int id, Map<Integer,Integer> map, Integer[] key, CyclicBarrier barrier) {
130 >            this.map = map;
131 >            this.key = key;
132              this.barrier = barrier;
133              position = key.length / 2;
134 +            rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
135 +            rng.next();
136          }
137  
138          int step() {
139 <            // random-walk around key positions,  bunching accesses
139 >            // random-walk around key positions, bunching accesses
140              int r = rng.next();
141              position += (r & 7) - 3;
142 <            while (position >= key.length) position -= key.length;  
142 >            while (position >= key.length) position -= key.length;
143              while (position < 0) position += key.length;
144  
145              Integer k = key[position];
146              Integer x = map.get(k);
147  
148              if (x != null) {
149 <                if (x.intValue() != k.intValue())
149 >                if (x.intValue() != k.intValue())
150                      throw new Error("bad mapping: " + x + " to " + k);
151  
152                  if (r < removesPerMaxRandom) {
# Line 168 | Line 160 | public class MapLoops {
160                  ++position;
161                  map.put(k, k);
162                  return 2;
163 <            }
163 >            }
164  
165              // Uncomment to add a little computation between accesses
166              //            total += LoopHelpers.compute1(k.intValue());
# Line 180 | Line 172 | public class MapLoops {
172              try {
173                  barrier.await();
174                  int ops = nops;
175 <                while (ops > 0)
175 >                while (ops > 0)
176                      ops -= step();
177                  barrier.await();
178              }
# Line 190 | Line 182 | public class MapLoops {
182          }
183      }
184   }
193

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines