View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
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;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.ByteArrayOutputStream;
26  import java.io.IOException;
27  import java.io.ObjectInputStream;
28  import java.io.ObjectOutputStream;
29  
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import org.junit.Test;
34  
35  /***
36   * Tests for the statistics class
37   *
38   * @author Greg Luck
39   * @version $Id: StatisticsTest.html 13146 2011-08-01 17:12:39Z oletizi $
40   */
41  public class StatisticsTest extends AbstractCacheTest {
42  
43      private static final Logger LOG = LoggerFactory.getLogger(StatisticsTest.class.getName());
44  
45      /***
46       * Test statistics directly from Statistics Object
47       */
48      @Test
49      public void testStatisticsFromStatisticsObject()
50              throws InterruptedException {
51          // Set size so the second element overflows to disk.
52          Cache cache = new Cache("test", 1, true, false, 5, 2);
53          manager.addCache(cache);
54  
55          cache.setStatisticsEnabled(true);
56  
57          cache.put(new Element("key1", "value1"));
58          cache.put(new Element("key2", "value1"));
59  
60          // allow disk write thread to complete
61          Thread.sleep(100);
62  
63          cache.get("key1");
64          cache.get("key2");
65  
66          Statistics statistics = cache.getStatistics();
67          assertEquals(2, statistics.getCacheHits());
68          assertEquals(1, statistics.getOnDiskHits());
69          assertEquals(1, statistics.getInMemoryHits());
70          assertEquals(0, statistics.getCacheMisses());
71          assertEquals(2, statistics.getObjectCount());
72          assertEquals(1, statistics.getMemoryStoreObjectCount());
73          assertEquals(2, statistics.getDiskStoreObjectCount());
74  
75          // key 2 should now be in the MemoryStore
76          cache.get("key2");
77  
78          statistics = cache.getStatistics();
79          assertEquals(3, statistics.getCacheHits());
80          assertEquals(1, statistics.getOnDiskHits());
81          assertEquals(2, statistics.getInMemoryHits());
82          assertEquals(0, statistics.getCacheMisses());
83  
84          // Let the idle expire
85          Thread.sleep(6000);
86  
87          // key 1 should now be expired
88          cache.get("key1");
89          statistics = cache.getStatistics();
90          assertEquals(3, statistics.getCacheHits());
91          assertEquals(1, statistics.getOnDiskHits());
92          assertEquals(2, statistics.getInMemoryHits());
93          assertEquals(1, statistics.getCacheMisses());
94  
95          // key 2 should also be expired
96          cache.get("key2");
97          statistics = cache.getStatistics();
98          assertEquals(3, statistics.getCacheHits());
99          assertEquals(1, statistics.getOnDiskHits());
100         assertEquals(2, statistics.getInMemoryHits());
101         assertEquals(2, statistics.getCacheMisses());
102 
103         assertNotNull(statistics.toString());
104     }
105 
106     /***
107      * Test statistics directly from Statistics Object
108      */
109     @Test
110     public void testClearStatistics() throws InterruptedException {
111         // Set size so the second element overflows to disk.
112         Cache cache = new Cache("test", 1, true, false, 5, 2);
113         manager.addCache(cache);
114 
115         cache.setStatisticsEnabled(true);
116 
117         cache.put(new Element("key1", "value1"));
118         cache.put(new Element("key2", "value1"));
119 
120         // allow disk write thread to complete
121         Thread.sleep(100);
122 
123         cache.get("key1");
124         cache.get("key2");
125 
126         Statistics statistics = cache.getStatistics();
127         assertEquals(2, statistics.getCacheHits());
128         assertEquals(1, statistics.getOnDiskHits());
129         assertEquals(1, statistics.getInMemoryHits());
130         assertEquals(0, statistics.getCacheMisses());
131 
132         // clear stats
133         statistics.clearStatistics();
134         statistics = cache.getStatistics();
135         assertEquals(0, statistics.getCacheHits());
136         assertEquals(0, statistics.getOnDiskHits());
137         assertEquals(0, statistics.getInMemoryHits());
138         assertEquals(0, statistics.getCacheMisses());
139     }
140 
141     /***
142      * CacheStatistics should always be sensible when the cache has not started.
143      */
144     @Test
145     public void testCacheStatisticsDegradesElegantlyWhenCacheDisposed() {
146         Cache cache = new Cache("test", 1, true, false, 5, 2);
147         try {
148             Statistics statistics = cache.getStatistics();
149             fail();
150         } catch (IllegalStateException e) {
151             assertEquals("The test Cache is not alive (STATUS_UNINITIALISED)", e.getMessage());
152         }
153 
154     }
155 
156     /***
157      * We want to be able to use Statistics as a value object.
158      * We need to do some magic with the refernence held to Cache
159      */
160     @Test
161     public void testSerialization() throws IOException, ClassNotFoundException, InterruptedException {
162         Cache cache = new Cache("test", 1, true, false, 5, 2);
163         manager.addCache(cache);
164 
165         cache.setStatisticsEnabled(true);
166 
167         cache.put(new Element("key1", "value1"));
168         cache.put(new Element("key2", "value1"));
169 
170         // allow disk write thread to complete
171         Thread.sleep(100);
172 
173         cache.get("key1");
174         cache.get("key2");
175 
176         Statistics statistics = cache.getStatistics();
177         assertEquals("test", statistics.getAssociatedCacheName());
178         assertEquals(2, statistics.getCacheHits());
179         assertEquals(1, statistics.getOnDiskHits());
180         assertEquals(1, statistics.getInMemoryHits());
181         assertEquals(0, statistics.getCacheMisses());
182         assertEquals(Statistics.STATISTICS_ACCURACY_BEST_EFFORT, statistics
183                 .getStatisticsAccuracy());
184         statistics.clearStatistics();
185 
186         ByteArrayOutputStream bout = new ByteArrayOutputStream();
187         ObjectOutputStream oos = new ObjectOutputStream(bout);
188         oos.writeObject(statistics);
189         byte[] serializedValue = bout.toByteArray();
190         oos.close();
191         Statistics afterDeserializationStatistics = null;
192         ByteArrayInputStream bin = new ByteArrayInputStream(serializedValue);
193         ObjectInputStream ois = new ObjectInputStream(bin);
194         afterDeserializationStatistics = (Statistics) ois.readObject();
195         ois.close();
196 
197         // Check after Serialization
198         assertEquals("test", afterDeserializationStatistics.getAssociatedCacheName());
199         assertEquals(2, afterDeserializationStatistics.getCacheHits());
200         assertEquals(1, afterDeserializationStatistics.getOnDiskHits());
201         assertEquals(1, afterDeserializationStatistics.getInMemoryHits());
202         assertEquals(0, afterDeserializationStatistics.getCacheMisses());
203         assertEquals(Statistics.STATISTICS_ACCURACY_BEST_EFFORT, statistics
204                 .getStatisticsAccuracy());
205         statistics.clearStatistics();
206 
207     }
208 
209     /***
210      * Tests average get time
211      */
212     @Test
213     public void testAverageGetTime() {
214         Ehcache cache = new Cache("test", 0, true, false, 5, 2);
215         manager.addCache(cache);
216 
217         cache.setStatisticsEnabled(true);
218 
219         Statistics statistics = cache.getStatistics();
220         float averageGetTime = statistics.getAverageGetTime();
221         assertTrue(0 == statistics.getAverageGetTime());
222 
223         for (int i = 0; i < 10000; i++) {
224             cache.put(new Element("" + i, "value1"));
225         }
226         cache.put(new Element("key1", "value1"));
227         cache.put(new Element("key2", "value1"));
228         for (int i = 0; i < 110000; i++) {
229             cache.get("" + i);
230         }
231 
232         statistics = cache.getStatistics();
233         averageGetTime = statistics.getAverageGetTime();
234         assertTrue(averageGetTime >= .000001);
235         statistics.clearStatistics();
236         statistics = cache.getStatistics();
237         assertTrue(0 == statistics.getAverageGetTime());
238     }
239 
240     /***
241      * Tests eviction statistics
242      */
243     @Test
244     public void testEvictionStatistics() throws InterruptedException {
245         // set to 0 to make it run slow
246         Ehcache ehcache = new net.sf.ehcache.Cache("test", 10, false, false, 2,
247                 2);
248         manager.addCache(ehcache);
249 
250         ehcache.setStatisticsEnabled(true);
251 
252         Statistics statistics = ehcache.getStatistics();
253         assertEquals(0, statistics.getEvictionCount());
254 
255         for (int i = 0; i < 10000; i++) {
256             ehcache.put(new Element("" + i, "value1"));
257         }
258         statistics = ehcache.getStatistics();
259         assertEquals(9990, statistics.getEvictionCount());
260 
261         Thread.sleep(2010);
262 
263         // expiries do not count
264         statistics = ehcache.getStatistics();
265         assertEquals(9990, statistics.getEvictionCount());
266 
267         statistics.clearStatistics();
268 
269         statistics = ehcache.getStatistics();
270         assertEquals(0, statistics.getEvictionCount());
271 
272     }
273 
274 }