1 21 22 package org.apache.derbyTesting.unitTests.services; 23 24 import org.apache.derby.iapi.services.cache.*; 25 26 import org.apache.derbyTesting.unitTests.harness.T_Fail; 27 28 import org.apache.derby.iapi.services.stream.HeaderPrintWriter; 29 30 import org.apache.derby.iapi.error.StandardException; 31 import org.apache.derby.iapi.reference.SQLState; 32 33 public class T_CacheUser implements Runnable { 34 35 protected CacheManager cm; 36 protected int iterations; 37 protected HeaderPrintWriter out; 38 protected T_CacheService parent; 39 40 public T_CacheUser(CacheManager cm, int iterations, T_CacheService parent, HeaderPrintWriter out) { 41 this.cm = cm; 42 this.iterations = iterations; 43 this.parent = parent; 44 this.out = out; 45 } 46 47 48 public void run() { 49 try { 50 thrashCache(); 51 } catch (T_Fail tf) { 52 parent.setChildException(tf); 53 } catch (StandardException se) { 54 parent.setChildException(T_Fail.exceptionFail(se)); 55 } 56 } 57 71 72 public void thrashCache() throws StandardException, T_Fail { 73 74 int f = 0, fs = 0, ff = 0, fe = 0; 76 int fc = 0, fcs = 0, fcf = 0; 77 int c = 0, cs = 0, cf = 0, ce = 0, cse = 0; 78 int cleanAll = 0, ageOut = 0; 79 int release = 0, remove = 0; 80 81 82 for (int i = 0; i < iterations; i++) { 83 84 if ((i % 100) == 0) 85 out.printlnWithHeader("iteration " + i); 86 87 T_Key tkey = T_Key.randomKey(); 88 89 double rand = Math.random(); 90 T_Cacheable e = null; 91 if (rand < 0.5) { 92 f++; 93 94 try { 95 96 e = (T_Cacheable) cm.find(tkey); 97 if (e == null) { 98 ff++; 99 continue; 100 } 101 102 fs++; 103 104 } catch (T_CacheException tc) { 105 if (tc.getType() == T_CacheException.ERROR) 106 throw tc; 107 108 fe++; 110 continue; 111 } 112 } else if (rand < 0.8) { 113 114 fc++; 115 116 e = (T_Cacheable) cm.findCached(tkey); 117 if (e == null) { 118 fcf++; 119 continue; 120 } 121 fcs++; 122 123 } else { 124 c++; 125 126 try { 127 128 e = (T_Cacheable) cm.create(tkey, Thread.currentThread()); 129 if (e == null) { 130 cf++; 131 continue; 132 } 133 134 cs++; 135 136 } catch (T_CacheException tc) { 137 if (tc.getType() == T_CacheException.ERROR) 138 throw tc; 139 140 ce++; 142 continue; 143 } catch (StandardException se) { 144 145 if (se.getMessageId().equals(SQLState.OBJECT_EXISTS_IN_CACHE)) { 146 cse++; 147 continue; 148 } 149 throw se; 150 } 151 } 152 153 cm.release(parent.t_findCachedSucceed(cm, tkey)); 155 156 if (Math.random() < 0.25) 157 e.setDirty(); 158 159 if (Math.random() < 0.75) 160 Thread.yield(); 161 162 if ((Math.random() < 0.10) && (e.canRemove())) { 163 remove++; 164 cm.remove(e); 165 } else { 166 release++; 167 cm.release(e); 168 } 169 e = null; 170 171 double rand2 = Math.random(); 172 173 if (rand2 < 0.02) { 174 cleanAll++; 175 cm.cleanAll(); 176 } 177 else if (rand2 < 0.04) { 178 ageOut++; 179 cm.ageOut(); 180 } 181 } 182 183 synchronized (parent) { 185 out.printlnWithHeader("find() calls " + f + " : found/not found/exception : " + fs + "/" + ff + "/" + fe); 186 out.printlnWithHeader("findCached() calls " + fc + " : found/not found : " + fcs + "/" + fcf); 187 out.printlnWithHeader("create() calls " + c + " : found/not found/exception/standard exception : " + cs + "/" + cf + "/" + ce + "/" + cse); 188 out.printlnWithHeader("release() calls " + release); 189 out.printlnWithHeader("remove() calls " + remove); 190 out.printlnWithHeader("cleanAll() calls " + cleanAll); 191 out.printlnWithHeader("ageOut() calls " + ageOut); 192 } 193 194 } 195 } 196 | Popular Tags |