KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > services > T_CacheUser


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.services.T_CacheUser
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

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 JavaDoc {
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     /**
58         T_CachedInteger range - 0 - 100
59
60         pick a key randomly
61                     48%/48%/4% chance of Int/String/invalid key
62                     90%/5%/5% chance of can find / can't find / raise exception
63                     50%/30%/20% find/findCached/create
64                     
65
66         @exception StandardException Standard Derby Error policy
67         @exception T_Fail Some error
68                 
69         
70     */

71
72     public void thrashCache() throws StandardException, T_Fail {
73
74         // stats
75
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                     // 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.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                     // acceptable error
141
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             // ensure we can find it cached and that the key matches
154
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         // ensure all our output in grouped.
184
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