KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > cache > LFUEvictionPolicyTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.object.cache;
6
7 import com.tc.object.ObjectID;
8
9 import java.util.Collection JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.LinkedHashMap JavaDoc;
12 import java.util.Random JavaDoc;
13
14 public class LFUEvictionPolicyTest extends LRUEvictionPolicyTest {
15
16   public void tests() throws Exception JavaDoc {
17     int capacity = 50;
18     int maxObjects = capacity * 2;
19     EvictionPolicy slc = createNewCache(capacity);
20
21     LinkedHashMap JavaDoc cacheables = new LinkedHashMap JavaDoc();
22     Random JavaDoc r = new Random JavaDoc();
23     for (int i = 1; i < 20000; i++) {
24       ObjectID id = new ObjectID(r.nextInt(maxObjects));
25       TestCacheable tc = (TestCacheable) cacheables.get(id);
26       if (tc == null) {
27         tc = new TestCacheable(id);
28         cacheables.put(id, tc);
29         boolean full = slc.add(tc);
30         if (full && cacheables.size() <= capacity) {
31           throw new AssertionError JavaDoc("Cache is full when capacity = " + capacity + "and size () = " + cacheables.size());
32         } else if (!full && cacheables.size() > capacity) {
33           // Formatter
34
throw new AssertionError JavaDoc("Cache is Not full when capacity = " + capacity + "and size () = "
35                                    + cacheables.size());
36         }
37       } else {
38         int accessed = tc.accessCount();
39         slc.markReferenced(tc);
40         if (++accessed != tc.accessCount()) { throw new AssertionError JavaDoc("Accessed Count Mismatch : " + tc.accessCount()
41                                                                        + " Expected : " + accessed); }
42       }
43     }
44
45     while (cacheables.size() > capacity) {
46       Collection JavaDoc rc = slc.getRemovalCandidates(10);
47       int maxAccessed = -1;
48       for (Iterator JavaDoc i = rc.iterator(); i.hasNext();) {
49         TestCacheable tc = (TestCacheable) i.next();
50         if (maxAccessed < tc.accessCount()) {
51           maxAccessed = tc.accessCount();
52         }
53         slc.remove(tc);
54         assertTrue(cacheables.remove(tc.getObjectID()) == tc);
55       }
56       System.err.println("Max Accessed is : " + maxAccessed);
57       int errorThreshold = 0;
58       for (Iterator JavaDoc i = cacheables.values().iterator(); i.hasNext();) {
59         TestCacheable tc = (TestCacheable) i.next();
60         assertFalse(rc.contains(tc));
61         if (tc.accessCount() < maxAccessed) {
62           System.err.println("WARNING : maxAccessed : " + maxAccessed + " tc.accessCount() " + tc.accessCount());
63           // throw new AssertionError("Got an Object that is accessed more that one available in cache");
64
errorThreshold++;
65         }
66       }
67       // Commenting this as this is assertion is not valid anymore.
68
// if (errorThreshold >= rc.size()) {
69
// throw new AssertionError("Beyond Error Threshold : " + errorThreshold);
70
// }
71
if(errorThreshold > 0) {
72         System.err.println("WARNING : errorThreshold is " + errorThreshold);
73       }
74     }
75   }
76
77   public void testLargeCacheEviction() throws Exception JavaDoc {
78     if (true) {
79       System.err.println("This test (testLargeCacheEviction) is DISABLED.");
80       return;
81     }
82     int capacity = 3500000;
83     int count = capacity + 500000;
84     EvictionPolicy slc = createNewCache(capacity);
85
86     LinkedHashMap JavaDoc cacheables = new LinkedHashMap JavaDoc();
87     for (int i = 1; i < count; i++) {
88       ObjectID id = new ObjectID(i);
89       TestCacheable tc = new TestCacheable(id, i);
90       cacheables.put(id, tc);
91       slc.add(tc);
92     }
93
94     int s_range = 0, e_range = 0;
95     while (cacheables.size() > capacity) {
96       long start = System.currentTimeMillis();
97       Collection JavaDoc rc = slc.getRemovalCandidates(10000);
98       s_range = e_range;
99       e_range = e_range + rc.size();
100       long end = System.currentTimeMillis();
101       System.err.println("Time take for evicting " + rc.size() + " from " + cacheables.size() + " = " + (end - start)
102                          + " ms");
103       for (Iterator JavaDoc i = rc.iterator(); i.hasNext();) {
104         TestCacheable tc = (TestCacheable) i.next();
105         if (s_range > tc.accessCount() || e_range < tc.accessCount()) {
106           // XXX:: This is not an error anymore since every call to getRemovalCandidates affects the accessCount
107
System.err.println("Access Count falls in the wrong for " + tc + " range : " + tc.accessCount() + " range = "
108                              + s_range + " , " + e_range);
109         }
110         slc.remove(tc);
111         assertTrue(cacheables.remove(tc.getObjectID()) == tc);
112       }
113     }
114   }
115
116   public EvictionPolicy createNewCache(int cacheSize) {
117     return new LFUEvictionPolicy(cacheSize);
118   }
119
120 }
121
Popular Tags