1 5 package com.tc.object.cache; 6 7 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt; 8 9 import com.tc.test.TCTestCase; 10 import com.tc.util.concurrent.ThreadUtil; 11 12 import java.util.ArrayList ; 13 import java.util.Date ; 14 import java.util.Random ; 15 import java.util.Vector ; 16 17 public class CacheManagerTest extends TCTestCase implements Evictable { 18 19 private static final int BYTES_SIZE = 10240; 20 int usedT = 50; 21 int usedCritialT = 90; 22 long sleepInterval = 500; 23 int lc = 2; 24 int percentage2Evict = 10; 25 SynchronizedInt callCount = new SynchronizedInt(0); 26 27 Vector v = new Vector (); 28 29 public void test() throws Exception { 30 CacheManager cm = new CacheManager(this, new TestCacheConfig()); 31 log("Cache Manager Created : " + cm); 32 hogMemory(); 33 assertTrue(callCount.get() > 0); 34 } 35 36 private void log(String message) { 37 System.err.println(time() + " - " + thread() + " : " + message); 38 } 39 40 private String time() { 41 return new Date ().toString(); 42 } 43 44 private String thread() { 45 return Thread.currentThread().getName(); 46 } 47 48 private void hogMemory() { 49 for (int i = 1; i < 300000; i++) { 50 byte[] b = new byte[BYTES_SIZE]; 51 v.add(b); 52 if (i == 100) { 53 ThreadUtil.reallySleep(2000); 55 } else if (i % 10000 == 0) { 56 log("Created " + i + " byte arrays - currently in vector = " + v.size()); 57 ThreadUtil.reallySleep(500); 58 } else if (i % 50 == 0) { 59 ThreadUtil.reallySleep(1); 60 } else if (i % 100 == 1) { 61 waitTillNotifiedIfCritical(); 62 } 63 } 64 } 65 66 private void waitTillNotifiedIfCritical() { 67 synchronized (v) { 68 Runtime runtime = Runtime.getRuntime(); 69 long max = runtime.maxMemory(); 70 long total = runtime.totalMemory(); 71 long free = runtime.freeMemory(); 72 if (total >= max * 0.97 && free < max * 0.05) { 74 log("WARNING :: Vector Size reached " + v.size() + " and free = " + free + " max = " + max + " total = " 76 + total + ". Pathway to OOME. Waiting for 5O msec or until reaping."); 77 try { 78 v.wait(50); 79 } catch (InterruptedException e) { 80 throw new RuntimeException (e); 81 } 82 } 83 } 84 85 } 86 87 public void evictCache(CacheStats stat) { 88 synchronized (v) { 89 int toEvict = stat.getObjectCountToEvict(v.size()); 90 int evicted = 0; 91 ArrayList targetObjects4GC = new ArrayList (); 92 if (toEvict >= v.size()) { 93 evicted = v.size(); 94 targetObjects4GC.addAll(v); 95 v.clear(); 96 } else { 97 for (int i = 0; i < toEvict; i++) { 98 targetObjects4GC.add(v.remove(rnd(v.size()))); 99 evicted++; 100 } 101 } 102 stat.objectEvicted(evicted, v.size(), targetObjects4GC); 103 if (callCount.increment() % 10 == 1) { 104 log(stat.toString()); 105 log("Asked to evict - " + toEvict + " Evicted : " + evicted + " Vector Size : " + v.size()); 106 } 107 v.notifyAll(); 108 } 109 } 110 111 Random r = new Random (); 112 113 private int rnd(int max) { 114 return r.nextInt(max); 115 } 116 117 public class TestCacheConfig implements CacheConfig { 118 119 public int getUsedCriticalThreshold() { 120 return usedCritialT; 121 } 122 123 public int getUsedThreshold() { 124 return usedT; 125 } 126 127 public int getLeastCount() { 128 return lc; 129 } 130 131 public int getPercentageToEvict() { 132 return percentage2Evict; 133 } 134 135 public long getSleepInterval() { 136 return sleepInterval; 137 } 138 139 public boolean isOnlyOldGenMonitored() { 140 return true; 141 } 142 143 public boolean isLoggingEnabled() { 144 return false; 145 } 146 147 } 148 149 } 150 | Popular Tags |