001: /**
002: * Copyright 2003-2007 Luck Consulting Pty Ltd
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package net.sf.ehcache;
016:
017: import org.apache.commons.logging.Log;
018: import org.apache.commons.logging.LogFactory;
019:
020: import java.io.IOException;
021: import java.io.ByteArrayOutputStream;
022: import java.io.ObjectOutputStream;
023: import java.io.ByteArrayInputStream;
024: import java.io.ObjectInputStream;
025:
026: /**
027: * Tests for the statistics class
028: *
029: * @author Greg Luck
030: * @version $Id: StatisticsTest.java 570 2007-12-23 10:07:52Z gregluck $
031: */
032: public class StatisticsTest extends AbstractCacheTest {
033:
034: private static final Log LOG = LogFactory
035: .getLog(StatisticsTest.class.getName());
036:
037: /**
038: * Test statistics directly from Cache
039: */
040: public void testStatistics() throws InterruptedException {
041: //Set size so the second element overflows to disk.
042: Cache cache = new Cache("test", 1, true, false, 5, 2);
043: manager.addCache(cache);
044: cache.put(new Element("key1", "value1"));
045: cache.put(new Element("key2", "value1"));
046:
047: //key1 should be in the Disk Store
048: cache.get("key1");
049: assertEquals(1, cache.getHitCount());
050: assertEquals(1, cache.getDiskStoreHitCount());
051: assertEquals(0, cache.getMemoryStoreHitCount());
052: assertEquals(0, cache.getMissCountExpired());
053: assertEquals(0, cache.getMissCountNotFound());
054:
055: //key 1 should now be in the LruMemoryStore
056: cache.get("key1");
057: assertEquals(2, cache.getHitCount());
058: assertEquals(1, cache.getDiskStoreHitCount());
059: assertEquals(1, cache.getMemoryStoreHitCount());
060: assertEquals(0, cache.getMissCountExpired());
061: assertEquals(0, cache.getMissCountNotFound());
062:
063: //Let the idle expire
064: Thread.sleep(5020);
065:
066: //key 1 should now be expired
067: cache.get("key1");
068: assertEquals(2, cache.getHitCount());
069: assertEquals(1, cache.getDiskStoreHitCount());
070: assertEquals(1, cache.getMemoryStoreHitCount());
071: assertEquals(1, cache.getMissCountExpired());
072: assertEquals(1, cache.getMissCountNotFound());
073: }
074:
075: /**
076: * Test statistics directly from Statistics Object
077: */
078: public void testStatisticsFromStatisticsObject()
079: throws InterruptedException {
080: //Set size so the second element overflows to disk.
081: Cache cache = new Cache("test", 1, true, false, 5, 2);
082: manager.addCache(cache);
083:
084: cache.put(new Element("key1", "value1"));
085: cache.put(new Element("key2", "value1"));
086: //key1 should be in the Disk Store
087: cache.get("key1");
088:
089: Statistics statistics = cache.getStatistics();
090: assertEquals(1, statistics.getCacheHits());
091: assertEquals(1, statistics.getOnDiskHits());
092: assertEquals(0, statistics.getInMemoryHits());
093: assertEquals(0, statistics.getCacheMisses());
094:
095: //key 1 should now be in the LruMemoryStore
096: cache.get("key1");
097:
098: statistics = cache.getStatistics();
099: assertEquals(2, statistics.getCacheHits());
100: assertEquals(1, statistics.getOnDiskHits());
101: assertEquals(1, statistics.getInMemoryHits());
102: assertEquals(0, statistics.getCacheMisses());
103:
104: //Let the idle expire
105: Thread.sleep(5020);
106:
107: //key 1 should now be expired
108: cache.get("key1");
109: statistics = cache.getStatistics();
110: assertEquals(2, statistics.getCacheHits());
111: assertEquals(1, statistics.getOnDiskHits());
112: assertEquals(1, statistics.getInMemoryHits());
113: assertEquals(2, statistics.getCacheMisses());
114:
115: assertNotNull(statistics.toString());
116: }
117:
118: /**
119: * Test statistics directly from Statistics Object
120: */
121: public void testClearStatistics() throws InterruptedException {
122: //Set size so the second element overflows to disk.
123: Cache cache = new Cache("test", 1, true, false, 5, 2);
124: manager.addCache(cache);
125:
126: cache.put(new Element("key1", "value1"));
127: cache.put(new Element("key2", "value1"));
128: //key1 should be in the Disk Store
129: cache.get("key1");
130:
131: Statistics statistics = cache.getStatistics();
132: assertEquals(1, statistics.getCacheHits());
133: assertEquals(1, statistics.getOnDiskHits());
134: assertEquals(0, statistics.getInMemoryHits());
135: assertEquals(0, statistics.getCacheMisses());
136:
137: //clear stats
138: statistics.clearStatistics();
139: statistics = cache.getStatistics();
140: assertEquals(0, statistics.getCacheHits());
141: assertEquals(0, statistics.getOnDiskHits());
142: assertEquals(0, statistics.getInMemoryHits());
143: assertEquals(0, statistics.getCacheMisses());
144: }
145:
146: /**
147: * CacheStatistics should always be sensible when the cache has not started.
148: */
149: public void testCacheStatisticsDegradesElegantlyWhenCacheDisposed() {
150: Cache cache = new Cache("test", 1, true, false, 5, 2);
151: try {
152: Statistics statistics = cache.getStatistics();
153: fail();
154: } catch (IllegalStateException e) {
155: assertEquals("The test Cache is not alive.", e.getMessage());
156: }
157:
158: }
159:
160: /**
161: * We want to be able to use Statistics as a value object.
162: * We need to do some magic with the refernence held to Cache
163: */
164: public void testSerialization() throws IOException,
165: ClassNotFoundException {
166:
167: Cache cache = new Cache("test", 1, true, false, 5, 2);
168: manager.addCache(cache);
169:
170: cache.put(new Element("key1", "value1"));
171: cache.put(new Element("key2", "value1"));
172: cache.get("key1");
173: cache.get("key1");
174:
175: Statistics statistics = cache.getStatistics();
176: assertEquals(2, statistics.getCacheHits());
177: assertEquals(1, statistics.getOnDiskHits());
178: assertEquals(1, statistics.getInMemoryHits());
179: assertEquals(0, statistics.getCacheMisses());
180: assertEquals(Statistics.STATISTICS_ACCURACY_BEST_EFFORT,
181: statistics.getStatisticsAccuracy());
182: statistics.clearStatistics();
183:
184: ByteArrayOutputStream bout = new ByteArrayOutputStream();
185: ObjectOutputStream oos = new ObjectOutputStream(bout);
186: oos.writeObject(statistics);
187: byte[] serializedValue = bout.toByteArray();
188: oos.close();
189: Statistics afterDeserializationStatistics = null;
190: ByteArrayInputStream bin = new ByteArrayInputStream(
191: serializedValue);
192: ObjectInputStream ois = new ObjectInputStream(bin);
193: afterDeserializationStatistics = (Statistics) ois.readObject();
194: ois.close();
195:
196: //Check after Serialization
197: assertEquals(2, afterDeserializationStatistics.getCacheHits());
198: assertEquals(1, afterDeserializationStatistics.getOnDiskHits());
199: assertEquals(1, afterDeserializationStatistics
200: .getInMemoryHits());
201: assertEquals(0, afterDeserializationStatistics.getCacheMisses());
202: assertEquals(Statistics.STATISTICS_ACCURACY_BEST_EFFORT,
203: statistics.getStatisticsAccuracy());
204: statistics.clearStatistics();
205:
206: }
207:
208: /**
209: * What happens when a long larger than int max value is cast to an int?
210: * <p/>
211: * The answer is that negative numbers are reported. The cast value is incorrect.
212: */
213: public void testIntOverflow() {
214:
215: long value = Integer.MAX_VALUE;
216: value += Integer.MAX_VALUE;
217: value += 5;
218: LOG.info("" + value);
219: int valueAsInt = (int) value;
220: LOG.info("" + valueAsInt);
221: assertEquals(3, valueAsInt);
222:
223: }
224:
225: /**
226: * Tests average get time
227: */
228: public void testAverageGetTime() {
229: Ehcache cache = new Cache("test", 0, true, false, 5, 2);
230: manager.addCache(cache);
231: Statistics statistics = cache.getStatistics();
232: float averageGetTime = statistics.getAverageGetTime();
233: assertTrue(0 == statistics.getAverageGetTime());
234:
235: for (int i = 0; i < 10000; i++) {
236: cache.put(new Element("" + i, "value1"));
237: }
238: cache.put(new Element("key1", "value1"));
239: cache.put(new Element("key2", "value1"));
240: for (int i = 0; i < 110000; i++) {
241: cache.get("" + i);
242: }
243:
244: statistics = cache.getStatistics();
245: averageGetTime = statistics.getAverageGetTime();
246: assertTrue(averageGetTime >= .05);
247: statistics.clearStatistics();
248: statistics = cache.getStatistics();
249: assertTrue(0 == statistics.getAverageGetTime());
250: }
251:
252: /**
253: * Tests eviction statistics
254: */
255: public void testEvictionStatistics() throws InterruptedException {
256: //set to 0 to make it run slow
257: Ehcache ehcache = new net.sf.ehcache.Cache("test", 10, false,
258: false, 2, 2);
259: manager.addCache(ehcache);
260: Statistics statistics = ehcache.getStatistics();
261: assertEquals(0, statistics.getEvictionCount());
262:
263: for (int i = 0; i < 10000; i++) {
264: ehcache.put(new Element("" + i, "value1"));
265: }
266: statistics = ehcache.getStatistics();
267: assertEquals(9990, statistics.getEvictionCount());
268:
269: Thread.sleep(2010);
270:
271: //expiries do not count
272: statistics = ehcache.getStatistics();
273: assertEquals(9990, statistics.getEvictionCount());
274:
275: statistics.clearStatistics();
276:
277: statistics = ehcache.getStatistics();
278: assertEquals(0, statistics.getEvictionCount());
279:
280: }
281:
282: }
|