1 /**
2 * Copyright 2003-2007 Greg Luck
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.store;
18
19 import net.sf.ehcache.Element;
20 import net.sf.ehcache.MemoryStoreTester;
21
22 import java.util.Map;
23
24
25 /**
26 * Test cases for the LruMemoryStore.
27 * <p/>
28 * There are no tests of expiry because this is handled by {@link net.sf.ehcache.Cache#get}
29 * <p/>
30 * <b>Performance:</b>
31 * v1.38 of DiskStore
32 * INFO: Time for benhmarkPutGetSurya: 7355
33 * INFO: Time for Bulk Load: 13
34 * INFO: Time for benhmarkPutGetRemove: 264
35 * INFO: Time for benhmarkPutGet: 154
36 * <p/>
37 * v 1.42 of DiskStore
38 * INFO: Time for Bulk Load: 12
39 * INFO: Time for benhmarkPutGetRemove: 256
40 * INFO: Time for benhmarkPutGet: 165
41 *
42 * @author Greg Luck
43 * @version $Id: LruMemoryStoreTest.java 512 2007-07-10 09:18:45Z gregluck $
44 */
45 public class LruMemoryStoreTest extends MemoryStoreTester {
46
47 /**
48 * setup test
49 */
50 protected void setUp() throws Exception {
51 super.setUp();
52 createMemoryStore(MemoryStoreEvictionPolicy.LRU);
53 }
54
55 /**
56 * The LRU map implementation can be overridden by setting the "net.sf.ehcache.useLRUMap" System property.
57 * Here we do not do that and it should be the java.util.LinkedHashMap.
58 */
59 public void testCorrectMapImplementation() throws Exception {
60 createMemoryStore(MemoryStoreEvictionPolicy.LRU, 5);
61
62 Map map = ((MemoryStore) store).getBackingMap();
63 assertTrue(map instanceof java.util.LinkedHashMap);
64 }
65
66
67 /**
68 * Test the LRU policy
69 */
70 public void testPolicy() throws Exception {
71 createMemoryStore(MemoryStoreEvictionPolicy.LRU, 5);
72
73 //Make sure that the store is empty to start with
74 assertEquals(0, store.getSize());
75
76 // Populate the store till the max limit
77 Element element = new Element("key1", "value1");
78 store.put(element);
79 assertEquals(1, store.getSize());
80
81 element = new Element("key2", "value2");
82 store.put(element);
83 assertEquals(2, store.getSize());
84
85 element = new Element("key3", "value3");
86 store.put(element);
87 assertEquals(3, store.getSize());
88
89 element = new Element("key4", "value4");
90 store.put(element);
91 assertEquals(4, store.getSize());
92
93 element = new Element("key5", "value5");
94 store.put(element);
95 assertEquals(5, store.getSize());
96
97 // Now access the elements to boost the hits count, although irrelevant for this test just to demonstrate
98 // hit count is immaterial for this test.
99 store.get("key1");
100 store.get("key1");
101 store.get("key3");
102 store.get("key3");
103 store.get("key3");
104 store.get("key4");
105
106 //Create a new element and put in the store so as to force the policy
107 element = new Element("key6", "value6");
108 store.put(element);
109
110 //max size
111 assertEquals(5, store.getSize());
112
113 //The element with key "key2" should be the least recently used
114 assertNull(store.get("key2"));
115
116 // Make some more accesses
117 store.get("key5");
118 store.get("key5");
119
120 // Insert another element to force the policy
121 element = new Element("key7", "value7");
122 store.put(element);
123 assertEquals(5, store.getSize());
124
125 //key1 should now be the least recently used.
126 assertNull(store.get("key1"));
127 }
128
129
130 /**
131 * Benchmark to test speed. This uses both memory and disk and tries to be realistic
132 * v 1.38 DiskStore 7355
133 * v 1.41 DiskStore 1609
134 * Adjusted for change to laptop
135 */
136 public void testBenchmarkPutGetSurya() throws Exception {
137 benchmarkPutGetSuryaTest(2500);
138 }
139
140
141 }