001: /*
002:
003: Derby - Class org.apache.derbyTesting.unitTests.services.T_CacheUser
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.unitTests.services;
023:
024: import org.apache.derby.iapi.services.cache.*;
025:
026: import org.apache.derbyTesting.unitTests.harness.T_Fail;
027:
028: import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
029:
030: import org.apache.derby.iapi.error.StandardException;
031: import org.apache.derby.iapi.reference.SQLState;
032:
033: public class T_CacheUser implements Runnable {
034:
035: protected CacheManager cm;
036: protected int iterations;
037: protected HeaderPrintWriter out;
038: protected T_CacheService parent;
039:
040: public T_CacheUser(CacheManager cm, int iterations,
041: T_CacheService parent, HeaderPrintWriter out) {
042: this .cm = cm;
043: this .iterations = iterations;
044: this .parent = parent;
045: this .out = out;
046: }
047:
048: public void run() {
049: try {
050: thrashCache();
051: } catch (T_Fail tf) {
052: parent.setChildException(tf);
053: } catch (StandardException se) {
054: parent.setChildException(T_Fail.exceptionFail(se));
055: }
056: }
057:
058: /**
059: T_CachedInteger range - 0 - 100
060:
061: pick a key randomly
062: 48%/48%/4% chance of Int/String/invalid key
063: 90%/5%/5% chance of can find / can't find / raise exception
064: 50%/30%/20% find/findCached/create
065:
066:
067: @exception StandardException Standard Derby Error policy
068: @exception T_Fail Some error
069:
070:
071: */
072:
073: public void thrashCache() throws StandardException, T_Fail {
074:
075: // stats
076: int f = 0, fs = 0, ff = 0, fe = 0;
077: int fc = 0, fcs = 0, fcf = 0;
078: int c = 0, cs = 0, cf = 0, ce = 0, cse = 0;
079: int cleanAll = 0, ageOut = 0;
080: int release = 0, remove = 0;
081:
082: for (int i = 0; i < iterations; i++) {
083:
084: if ((i % 100) == 0)
085: out.printlnWithHeader("iteration " + i);
086:
087: T_Key tkey = T_Key.randomKey();
088:
089: double rand = Math.random();
090: T_Cacheable e = null;
091: if (rand < 0.5) {
092: f++;
093:
094: try {
095:
096: e = (T_Cacheable) cm.find(tkey);
097: if (e == null) {
098: ff++;
099: continue;
100: }
101:
102: fs++;
103:
104: } catch (T_CacheException tc) {
105: if (tc.getType() == T_CacheException.ERROR)
106: throw tc;
107:
108: // acceptable error
109: 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
129: .currentThread());
130: if (e == null) {
131: cf++;
132: continue;
133: }
134:
135: cs++;
136:
137: } catch (T_CacheException tc) {
138: if (tc.getType() == T_CacheException.ERROR)
139: throw tc;
140:
141: // acceptable error
142: ce++;
143: continue;
144: } catch (StandardException se) {
145:
146: if (se.getMessageId().equals(
147: SQLState.OBJECT_EXISTS_IN_CACHE)) {
148: cse++;
149: continue;
150: }
151: throw se;
152: }
153: }
154:
155: // ensure we can find it cached and that the key matches
156: cm.release(parent.t_findCachedSucceed(cm, tkey));
157:
158: if (Math.random() < 0.25)
159: e.setDirty();
160:
161: if (Math.random() < 0.75)
162: Thread.yield();
163:
164: if ((Math.random() < 0.10) && (e.canRemove())) {
165: remove++;
166: cm.remove(e);
167: } else {
168: release++;
169: cm.release(e);
170: }
171: e = null;
172:
173: double rand2 = Math.random();
174:
175: if (rand2 < 0.02) {
176: cleanAll++;
177: cm.cleanAll();
178: } else if (rand2 < 0.04) {
179: ageOut++;
180: cm.ageOut();
181: }
182: }
183:
184: // ensure all our output in grouped.
185: synchronized (parent) {
186: out.printlnWithHeader("find() calls " + f
187: + " : found/not found/exception : " + fs + "/" + ff
188: + "/" + fe);
189: out.printlnWithHeader("findCached() calls " + fc
190: + " : found/not found : " + fcs + "/"
191: + fcf);
192: out
193: .printlnWithHeader("create() calls "
194: + c
195: + " : found/not found/exception/standard exception : "
196: + cs + "/" + cf + "/" + ce + "/" + cse);
197: out.printlnWithHeader("release() calls " + release);
198: out.printlnWithHeader("remove() calls " + remove);
199: out.printlnWithHeader("cleanAll() calls " + cleanAll);
200: out.printlnWithHeader("ageOut() calls " + ageOut);
201: }
202:
203: }
204: }
|