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.3 by dl, Fri Oct 23 19:57:07 2009 UTC vs.
Revision 1.10 by jsr166, Thu Dec 18 18:13:06 2014 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   import java.util.*;
8   import java.util.concurrent.*;
9  
10   public class StringMapLoops {
11 <    static int nkeys       = 75000;
11 >    static int nkeys       = 75000;
12      static int pinsert     = 60;
13      static int premove     =  2;
14      static int maxThreads  = 100;
# Line 24 | 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 60 | Line 60 | public class StringMapLoops {
60  
61          String[] key = makeKeys(nkeys);
62  
63        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 71 | 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) {
# Line 96 | 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 128 | Line 127 | public class StringMapLoops {
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 148 | 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 179 | 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 189 | 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 199 | Line 198 | public class StringMapLoops {
198          }
199      }
200   }
202

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines