View Javadoc

1   /**
2    *  Copyright 2003-2006 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  
23  /**
24   * Test cases for the LruMemoryStore.
25   * <p/>
26   * There are no tests of expiry because this is handled by {@link net.sf.ehcache.Cache#get}
27   * <p/>
28   * <b>Performance:</b>
29   * v1.38 of DiskStore
30   * INFO: Time for benhmarkPutGetSurya: 7355
31   * INFO: Time for Bulk Load: 13
32   * INFO: Time for benhmarkPutGetRemove: 264
33   * INFO: Time for benhmarkPutGet: 154
34   * <p/>
35   * v 1.42 of DiskStore
36   * INFO: Time for Bulk Load: 12
37   * INFO: Time for benhmarkPutGetRemove: 256
38   * INFO: Time for benhmarkPutGet: 165
39   *
40   * @author Greg Luck
41   * @version $Id: LruMemoryStoreTest.java 28 2006-04-15 05:12:32Z gregluck $
42   */
43  public class LruMemoryStoreTest extends MemoryStoreTester {
44  
45      /**
46       * setup test
47       */
48      protected void setUp() throws Exception {
49          super.setUp();
50          createMemoryStore(MemoryStoreEvictionPolicy.LRU);
51      }
52  
53  
54      /**
55       * Benchmark to test speed. This uses both memory and disk and tries to be realistic
56       * v 1.38 DiskStore 7355
57       * v 1.41 DiskStore 1609
58       * Adjusted for change to laptop
59       */
60      public void testBenchmarkPutGetSurya() throws Exception {
61          benchmarkPutGetSuryaTest(2500);
62      }
63  
64      /**
65       * Test the LRU policy
66       */
67      public void testPolicy() throws Exception {
68          createMemoryStore(MemoryStoreEvictionPolicy.LRU, 5);
69  
70          //Make sure that the store is empty to start with
71          assertEquals(0, store.getSize());
72  
73          // Populate the store till the max limit
74          Element element = new Element("key1", "value1");
75          store.put(element);
76          assertEquals(1, store.getSize());
77  
78          element = new Element("key2", "value2");
79          store.put(element);
80          assertEquals(2, store.getSize());
81  
82          element = new Element("key3", "value3");
83          store.put(element);
84          assertEquals(3, store.getSize());
85  
86          element = new Element("key4", "value4");
87          store.put(element);
88          assertEquals(4, store.getSize());
89  
90          element = new Element("key5", "value5");
91          store.put(element);
92          assertEquals(5, store.getSize());
93  
94          // Now access the elements to boost the hits count, although irrelevant for this test just to demonstrate
95          // hit count is immaterial for this test.
96          store.get("key1");
97          store.get("key1");
98          store.get("key3");
99          store.get("key3");
100         store.get("key3");
101         store.get("key4");
102 
103         //Create a new element and put in the store so as to force the policy
104         element = new Element("key6", "value6");
105         store.put(element);
106 
107         //max size
108         assertEquals(5, store.getSize());
109 
110         //The element with key "key2" should be the least recently used
111         assertNull(store.get("key2"));
112 
113         // Make some more accesses
114         store.get("key5");
115         store.get("key5");
116 
117         // Insert another element to force the policy
118         element = new Element("key7", "value7");
119         store.put(element);
120         assertEquals(5, store.getSize());
121 
122         //key1 should now be the least recently used.
123         assertNull(store.get("key1"));
124     }
125 
126 
127 }