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

Comparing jsr166/src/test/loops/StringMapLoops.java (file contents):
Revision 1.1 by dl, Sun Aug 7 19:25:55 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 StringMapLoops {
11 <    static int nkeys       = 1000;
11 >    static int nkeys       = 75000;
12      static int pinsert     = 60;
13 <    static int premove     = 2;
13 >    static int premove     =  2;
14      static int maxThreads  = 100;
15 <    static int nops        = 1000000;
15 >    static int nops        = 8000000;
16      static int removesPerMaxRandom;
17      static int insertsPerMaxRandom;
18  
# Line 34 | Line 24 | public class StringMapLoops {
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 70 | Line 60 | public class StringMapLoops {
60  
61          String[] key = makeKeys(nkeys);
62  
73        int k = 1;
63          int warmups = 2;
64 <        for (int i = 1; i <= maxThreads;) {
64 >        for (int k = 1, i = 1; i <= maxThreads;) {
65              Thread.sleep(100);
66              test(i, nkeys, key, mapClass);
67              shuffleKeys(key);
# Line 81 | Line 70 | public class StringMapLoops {
70              else if (i == k) {
71                  k = i << 1;
72                  i = i + (i >>> 1);
73 <            }
73 >            }
74              else if (i == 1 && k == 2) {
75                  i = k;
76                  warmups = 1;
77              }
78 <            else
78 >            else
79                  i = k;
80          }
81 +        for (int j = 0; j < 10; ++j) {
82 +            Thread.sleep(100);
83 +            test(1, nkeys, key, mapClass);
84 +            //            shuffleKeys(key);
85 +        }
86          pool.shutdown();
87      }
88  
# Line 101 | Line 95 | public class StringMapLoops {
95              char[] c = new char[len * 4];
96              for (int j = 0; j < len; ++j) {
97                  int r = rng.next();
98 <                c[k++] = (char)(' ' + (r & 0x7f));
98 >                c[k++] = (char) (' ' + (r & 0x7f));
99                  r >>>= 8;
100 <                c[k++] = (char)(' ' + (r & 0x7f));
100 >                c[k++] = (char) (' ' + (r & 0x7f));
101                  r >>>= 8;
102 <                c[k++] = (char)(' ' + (r & 0x7f));
102 >                c[k++] = (char) (' ' + (r & 0x7f));
103                  r >>>= 8;
104 <                c[k++] = (char)(' ' + (r & 0x7f));
104 >                c[k++] = (char) (' ' + (r & 0x7f));
105              }
106              key[i] = new String(c);
107          }
# Line 130 | Line 124 | public class StringMapLoops {
124          // Uncomment to start with a non-empty table
125          //        for (int j = 0; j < nkeys; j += 4) // start 1/4 occupied
126          //            map.put(key[j], key[j]);
127 +
128          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
129          CyclicBarrier barrier = new CyclicBarrier(i+1, timer);
130 <        for (int t = 0; t < i; ++t)
130 >        for (int t = 0; t < i; ++t)
131              pool.execute(new Runner(t, map, key, barrier));
132          barrier.await();
133          barrier.await();
134          long time = timer.getTime();
135 <        long tpo = time / (i * (long)nops);
135 >        long tpo = time / (i * (long) nops);
136          System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
137 <        double secs = (double)(time) / 1000000000.0;
137 >        double secs = (double) time / 1000000000.0;
138          System.out.println("\t " + secs + "s run time");
139          map.clear();
140      }
# Line 152 | Line 147 | public class StringMapLoops {
147          int position;
148          int total;
149  
150 <        Runner(int id, Map<String,String> map, String[] key,  CyclicBarrier barrier) {
151 <            this.map = map;
152 <            this.key = key;
150 >        Runner(int id, Map<String,String> map, String[] key, CyclicBarrier barrier) {
151 >            this.map = map;
152 >            this.key = key;
153              this.barrier = barrier;
154 <            position = key.length / 2;
154 >            position = key.length / 2;
155              rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
156              rng.next();
157          }
158  
159          int step() {
160 <            // random-walk around key positions,  bunching accesses
160 >            // random-walk around key positions, bunching accesses
161              int r = rng.next();
162              position += (r & 7) - 3;
163 <            while (position >= key.length) position -= key.length;  
163 >            while (position >= key.length) position -= key.length;
164              while (position < 0) position += key.length;
165  
166              String k = key[position];
# Line 183 | Line 178 | public class StringMapLoops {
178                  ++position;
179                  map.put(k, k);
180                  return 2;
181 <            }
181 >            }
182  
183              total += r;
184              return 1;
# Line 193 | Line 188 | public class StringMapLoops {
188              try {
189                  barrier.await();
190                  int ops = nops;
191 <                while (ops > 0)
191 >                while (ops > 0)
192                      ops -= step();
193                  barrier.await();
194              }
# Line 203 | Line 198 | public class StringMapLoops {
198          }
199      }
200   }
206

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines