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.2 by dl, Mon Feb 19 00:46:06 2007 UTC vs.
Revision 1.8 by jsr166, Mon Dec 5 04:08:46 2011 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       = 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 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 71 | Line 71 | public class StringMapLoops {
71              else if (i == k) {
72                  k = i << 1;
73                  i = i + (i >>> 1);
74 <            }
74 >            }
75              else if (i == 1 && k == 2) {
76                  i = k;
77                  warmups = 1;
78              }
79 <            else
79 >            else
80                  i = k;
81          }
82 +        for (int j = 0; j < 10; ++j) {
83 +            Thread.sleep(100);
84 +            test(1, nkeys, key, mapClass);
85 +            //            shuffleKeys(key);
86 +        }
87          pool.shutdown();
88      }
89  
# Line 91 | Line 96 | public class StringMapLoops {
96              char[] c = new char[len * 4];
97              for (int j = 0; j < len; ++j) {
98                  int r = rng.next();
99 <                c[k++] = (char)(' ' + (r & 0x7f));
99 >                c[k++] = (char) (' ' + (r & 0x7f));
100                  r >>>= 8;
101 <                c[k++] = (char)(' ' + (r & 0x7f));
101 >                c[k++] = (char) (' ' + (r & 0x7f));
102                  r >>>= 8;
103 <                c[k++] = (char)(' ' + (r & 0x7f));
103 >                c[k++] = (char) (' ' + (r & 0x7f));
104                  r >>>= 8;
105 <                c[k++] = (char)(' ' + (r & 0x7f));
105 >                c[k++] = (char) (' ' + (r & 0x7f));
106              }
107              key[i] = new String(c);
108          }
# Line 120 | Line 125 | public class StringMapLoops {
125          // Uncomment to start with a non-empty table
126          //        for (int j = 0; j < nkeys; j += 4) // start 1/4 occupied
127          //            map.put(key[j], key[j]);
128 +
129          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
130          CyclicBarrier barrier = new CyclicBarrier(i+1, timer);
131 <        for (int t = 0; t < i; ++t)
131 >        for (int t = 0; t < i; ++t)
132              pool.execute(new Runner(t, map, key, barrier));
133          barrier.await();
134          barrier.await();
135          long time = timer.getTime();
136 <        long tpo = time / (i * (long)nops);
136 >        long tpo = time / (i * (long) nops);
137          System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
138 <        double secs = (double)(time) / 1000000000.0;
138 >        double secs = (double) time / 1000000000.0;
139          System.out.println("\t " + secs + "s run time");
140          map.clear();
141      }
# Line 142 | Line 148 | public class StringMapLoops {
148          int position;
149          int total;
150  
151 <        Runner(int id, Map<String,String> map, String[] key,  CyclicBarrier barrier) {
152 <            this.map = map;
153 <            this.key = key;
151 >        Runner(int id, Map<String,String> map, String[] key, CyclicBarrier barrier) {
152 >            this.map = map;
153 >            this.key = key;
154              this.barrier = barrier;
155 <            position = key.length / 2;
155 >            position = key.length / 2;
156              rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
157              rng.next();
158          }
# Line 155 | Line 161 | public class StringMapLoops {
161              // random-walk around key positions,  bunching accesses
162              int r = rng.next();
163              position += (r & 7) - 3;
164 <            while (position >= key.length) position -= key.length;  
164 >            while (position >= key.length) position -= key.length;
165              while (position < 0) position += key.length;
166  
167              String k = key[position];
# Line 173 | Line 179 | public class StringMapLoops {
179                  ++position;
180                  map.put(k, k);
181                  return 2;
182 <            }
182 >            }
183  
184              total += r;
185              return 1;
# Line 183 | Line 189 | public class StringMapLoops {
189              try {
190                  barrier.await();
191                  int ops = nops;
192 <                while (ops > 0)
192 >                while (ops > 0)
193                      ops -= step();
194                  barrier.await();
195              }
# Line 193 | Line 199 | public class StringMapLoops {
199          }
200      }
201   }
196

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines