1 5 package com.tc.object.cache; 6 7 import com.tc.object.ObjectID; 8 9 import java.util.Collection ; 10 import java.util.Iterator ; 11 import java.util.LinkedHashMap ; 12 import java.util.Random ; 13 14 public class LFUEvictionPolicyTest extends LRUEvictionPolicyTest { 15 16 public void tests() throws Exception { 17 int capacity = 50; 18 int maxObjects = capacity * 2; 19 EvictionPolicy slc = createNewCache(capacity); 20 21 LinkedHashMap cacheables = new LinkedHashMap (); 22 Random r = new Random (); 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 ("Cache is full when capacity = " + capacity + "and size () = " + cacheables.size()); 32 } else if (!full && cacheables.size() > capacity) { 33 throw new AssertionError ("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 ("Accessed Count Mismatch : " + tc.accessCount() 41 + " Expected : " + accessed); } 42 } 43 } 44 45 while (cacheables.size() > capacity) { 46 Collection rc = slc.getRemovalCandidates(10); 47 int maxAccessed = -1; 48 for (Iterator 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 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 errorThreshold++; 65 } 66 } 67 if(errorThreshold > 0) { 72 System.err.println("WARNING : errorThreshold is " + errorThreshold); 73 } 74 } 75 } 76 77 public void testLargeCacheEviction() throws Exception { 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 cacheables = new LinkedHashMap (); 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 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 i = rc.iterator(); i.hasNext();) { 104 TestCacheable tc = (TestCacheable) i.next(); 105 if (s_range > tc.accessCount() || e_range < tc.accessCount()) { 106 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 |