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

Comparing jsr166/src/test/loops/IntMapCheck.java (file contents):
Revision 1.3 by dl, Fri Oct 23 19:57:06 2009 UTC vs.
Revision 1.14 by jsr166, Thu Jan 15 18:34:19 2015 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   * @test
8   * @synopsis Times and checks basic map operations
9   */
10 import java.util.*;
10   import java.io.*;
11 + import java.util.*;
12  
13   public class IntMapCheck {
14      static int absentSize;
# Line 22 | Line 22 | public class IntMapCheck {
22      }
23  
24      public static void main(String[] args) throws Exception {
25 <        Class mapClass = java.util.concurrent.ConcurrentHashMap.class;
25 >        Class<?> mapClass = java.util.concurrent.ConcurrentHashMap.class;
26          int numTests = 50;
27          int size = 75000;
28  
29          if (args.length > 0) {
30              try {
31                  mapClass = Class.forName(args[0]);
32 <            } catch(ClassNotFoundException e) {
32 >            } catch (ClassNotFoundException e) {
33                  throw new RuntimeException("Class " + args[0] + " not found.");
34              }
35          }
36  
37 <
38 <        if (args.length > 1)
37 >        if (args.length > 1)
38              numTests = Integer.parseInt(args[1]);
39  
40 <        if (args.length > 2)
40 >        if (args.length > 2)
41              size = Integer.parseInt(args[2]);
42  
43          boolean doSerializeTest = args.length > 3;
# Line 49 | Line 48 | public class IntMapCheck {
48          while (absentSize < size) absentSize <<= 1;
49          absentMask = absentSize-1;
50          absent = new Integer[absentSize];
51 <        for (int i = 0; i < absentSize/2; ++i)
51 >        for (int i = 0; i < absentSize/2; ++i)
52              absent[i] = Integer.valueOf(-i - 1);
53 <        for (int i = absentSize/2; i < absentSize; ++i)
53 >        for (int i = absentSize/2; i < absentSize; ++i)
54              absent[i] = Integer.valueOf(size + i + 1);
55  
56          Integer[] key = new Integer[size];
57 <        for (int i = 0; i < size; ++i)
57 >        for (int i = 0; i < size; ++i)
58              key[i] = Integer.valueOf(i);
59  
60          for (int rep = 0; rep < numTests; ++rep) {
# Line 68 | Line 67 | public class IntMapCheck {
67  
68          TestTimer.printStats();
69  
71
70          if (doSerializeTest)
71              stest(newMap(mapClass), size);
72      }
73  
74 <    static Map<Integer,Integer> newMap(Class cl) {
74 >    static Map<Integer,Integer> newMap(Class<?> cl) {
75          try {
76              Map m = (Map<Integer,Integer>)cl.newInstance();
77              return m;
78 <        } catch(Exception e) {
78 >        } catch (Exception e) {
79              throw new RuntimeException("Can't instantiate " + cl + ": " + e);
80          }
81      }
82  
85
83      static void runTest(Map<Integer,Integer> s, Integer[] key) {
84          int size = key.length;
85          long startTime = System.nanoTime();
# Line 98 | Line 95 | public class IntMapCheck {
95                  if (s.get(key[i]) != null) ++sum;
96              }
97          }
98 <        timer.finish();
99 <        reallyAssert (sum == expect * iters);
98 >        timer.finish();
99 >        reallyAssert(sum == expect * iters);
100      }
101  
102      static void t1Boxed(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) {
# Line 108 | Line 105 | public class IntMapCheck {
105          timer.start(nm, n * iters);
106          for (int j = 0; j < iters; ++j) {
107              for (int i = 0; i < n; i++) {
108 <                if ((Integer)(s.get(i)) != i) ++sum;
108 >                if (s.get(i) != i) ++sum;
109              }
110          }
111 <        timer.finish();
112 <        reallyAssert (sum == expect * iters);
111 >        timer.finish();
112 >        reallyAssert(sum == expect * iters);
113      }
114  
118
115      static void t2(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) {
116          int sum = 0;
117          timer.start(nm, n);
118          for (int i = 0; i < n; i++) {
119              if (s.remove(key[i]) != null) ++sum;
120          }
121 <        timer.finish();
122 <        reallyAssert (sum == expect);
121 >        timer.finish();
122 >        reallyAssert(sum == expect);
123      }
124  
125      static void t3(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) {
# Line 134 | Line 130 | public class IntMapCheck {
130              Integer v = absent[i & absentMask];
131              if (s.put(k, v) == null) ++sum;
132          }
133 <        timer.finish();
134 <        reallyAssert (sum == expect);
133 >        timer.finish();
134 >        reallyAssert(sum == expect);
135      }
136  
137      static void t4(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) {
# Line 144 | Line 140 | public class IntMapCheck {
140          for (int i = 0; i < n; i++) {
141              if (s.containsKey(key[i])) ++sum;
142          }
143 <        timer.finish();
144 <        reallyAssert (sum == expect);
143 >        timer.finish();
144 >        reallyAssert(sum == expect);
145      }
146  
147      static void t5(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) {
# Line 154 | Line 150 | public class IntMapCheck {
150          for (int i = n-2; i >= 0; i-=2) {
151              if (s.remove(key[i]) != null) ++sum;
152          }
153 <        timer.finish();
154 <        reallyAssert (sum == expect);
153 >        timer.finish();
154 >        reallyAssert(sum == expect);
155      }
156  
157      static void t6(String nm, int n, Map<Integer,Integer> s, Integer[] k1, Integer[] k2) {
# Line 165 | Line 161 | public class IntMapCheck {
161              if (s.get(k1[i]) != null) ++sum;
162              if (s.get(k2[i & absentMask]) != null) ++sum;
163          }
164 <        timer.finish();
165 <        reallyAssert (sum == n);
164 >        timer.finish();
165 >        reallyAssert(sum == n);
166      }
167  
168      static void t7(String nm, int n, Map<Integer,Integer> s, Integer[] k1, Integer[] k2) {
# Line 176 | Line 172 | public class IntMapCheck {
172              if (s.containsKey(k1[i])) ++sum;
173              if (s.containsKey(k2[i & absentMask])) ++sum;
174          }
175 <        timer.finish();
176 <        reallyAssert (sum == n);
175 >        timer.finish();
176 >        reallyAssert(sum == n);
177      }
178  
179      static void t8(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) {
# Line 186 | Line 182 | public class IntMapCheck {
182          for (int i = 0; i < n; i++) {
183              if (s.get(key[i]) != null) ++sum;
184          }
185 <        timer.finish();
186 <        reallyAssert (sum == expect);
185 >        timer.finish();
186 >        reallyAssert(sum == expect);
187      }
188  
193
189      static void t9(Map<Integer,Integer> s) {
190          int sum = 0;
191          int iters = 20;
# Line 198 | Line 193 | public class IntMapCheck {
193          int step = absentSize / iters;
194          for (int i = 0; i < absentSize; i += step)
195              if (s.containsValue(absent[i])) ++sum;
196 <        timer.finish();
197 <        reallyAssert (sum != 0);
196 >        timer.finish();
197 >        reallyAssert(sum != 0);
198      }
199  
205
200      static void ktest(Map<Integer,Integer> s, int size, Integer[] key) {
201          timer.start("ContainsKey            ", size);
202          Set ks = s.keySet();
# Line 210 | Line 204 | public class IntMapCheck {
204          for (int i = 0; i < size; i++) {
205              if (ks.contains(key[i])) ++sum;
206          }
207 <        timer.finish();
208 <        reallyAssert (sum == size);
207 >        timer.finish();
208 >        reallyAssert(sum == size);
209      }
210  
217
211      static void ittest1(Map<Integer,Integer> s, int size) {
212          int sum = 0;
213          timer.start("Iter Key               ", size);
214          for (Iterator it = s.keySet().iterator(); it.hasNext(); ) {
215 <            if(it.next() != MISSING)
215 >            if (it.next() != MISSING)
216                  ++sum;
217          }
218 <        timer.finish();
219 <        //        if (sum != size)
218 >        timer.finish();
219 >        //        if (sum != size)
220          //            System.out.println("iters " + sum + " size " + size);
221 <        reallyAssert (sum == size);
221 >        reallyAssert(sum == size);
222      }
223  
224      static void ittest2(Map<Integer,Integer> s, int size) {
225          int sum = 0;
226          timer.start("Iter Value             ", size);
227          for (Iterator it = s.values().iterator(); it.hasNext(); ) {
228 <            if(it.next() != MISSING)
228 >            if (it.next() != MISSING)
229                  ++sum;
230          }
231 <        timer.finish();
232 <        //        if (sum != size)
231 >        timer.finish();
232 >        //        if (sum != size)
233          //            System.out.println("iters " + sum + " size " + size);
234 <        reallyAssert (sum == size);
234 >        reallyAssert(sum == size);
235      }
236      static void ittest3(Map<Integer,Integer> s, int size) {
237          int sum = 0;
238          timer.start("Iter Entry             ", size);
239          for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) {
240 <            if(it.next() != MISSING)
240 >            if (it.next() != MISSING)
241                  ++sum;
242          }
243 <        timer.finish();
244 <        reallyAssert (sum == size);
243 >        timer.finish();
244 >        reallyAssert(sum == size);
245      }
246  
247      static void ittest4(Map<Integer,Integer> s, int size, int pos) {
248          IdentityHashMap seen = new IdentityHashMap(size);
249 <        reallyAssert (s.size() == size);
249 >        reallyAssert(s.size() == size);
250          int sum = 0;
251          timer.start("Iter XEntry            ", size);
252 <        Iterator it = s.entrySet().iterator();
252 >        Iterator it = s.entrySet().iterator();
253          Integer k = null;
254          Integer v = null;
255          for (int i = 0; i < size-pos; ++i) {
# Line 267 | Line 260 | public class IntMapCheck {
260              if (v != MISSING)
261                  ++sum;
262          }
263 <        reallyAssert (s.containsKey(k));
263 >        reallyAssert(s.containsKey(k));
264          it.remove();
265 <        reallyAssert (!s.containsKey(k));
265 >        reallyAssert(!s.containsKey(k));
266          while (it.hasNext()) {
267              Map.Entry<Integer,Integer> x = (Map.Entry<Integer,Integer>)(it.next());
268 <            Integer k2 = (Integer)x.getKey();
268 >            Integer k2 = x.getKey();
269              seen.put(k2, k2);
270              if (k2 != MISSING)
271                  ++sum;
272          }
273  
274 <        reallyAssert (s.size() == size-1);
274 >        reallyAssert(s.size() == size-1);
275          s.put(k, v);
276 <        reallyAssert (seen.size() == size);
277 <        timer.finish();
278 <        reallyAssert (sum == size);
279 <        reallyAssert (s.size() == size);
276 >        reallyAssert(seen.size() == size);
277 >        timer.finish();
278 >        reallyAssert(sum == size);
279 >        reallyAssert(s.size() == size);
280      }
281  
289
282      static void ittest(Map<Integer,Integer> s, int size) {
283          for (int i = 0; i < 4; ++i) {
284              ittest1(s, size);
285              ittest2(s, size);
286              ittest3(s, size);
287          }
288 <        //        for (int i = 0; i < size-1; ++i)
288 >        //        for (int i = 0; i < size-1; ++i)
289          //            ittest4(s, size, i);
290      }
291  
# Line 305 | Line 297 | public class IntMapCheck {
297              if (en.nextElement() != MISSING)
298                  ++sum;
299          }
300 <        timer.finish();
301 <        reallyAssert (sum == size);
300 >        timer.finish();
301 >        reallyAssert(sum == size);
302      }
303  
304      static void entest2(Hashtable ht, int size) {
# Line 316 | Line 308 | public class IntMapCheck {
308              if (en.nextElement() != MISSING)
309                  ++sum;
310          }
311 <        timer.finish();
312 <        reallyAssert (sum == size);
311 >        timer.finish();
312 >        reallyAssert(sum == size);
313      }
314  
323
315      static void entest3(Hashtable ht, int size) {
316          int sum = 0;
317  
318          timer.start("Iterf Enumeration Key  ", size);
319 <        Enumeration en = ht.keys();
319 >        Enumeration en = ht.keys();
320          for (int i = 0; i < size; ++i) {
321              if (en.nextElement() != MISSING)
322                  ++sum;
323          }
324 <        timer.finish();
325 <        reallyAssert (sum == size);
324 >        timer.finish();
325 >        reallyAssert(sum == size);
326      }
327  
328      static void entest4(Hashtable ht, int size) {
329          int sum = 0;
330          timer.start("Iterf Enumeration Value", size);
331 <        Enumeration en = ht.elements();
331 >        Enumeration en = ht.elements();
332          for (int i = 0; i < size; ++i) {
333              if (en.nextElement() != MISSING)
334                  ++sum;
335          }
336 <        timer.finish();
337 <        reallyAssert (sum == size);
336 >        timer.finish();
337 >        reallyAssert(sum == size);
338      }
339  
340      static void entest(Map<Integer,Integer> s, int size) {
341          if (s instanceof Hashtable) {
342 <            Hashtable ht = (Hashtable)s;
342 >            Hashtable ht = (Hashtable) s;
343              //            entest3(ht, size);
344              //            entest4(ht, size);
345              entest1(ht, size);
# Line 367 | Line 358 | public class IntMapCheck {
358              it.remove();
359          }
360          reallyAssert(s.isEmpty());
361 <        timer.finish();
361 >        timer.finish();
362      }
363  
364      static void stest(Map<Integer,Integer> s, int size) throws Exception {
365 <        if (!(s instanceof Serializable))
365 >        if (!(s instanceof Serializable))
366              return;
367          System.out.print("Serialize              : ");
368 <      
368 >
369          for (int i = 0; i < size; i++) {
370              s.put(Integer.valueOf(i), Integer.valueOf(1));
371          }
# Line 396 | Line 387 | public class IntMapCheck {
387          System.out.print(time + "ms");
388  
389          if (s instanceof IdentityHashMap) return;
390 <        reallyAssert (s.equals(m));
390 >        reallyAssert(s.equals(m));
391      }
392  
402    
393      static void test(Map<Integer,Integer> s, Integer[] key) {
394          int size = key.length;
395  
# Line 433 | Line 423 | public class IntMapCheck {
423          reallyAssert(s.size() == size);
424          timer.start("Clear                  ", size);
425          s.clear();
426 <        timer.finish();
426 >        timer.finish();
427          t1("Get (absent)           ", size, s, key, 0, 1);
428          t4("ContainsKey            ", size, s, key, 0);
429          t2("Remove (absent)        ", size, s, key, 0);
# Line 445 | Line 435 | public class IntMapCheck {
435          reallyAssert(s.size() == 0);
436          timer.start("Clear                  ", size);
437          s.clear();
438 <        timer.finish();
438 >        timer.finish();
439          t3("Put (presized)         ", size, s, key, size);
440  
441          timer.start("Put (putAll)           ", size * 2);
# Line 455 | Line 445 | public class IntMapCheck {
445              s2.putAll(s);
446          }
447          catch (Exception e) { e.printStackTrace(); return; }
448 <        timer.finish();
449 <    
448 >        timer.finish();
449 >
450          timer.start("Iter Equals            ", size * 2);
451          boolean eqt = s2.equals(s) && s.equals(s2);
452 <        reallyAssert (eqt);
453 <        timer.finish();
452 >        reallyAssert(eqt);
453 >        timer.finish();
454  
455          timer.start("Iter HashCode          ", size * 2);
456          int shc = s.hashCode();
457          int s2hc = s2.hashCode();
458 <        reallyAssert (shc == s2hc);
459 <        timer.finish();
458 >        reallyAssert(shc == s2hc);
459 >        timer.finish();
460  
461          timer.start("Put (present)          ", size * 2);
462          s2.putAll(s);
463 <        timer.finish();
463 >        timer.finish();
464  
465          timer.start("Put (present)          ", size);
466          int ipsum = 0;
# Line 479 | Line 469 | public class IntMapCheck {
469              if (s2.put(me.getKey(), me.getValue()) != null)
470                  ++ipsum;
471          }
472 <        reallyAssert (ipsum == s.size());
473 <        timer.finish();
472 >        reallyAssert(ipsum == s.size());
473 >        timer.finish();
474  
475          timer.start("Iter EntrySet contains ", size * 2);
476          Set es2 = s2.entrySet();
# Line 489 | Line 479 | public class IntMapCheck {
479              Object entry = i1.next();
480              if (es2.contains(entry)) ++sum;
481          }
482 <        timer.finish();
483 <        reallyAssert (sum == size);
482 >        timer.finish();
483 >        reallyAssert(sum == size);
484  
485          Integer hold = s2.get(key[size-1]);
486          s2.put(key[size-1], absent[0]);
487          timer.start("Iter Equals            ", size * 2);
488          eqt = s2.equals(s) && s.equals(s2);
489 <        reallyAssert (!eqt);
490 <        timer.finish();
489 >        reallyAssert(!eqt);
490 >        timer.finish();
491  
492          timer.start("Iter HashCode          ", size * 2);
493          int s1h = s.hashCode();
494          int s2h = s2.hashCode();
495 <        reallyAssert (s1h != s2h);
496 <        timer.finish();
495 >        reallyAssert(s1h != s2h);
496 >        timer.finish();
497  
498          s2.put(key[size-1], hold);
499          timer.start("Remove (present)       ", size * 2);
# Line 511 | Line 501 | public class IntMapCheck {
501          Set es = s.entrySet();
502          while (s2i.hasNext())
503              reallyAssert(es.remove(s2i.next()));
504 <        timer.finish();
504 >        timer.finish();
505  
506 <        reallyAssert (s.isEmpty());
506 >        reallyAssert(s.isEmpty());
507  
508          timer.start("Clear                  ", size);
509          s2.clear();
510 <        timer.finish();
511 <        reallyAssert (s2.isEmpty() && s.isEmpty());
510 >        timer.finish();
511 >        reallyAssert(s2.isEmpty() && s.isEmpty());
512      }
513  
514      static class TestTimer {
# Line 528 | Line 518 | public class IntMapCheck {
518          private String cname;
519  
520          static final java.util.TreeMap accum = new java.util.TreeMap();
521 <    
521 >
522          static void printStats() {
523              for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) {
524                  Map.Entry e = (Map.Entry)(it.next());
525 <                Stats stats = ((Stats)(e.getValue()));
525 >                Stats stats = ((Stats) (e.getValue()));
526                  long n = stats.number;
527                  double t;
528 <                if (n > 0)
528 >                if (n > 0)
529                      t = stats.sum / n;
530                  else
531                      t = stats.least;
# Line 543 | Line 533 | public class IntMapCheck {
533                  System.out.println(e.getKey() + ": " + nano);
534              }
535          }
536 <    
536 >
537          void start(String name, long numOps) {
538              this.name = name;
539              this.cname = classify();
540              this.numOps = numOps;
541              startTime = System.nanoTime();
542          }
553    
543  
544          String classify() {
545              if (name.startsWith("Get"))
# Line 561 | Line 550 | public class IntMapCheck {
550                  return "Remove                 ";
551              else if (name.startsWith("Iter"))
552                  return "Iter                   ";
553 <            else
553 >            else
554                  return null;
555          }
556  
557          void finish() {
558              long endTime = System.nanoTime();
559              long time = endTime - startTime;
560 <            double timePerOp = ((double)time)/numOps;
560 >            double timePerOp = ((double) time)/numOps;
561  
562              Object st = accum.get(name);
563              if (st == null)
# Line 607 | Line 596 | public class IntMapCheck {
596  
597      static void shuffle(Integer[] keys) {
598          int size = keys.length;
599 <        for (int i=size; i>1; i--) {
599 >        for (int i = size; i > 1; i--) {
600              int r = rng.nextInt(i);
601              Integer t = keys[i-1];
602              keys[i-1] = keys[r];
# Line 616 | Line 605 | public class IntMapCheck {
605      }
606  
607   }
619

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines