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.2 by dl, Mon May 9 19:33:30 2005 UTC vs.
Revision 1.6 by jsr166, Mon Nov 2 23:51:32 2009 UTC

# Line 24 | Line 24 | public class IntMapCheck {
24      public static void main(String[] args) throws Exception {
25          Class mapClass = java.util.concurrent.ConcurrentHashMap.class;
26          int numTests = 50;
27 <        int size = 50000;
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)
38 >        if (args.length > 1)
39              numTests = Integer.parseInt(args[1]);
40  
41 <        if (args.length > 2)
41 >        if (args.length > 2)
42              size = Integer.parseInt(args[2]);
43  
44          boolean doSerializeTest = args.length > 3;
# Line 46 | Line 46 | public class IntMapCheck {
46          System.out.println("Testing " + mapClass.getName() + " trials: " + numTests + " size: " + size);
47  
48          absentSize = 4;
49 <        while (absentSize <= size) absentSize <<= 1;
49 >        while (absentSize < size) absentSize <<= 1;
50          absentMask = absentSize-1;
51          absent = new Integer[absentSize];
52 <        for (int i = 0; i < absentSize; ++i)
53 <            absent[i] = new Integer(2 * (i - 1) + 1);
52 >        for (int i = 0; i < absentSize/2; ++i)
53 >            absent[i] = Integer.valueOf(-i - 1);
54 >        for (int i = absentSize/2; i < absentSize; ++i)
55 >            absent[i] = Integer.valueOf(size + i + 1);
56  
57          Integer[] key = new Integer[size];
58 <        for (int i = 0; i < size; ++i)
59 <            key[i] = new Integer(2 * i);
58 >        for (int i = 0; i < size; ++i)
59 >            key[i] = Integer.valueOf(i);
60  
61          for (int rep = 0; rep < numTests; ++rep) {
60            shuffle(absent);
62              runTest(newMap(mapClass), key);
63 +            if ((rep & 3) == 3 && rep < numTests - 1) {
64 +                shuffle(key);
65 +                //                Thread.sleep(50);
66 +            }
67          }
68  
69          TestTimer.printStats();
# Line 72 | Line 77 | public class IntMapCheck {
77          try {
78              Map m = (Map<Integer,Integer>)cl.newInstance();
79              return m;
80 <        } catch(Exception e) {
80 >        } catch (Exception e) {
81              throw new RuntimeException("Can't instantiate " + cl + ": " + e);
82          }
83      }
84  
85  
86      static void runTest(Map<Integer,Integer> s, Integer[] key) {
82        shuffle(key);
87          int size = key.length;
88          long startTime = System.nanoTime();
89          test(s, key);
90          long time = System.nanoTime() - startTime;
91      }
92  
93 <
90 <    static void t1(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) {
93 >    static void t1(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect, int iters) {
94          int sum = 0;
92        int iters = 4;
95          timer.start(nm, n * iters);
96          for (int j = 0; j < iters; ++j) {
97              for (int i = 0; i < n; i++) {
98                  if (s.get(key[i]) != null) ++sum;
99              }
100          }
101 <        timer.finish();
101 >        timer.finish();
102 >        reallyAssert (sum == expect * iters);
103 >    }
104 >
105 >    static void t1Boxed(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) {
106 >        int sum = 0;
107 >        int iters = 8;
108 >        timer.start(nm, n * iters);
109 >        for (int j = 0; j < iters; ++j) {
110 >            for (int i = 0; i < n; i++) {
111 >                if ((Integer) (s.get(i)) != i) ++sum;
112 >            }
113 >        }
114 >        timer.finish();
115          reallyAssert (sum == expect * iters);
116      }
117  
118 +
119      static void t2(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) {
120          int sum = 0;
121          timer.start(nm, n);
122          for (int i = 0; i < n; i++) {
123              if (s.remove(key[i]) != null) ++sum;
124          }
125 <        timer.finish();
125 >        timer.finish();
126          reallyAssert (sum == expect);
127      }
128  
# Line 118 | Line 134 | public class IntMapCheck {
134              Integer v = absent[i & absentMask];
135              if (s.put(k, v) == null) ++sum;
136          }
137 <        timer.finish();
137 >        timer.finish();
138          reallyAssert (sum == expect);
139      }
140  
# Line 128 | Line 144 | public class IntMapCheck {
144          for (int i = 0; i < n; i++) {
145              if (s.containsKey(key[i])) ++sum;
146          }
147 <        timer.finish();
147 >        timer.finish();
148          reallyAssert (sum == expect);
149      }
150  
# Line 138 | Line 154 | public class IntMapCheck {
154          for (int i = n-2; i >= 0; i-=2) {
155              if (s.remove(key[i]) != null) ++sum;
156          }
157 <        timer.finish();
157 >        timer.finish();
158          reallyAssert (sum == expect);
159      }
160  
# Line 149 | Line 165 | public class IntMapCheck {
165              if (s.get(k1[i]) != null) ++sum;
166              if (s.get(k2[i & absentMask]) != null) ++sum;
167          }
168 <        timer.finish();
168 >        timer.finish();
169          reallyAssert (sum == n);
170      }
171  
# Line 160 | Line 176 | public class IntMapCheck {
176              if (s.containsKey(k1[i])) ++sum;
177              if (s.containsKey(k2[i & absentMask])) ++sum;
178          }
179 <        timer.finish();
179 >        timer.finish();
180          reallyAssert (sum == n);
181      }
182  
# Line 170 | Line 186 | public class IntMapCheck {
186          for (int i = 0; i < n; i++) {
187              if (s.get(key[i]) != null) ++sum;
188          }
189 <        timer.finish();
189 >        timer.finish();
190          reallyAssert (sum == expect);
191      }
192  
# Line 182 | Line 198 | public class IntMapCheck {
198          int step = absentSize / iters;
199          for (int i = 0; i < absentSize; i += step)
200              if (s.containsValue(absent[i])) ++sum;
201 <        timer.finish();
201 >        timer.finish();
202          reallyAssert (sum != 0);
203      }
204  
# Line 194 | Line 210 | public class IntMapCheck {
210          for (int i = 0; i < size; i++) {
211              if (ks.contains(key[i])) ++sum;
212          }
213 <        timer.finish();
213 >        timer.finish();
214          reallyAssert (sum == size);
215      }
216  
# Line 203 | Line 219 | public class IntMapCheck {
219          int sum = 0;
220          timer.start("Iter Key               ", size);
221          for (Iterator it = s.keySet().iterator(); it.hasNext(); ) {
222 <            if(it.next() != MISSING)
222 >            if (it.next() != MISSING)
223                  ++sum;
224          }
225 <        timer.finish();
225 >        timer.finish();
226 >        //        if (sum != size)
227 >        //            System.out.println("iters " + sum + " size " + size);
228          reallyAssert (sum == size);
229      }
230  
# Line 214 | Line 232 | public class IntMapCheck {
232          int sum = 0;
233          timer.start("Iter Value             ", size);
234          for (Iterator it = s.values().iterator(); it.hasNext(); ) {
235 <            if(it.next() != MISSING)
235 >            if (it.next() != MISSING)
236                  ++sum;
237          }
238 <        timer.finish();
238 >        timer.finish();
239 >        //        if (sum != size)
240 >        //            System.out.println("iters " + sum + " size " + size);
241          reallyAssert (sum == size);
242      }
243      static void ittest3(Map<Integer,Integer> s, int size) {
244          int sum = 0;
245          timer.start("Iter Entry             ", size);
246          for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) {
247 <            if(it.next() != MISSING)
247 >            if (it.next() != MISSING)
248                  ++sum;
249          }
250 <        timer.finish();
250 >        timer.finish();
251          reallyAssert (sum == size);
252      }
253  
# Line 236 | Line 256 | public class IntMapCheck {
256          reallyAssert (s.size() == size);
257          int sum = 0;
258          timer.start("Iter XEntry            ", size);
259 <        Iterator it = s.entrySet().iterator();
259 >        Iterator it = s.entrySet().iterator();
260          Integer k = null;
261          Integer v = null;
262          for (int i = 0; i < size-pos; ++i) {
# Line 252 | Line 272 | public class IntMapCheck {
272          reallyAssert (!s.containsKey(k));
273          while (it.hasNext()) {
274              Map.Entry<Integer,Integer> x = (Map.Entry<Integer,Integer>)(it.next());
275 <            Integer k2 = (Integer)x.getKey();
275 >            Integer k2 = (Integer) x.getKey();
276              seen.put(k2, k2);
277              if (k2 != MISSING)
278                  ++sum;
# Line 261 | Line 281 | public class IntMapCheck {
281          reallyAssert (s.size() == size-1);
282          s.put(k, v);
283          reallyAssert (seen.size() == size);
284 <        timer.finish();
284 >        timer.finish();
285          reallyAssert (sum == size);
286          reallyAssert (s.size() == size);
287      }
288  
289  
290      static void ittest(Map<Integer,Integer> s, int size) {
291 <        ittest1(s, size);
292 <        ittest2(s, size);
293 <        ittest3(s, size);
294 <        //        for (int i = 0; i < size-1; ++i)
291 >        for (int i = 0; i < 4; ++i) {
292 >            ittest1(s, size);
293 >            ittest2(s, size);
294 >            ittest3(s, size);
295 >        }
296 >        //        for (int i = 0; i < size-1; ++i)
297          //            ittest4(s, size, i);
298      }
299  
# Line 283 | Line 305 | public class IntMapCheck {
305              if (en.nextElement() != MISSING)
306                  ++sum;
307          }
308 <        timer.finish();
308 >        timer.finish();
309          reallyAssert (sum == size);
310      }
311  
# Line 294 | Line 316 | public class IntMapCheck {
316              if (en.nextElement() != MISSING)
317                  ++sum;
318          }
319 <        timer.finish();
319 >        timer.finish();
320          reallyAssert (sum == size);
321      }
322  
# Line 303 | Line 325 | public class IntMapCheck {
325          int sum = 0;
326  
327          timer.start("Iterf Enumeration Key  ", size);
328 <        Enumeration en = ht.keys();
328 >        Enumeration en = ht.keys();
329          for (int i = 0; i < size; ++i) {
330              if (en.nextElement() != MISSING)
331                  ++sum;
332          }
333 <        timer.finish();
333 >        timer.finish();
334          reallyAssert (sum == size);
335      }
336  
337      static void entest4(Hashtable ht, int size) {
338          int sum = 0;
339          timer.start("Iterf Enumeration Value", size);
340 <        Enumeration en = ht.elements();
340 >        Enumeration en = ht.elements();
341          for (int i = 0; i < size; ++i) {
342              if (en.nextElement() != MISSING)
343                  ++sum;
344          }
345 <        timer.finish();
345 >        timer.finish();
346          reallyAssert (sum == size);
347      }
348  
349      static void entest(Map<Integer,Integer> s, int size) {
350          if (s instanceof Hashtable) {
351 <            Hashtable ht = (Hashtable)s;
351 >            Hashtable ht = (Hashtable) s;
352              //            entest3(ht, size);
353              //            entest4(ht, size);
354              entest1(ht, size);
# Line 344 | Line 366 | public class IntMapCheck {
366              it.next();
367              it.remove();
368          }
369 <        timer.finish();
369 >        reallyAssert(s.isEmpty());
370 >        timer.finish();
371      }
372  
373 <    static void rvtest(Map<Integer,Integer> s, int size) {
374 <        timer.start("Remove (iterator)      ", size);
375 <        for (Iterator it = s.values().iterator(); it.hasNext(); ) {
376 <            it.next();
377 <            it.remove();
373 >    static void stest(Map<Integer,Integer> s, int size) throws Exception {
374 >        if (!(s instanceof Serializable))
375 >            return;
376 >        System.out.print("Serialize              : ");
377 >
378 >        for (int i = 0; i < size; i++) {
379 >            s.put(Integer.valueOf(i), Integer.valueOf(1));
380          }
381 <        timer.finish();
381 >
382 >        long startTime = System.nanoTime();
383 >
384 >        FileOutputStream fs = new FileOutputStream("IntMapCheck.dat");
385 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(fs));
386 >        out.writeObject(s);
387 >        out.close();
388 >
389 >        FileInputStream is = new FileInputStream("IntMapCheck.dat");
390 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(is));
391 >        Map<Integer,Integer> m = (Map<Integer,Integer>)in.readObject();
392 >
393 >        long endTime = System.nanoTime();
394 >        long time = endTime - startTime;
395 >
396 >        System.out.print(time + "ms");
397 >
398 >        if (s instanceof IdentityHashMap) return;
399 >        reallyAssert (s.equals(m));
400      }
401  
402  
403 <    static void dtest(Map<Integer,Integer> s, int size, Integer[] key) {
403 >    static void test(Map<Integer,Integer> s, Integer[] key) {
404 >        int size = key.length;
405 >
406 >        t3("Put (absent)           ", size, s, key, size);
407 >        reallyAssert(s.size() == size);
408 >        t1("Get (present)          ", size, s, key, size, 8);
409 >        t1Boxed("Get boxed (present)    ", size, s, key, size);
410 >        ittest1(s, size);
411 >        t3("Put (present)          ", size, s, key, 0);
412 >        reallyAssert(s.size() == size);
413 >        t7("ContainsKey            ", size, s, key, absent);
414 >        t4("ContainsKey            ", size, s, key, size);
415 >        ktest(s, size, key);
416 >        t4("ContainsKey            ", absentSize, s, absent, 0);
417 >        t6("Get                    ", size, s, key, absent);
418 >        t1("Get (present)          ", size, s, key, size, 8);
419 >        t1("Get (absent)           ", absentSize, s, absent, 0, 1);
420 >        reallyAssert(s.size() == size);
421 >        t2("Remove (absent)        ", absentSize, s, absent, 0);
422 >        reallyAssert(s.size() == size);
423 >        t5("Remove (present)       ", size, s, key, size / 2);
424 >        reallyAssert(s.size() == size / 2);
425 >        t1("Get                    ", size, s, key, size / 2, 8);
426 >        ittest1(s, size / 2);
427 >        t3("Put (half present)     ", size, s, key, size / 2);
428 >        reallyAssert(s.size() == size);
429 >        t1("Get (present)          ", size, s, key, size, 4);
430 >
431 >        entest(s, size);
432 >        t9(s);
433 >        reallyAssert(s.size() == size);
434 >        timer.start("Clear                  ", size);
435 >        s.clear();
436 >        timer.finish();
437 >        t1("Get (absent)           ", size, s, key, 0, 1);
438 >        t4("ContainsKey            ", size, s, key, 0);
439 >        t2("Remove (absent)        ", size, s, key, 0);
440 >        t3("Put (presized)         ", size, s, key, size);
441 >        t1("Get (present)          ", size, s, key, size, 4);
442 >        reallyAssert(s.size() == size);
443 >        ittest(s, size);
444 >        rtest(s, size);
445 >        reallyAssert(s.size() == 0);
446 >        timer.start("Clear                  ", size);
447 >        s.clear();
448 >        timer.finish();
449 >        t3("Put (presized)         ", size, s, key, size);
450 >
451          timer.start("Put (putAll)           ", size * 2);
452          Map<Integer,Integer> s2 = null;
453          try {
# Line 365 | Line 455 | public class IntMapCheck {
455              s2.putAll(s);
456          }
457          catch (Exception e) { e.printStackTrace(); return; }
458 <        timer.finish();
459 <    
458 >        timer.finish();
459 >
460          timer.start("Iter Equals            ", size * 2);
461          boolean eqt = s2.equals(s) && s.equals(s2);
462          reallyAssert (eqt);
463 <        timer.finish();
463 >        timer.finish();
464  
465          timer.start("Iter HashCode          ", size * 2);
466          int shc = s.hashCode();
467          int s2hc = s2.hashCode();
468          reallyAssert (shc == s2hc);
469 <        timer.finish();
469 >        timer.finish();
470  
471 <        timer.start("Put (present)          ", size);
471 >        timer.start("Put (present)          ", size * 2);
472          s2.putAll(s);
473 <        timer.finish();
473 >        timer.finish();
474 >
475 >        timer.start("Put (present)          ", size);
476 >        int ipsum = 0;
477 >        for (Iterator i0 = s.entrySet().iterator(); i0.hasNext(); ) {
478 >            Map.Entry<Integer,Integer> me = (Map.Entry<Integer,Integer>)(i0.next());
479 >            if (s2.put(me.getKey(), me.getValue()) != null)
480 >                ++ipsum;
481 >        }
482 >        reallyAssert (ipsum == s.size());
483 >        timer.finish();
484  
485          timer.start("Iter EntrySet contains ", size * 2);
486          Set es2 = s2.entrySet();
# Line 389 | Line 489 | public class IntMapCheck {
489              Object entry = i1.next();
490              if (es2.contains(entry)) ++sum;
491          }
492 <        timer.finish();
492 >        timer.finish();
493          reallyAssert (sum == size);
494  
395        t6("Get                    ", size, s2, key, absent);
396
495          Integer hold = s2.get(key[size-1]);
496          s2.put(key[size-1], absent[0]);
497          timer.start("Iter Equals            ", size * 2);
498          eqt = s2.equals(s) && s.equals(s2);
499          reallyAssert (!eqt);
500 <        timer.finish();
500 >        timer.finish();
501  
502          timer.start("Iter HashCode          ", size * 2);
503          int s1h = s.hashCode();
504          int s2h = s2.hashCode();
505          reallyAssert (s1h != s2h);
506 <        timer.finish();
506 >        timer.finish();
507  
508          s2.put(key[size-1], hold);
509 <        timer.start("Remove (iterator)      ", size * 2);
509 >        timer.start("Remove (present)       ", size * 2);
510          Iterator s2i = s2.entrySet().iterator();
511          Set es = s.entrySet();
512          while (s2i.hasNext())
513              reallyAssert(es.remove(s2i.next()));
514 <        timer.finish();
514 >        timer.finish();
515  
418        if (!s.isEmpty()) System.out.println(s);
516          reallyAssert (s.isEmpty());
517  
518          timer.start("Clear                  ", size);
519          s2.clear();
520 <        timer.finish();
520 >        timer.finish();
521          reallyAssert (s2.isEmpty() && s.isEmpty());
522      }
523  
427    static void stest(Map<Integer,Integer> s, int size) throws Exception {
428        if (!(s instanceof Serializable))
429            return;
430        System.out.print("Serialize              : ");
431      
432        for (int i = 0; i < size; i++) {
433            s.put(new Integer(i), new Integer(1));
434        }
435
436        long startTime = System.nanoTime();
437
438        FileOutputStream fs = new FileOutputStream("IntMapCheck.dat");
439        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(fs));
440        out.writeObject(s);
441        out.close();
442
443        FileInputStream is = new FileInputStream("IntMapCheck.dat");
444        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(is));
445        Map<Integer,Integer> m = (Map<Integer,Integer>)in.readObject();
446
447        long endTime = System.nanoTime();
448        long time = endTime - startTime;
449
450        System.out.print(time + "ms");
451
452        if (s instanceof IdentityHashMap) return;
453        reallyAssert (s.equals(m));
454    }
455
456    
457    static void test(Map<Integer,Integer> s, Integer[] key) {
458        int size = key.length;
459
460        t3("Put (absent)           ", size, s, key, size);
461        t3("Put (present)          ", size, s, key, 0);
462        t7("ContainsKey            ", size, s, key, absent);
463        t4("ContainsKey            ", size, s, key, size);
464        ktest(s, size, key);
465        t4("ContainsKey            ", absentSize, s, absent, 0);
466        t6("Get                    ", size, s, key, absent);
467        t1("Get (present)          ", size, s, key, size);
468        t1("Get (absent)           ", absentSize, s, absent, 0);
469        t2("Remove (absent)        ", absentSize, s, absent, 0);
470        t5("Remove (present)       ", size, s, key, size / 2);
471        t3("Put (half present)     ", size, s, key, size / 2);
472
473        ittest(s, size);
474        entest(s, size);
475        t9(s);
476        rtest(s, size);
477
478        t4("ContainsKey            ", size, s, key, 0);
479        t2("Remove (absent)        ", size, s, key, 0);
480        t3("Put (presized)         ", size, s, key, size);
481        dtest(s, size, key);
482    }
483
524      static class TestTimer {
525          private String name;
526          private long numOps;
# Line 488 | Line 528 | public class IntMapCheck {
528          private String cname;
529  
530          static final java.util.TreeMap accum = new java.util.TreeMap();
531 <    
531 >
532          static void printStats() {
533              for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) {
534                  Map.Entry e = (Map.Entry)(it.next());
535 <                Stats stats = ((Stats)(e.getValue()));
535 >                Stats stats = ((Stats) (e.getValue()));
536                  long n = stats.number;
537                  double t;
538 <                if (n > 0)
538 >                if (n > 0)
539                      t = stats.sum / n;
540                  else
541                      t = stats.least;
542 <                long nano = Math.round(1000000.0 * t);
542 >                long nano = Math.round(t);
543                  System.out.println(e.getKey() + ": " + nano);
544              }
545          }
546 <    
546 >
547          void start(String name, long numOps) {
548              this.name = name;
549              this.cname = classify();
550              this.numOps = numOps;
551              startTime = System.nanoTime();
552          }
553 <    
553 >
554  
555          String classify() {
556              if (name.startsWith("Get"))
# Line 521 | Line 561 | public class IntMapCheck {
561                  return "Remove                 ";
562              else if (name.startsWith("Iter"))
563                  return "Iter                   ";
564 <            else
564 >            else
565                  return null;
566          }
567  
568          void finish() {
569              long endTime = System.nanoTime();
570              long time = endTime - startTime;
571 <            double timePerOp = (((double)time)/numOps) / 1000000;
571 >            double timePerOp = ((double)time)/numOps;
572  
573              Object st = accum.get(name);
574              if (st == null)
# Line 563 | Line 603 | public class IntMapCheck {
603          Stats(double t) { least = t; }
604      }
605  
606 <    static Random rng = new Random();
606 >    static Random rng = new Random(3152688);
607  
608      static void shuffle(Integer[] keys) {
609          int size = keys.length;
# Line 576 | Line 616 | public class IntMapCheck {
616      }
617  
618   }
579

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines