1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ehcache.store;
18
19 import net.sf.ehcache.AbstractCacheTest;
20 import net.sf.ehcache.Element;
21 import net.sf.ehcache.MemoryStoreTester;
22 import net.sf.ehcache.StopWatch;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import java.io.IOException;
27 import java.util.Date;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.Map;
31 import java.util.Random;
32
33
34
35
36
37
38
39 public class LfuMemoryStoreTest extends MemoryStoreTester {
40
41 private static final Log LOG = LogFactory.getLog(LfuMemoryStoreTest.class.getName());
42
43
44
45
46 protected void setUp() throws Exception {
47 super.setUp();
48 createMemoryStore(MemoryStoreEvictionPolicy.LFU);
49 }
50
51
52
53
54
55 public void testPutFromConfig() throws Exception {
56 createMemoryStore(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-policy-test.xml", "sampleLFUCache1");
57 putTest();
58 }
59
60
61
62
63 public void testPutFromConfigZeroMemoryStore() throws Exception {
64 createMemoryStore(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-policy-test.xml", "sampleLFUCache2");
65 Element element = new Element("1", "value");
66 store.put(element);
67 assertNull(store.get("1"));
68 }
69
70
71
72
73 public void testRemoveFromConfig() throws Exception {
74 createMemoryStore(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-policy-test.xml", "sampleLFUCache1");
75 removeTest();
76 }
77
78
79
80
81
82
83 public void testBenchmarkPutGetSurya() throws Exception {
84 benchmarkPutGetSuryaTest(9000);
85 }
86
87
88
89
90 public void testLfuPolicy() throws Exception {
91 createMemoryStore(MemoryStoreEvictionPolicy.LFU, 4);
92 lfuPolicyTest();
93 }
94
95
96
97
98 public void testLfuPolicyFromConfig() throws Exception {
99 createMemoryStore(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-policy-test.xml", "sampleLFUCache1");
100 lfuPolicyTest();
101 }
102
103
104 private void lfuPolicyTest() throws IOException {
105
106 assertEquals(0, store.getSize());
107
108
109 Element element = new Element("key1", "value1");
110 store.put(element);
111 assertEquals(1, store.getSize());
112
113 element = new Element("key2", "value2");
114 store.put(element);
115 assertEquals(2, store.getSize());
116
117 element = new Element("key3", "value3");
118 store.put(element);
119 assertEquals(3, store.getSize());
120
121 element = new Element("key4", "value4");
122 store.put(element);
123 assertEquals(4, store.getSize());
124
125
126 store.get("key1");
127 store.get("key1");
128 store.get("key3");
129 store.get("key3");
130 store.get("key3");
131 store.get("key4");
132
133
134 element = new Element("key5", "value5");
135 store.put(element);
136
137 assertEquals(4, store.getSize());
138
139 assertNull(store.get("key2"));
140
141
142 store.get("key5");
143 store.get("key5");
144
145
146 element = new Element("key6", "value6");
147 store.put(element);
148 assertEquals(4, store.getSize());
149 assertNull(store.get("key4"));
150
151
152
153 }
154
155
156
157
158
159 public void testBenchmarkPutGetRemove() throws Exception {
160 super.testBenchmarkPutGetRemove();
161 }
162
163
164
165
166
167
168
169
170 public void testMemoryLeak() throws Exception {
171 super.testMemoryLeak();
172 }
173
174
175
176
177
178 public void testBenchmarkPutGet() throws Exception {
179 super.testBenchmarkPutGet();
180 }
181
182
183
184
185
186
187
188
189
190
191
192 public void testRandomnessOfIterator() {
193 int mean = 0;
194 int absoluteDifferences = 0;
195 int lastReading = 0;
196 Map map = new HashMap();
197 for (int i = 1; i <= 500; i++) {
198 mean += i;
199 map.put("" + i, " ");
200 }
201 mean = mean / 500;
202 for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
203 String string = (String) iterator.next();
204 int thisReading = Integer.parseInt(string);
205 absoluteDifferences += Math.abs(lastReading - thisReading);
206 lastReading = thisReading;
207 }
208 LOG.info("Mean difference through iteration: " + absoluteDifferences / 500);
209
210
211 Random random = new Random();
212 while (map.size() != 0) {
213 int thisReading = random.nextInt(501);
214 Object o = map.remove("" + thisReading);
215 if (o == null) {
216 continue;
217 }
218 absoluteDifferences += Math.abs(lastReading - thisReading);
219 lastReading = thisReading;
220 }
221 LOG.info("Mean difference with random selection without replacement : " + absoluteDifferences / 500);
222 LOG.info("Mean of range 1 - 500 : " + mean);
223
224 }
225
226
227
228
229
230
231
232 public void testSampling() throws IOException {
233 createMemoryStore(MemoryStoreEvictionPolicy.LFU, 1000);
234 LfuPolicy.Metadata[] elements = null;
235 for (int i = 0; i < 10; i++) {
236 store.put(new Element("" + i, new Date()));
237 elements = ((LfuMemoryStore) store).sampleElements(i + 1);
238 }
239
240 for (int i = 10; i < 2000; i++) {
241 store.put(new Element("" + i, new Date()));
242 elements = ((LfuMemoryStore) store).sampleElements(10);
243 assertEquals(10, elements.length);
244 }
245 }
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282 public void testLowest() throws IOException {
283 createMemoryStore(MemoryStoreEvictionPolicy.LFU, 5000);
284 Element element = null;
285 Element newElement = null;
286 for (int i = 0; i < 10; i++) {
287 newElement = new Element("" + i, new Date());
288 store.put(newElement);
289 int j;
290 for (j = 0; j <= i; j++) {
291 store.get("" + i);
292 }
293 if (i > 0) {
294 element = ((LfuMemoryStore) store).findRelativelyUnused(newElement);
295 assertTrue(!element.equals(newElement));
296 assertTrue(element.getHitCount() < 2);
297 }
298 }
299
300 int lowestQuarterNotIdentified = 0;
301
302 long findTime = 0;
303 StopWatch stopWatch = new StopWatch();
304 for (int i = 10; i < 5000; i++) {
305 store.put(new Element("" + i, new Date()));
306 int j;
307 int maximumHitCount = 0;
308 for (j = 0; j <= i; j += 10) {
309 store.get("" + i);
310 maximumHitCount++;
311 }
312
313 stopWatch.getElapsedTime();
314 element = ((LfuMemoryStore) store).findRelativelyUnused(newElement);
315 findTime += stopWatch.getElapsedTime();
316 long lowest = element.getHitCount();
317 long bottomQuarter = (Math.round(maximumHitCount / 4.0) + 1);
318 assertTrue(!element.equals(newElement));
319 if (lowest > bottomQuarter) {
320 lowestQuarterNotIdentified++;
321
322 }
323 }
324 LOG.info("Find time: " + findTime);
325 assertTrue(findTime < 1000);
326 LOG.info("Selections not in lowest quartile: " + lowestQuarterNotIdentified);
327 assertTrue(lowestQuarterNotIdentified < 5);
328
329 }
330
331 }