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.jcache;
016:
017: import net.sf.ehcache.AbstractCacheTest;
018: import net.sf.ehcache.Ehcache;
019: import net.sf.ehcache.Element;
020: import net.sf.jsr107cache.Cache;
021: import net.sf.jsr107cache.CacheStatistics;
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import java.io.ByteArrayInputStream;
026: import java.io.ByteArrayOutputStream;
027: import java.io.IOException;
028: import java.io.ObjectInputStream;
029: import java.io.ObjectOutputStream;
030:
031: /**
032: * Tests for the statistics class
033: *
034: * @author Greg Luck
035: * @version $Id:JCacheStatisticsTest.java 318 2007-01-25 01:48:35Z gregluck $
036: */
037: public class JCacheStatisticsTest extends AbstractCacheTest {
038:
039: private static final Log LOG = LogFactory
040: .getLog(JCacheStatisticsTest.class.getName());
041:
042: /**
043: * Test statistics directly from Statistics Object
044: */
045: public void testStatisticsFromStatisticsObject()
046: throws InterruptedException {
047: //Set size so the second element overflows to disk.
048: Ehcache ehcache = new net.sf.ehcache.Cache("testStatistics", 1,
049: true, false, 5, 2);
050: manager.addCache(ehcache);
051: JCache cache = new JCache(ehcache, null);
052: exerciseStatistics(cache);
053:
054: //Exercise aftter setting accuracy
055: ehcache = new net.sf.ehcache.Cache("testStatistics2", 1, true,
056: false, 5, 2);
057: manager.addCache(ehcache);
058: cache = new JCache(ehcache, null);
059: cache
060: .setStatisticsAccuracy(CacheStatistics.STATISTICS_ACCURACY_NONE);
061: exerciseStatistics(cache);
062:
063: ehcache = new net.sf.ehcache.Cache("testStatistics4", 1, true,
064: false, 5, 2);
065: manager.addCache(ehcache);
066: cache = new JCache(ehcache, null);
067: cache
068: .setStatisticsAccuracy(CacheStatistics.STATISTICS_ACCURACY_BEST_EFFORT);
069: exerciseStatistics(cache);
070:
071: }
072:
073: private void exerciseStatistics(Cache cache)
074: throws InterruptedException {
075: cache.put("key1", "value1");
076: cache.put("key2", "value1");
077: //key1 should be in the Disk Store
078: cache.get("key1");
079:
080: CacheStatistics statistics = cache.getCacheStatistics();
081: assertEquals(1, statistics.getCacheHits());
082: assertEquals(0, statistics.getCacheMisses());
083:
084: //key 1 should now be in the LruMemoryStore
085: cache.get("key1");
086:
087: statistics = cache.getCacheStatistics();
088: assertEquals(2, statistics.getCacheHits());
089: assertEquals(0, statistics.getCacheMisses());
090:
091: //Let the idle expire
092: Thread.sleep(5020);
093:
094: //key 1 should now be expired
095: cache.get("key1");
096: statistics = cache.getCacheStatistics();
097: assertEquals(2, statistics.getCacheHits());
098: assertEquals(2, statistics.getCacheMisses());
099: assertNotNull(statistics.toString());
100: }
101:
102: /**
103: * CacheStatistics should always be sensible when the cache has not started.
104: */
105: public void testCacheStatisticsDegradesElegantlyWhenCacheDisposed() {
106: Ehcache ehcache = new net.sf.ehcache.Cache("test", 1, true,
107: false, 5, 2);
108: Cache cache = new JCache(ehcache, null);
109: try {
110: CacheStatistics statistics = cache.getCacheStatistics();
111: fail();
112: } catch (IllegalStateException e) {
113: assertEquals("The test Cache is not alive.", e.getMessage());
114: }
115:
116: }
117:
118: /**
119: * We want to be able to use Statistics as a value object.
120: * We need to do some magic with the reference held to Cache
121: */
122: public void testSerialization() throws IOException,
123: ClassNotFoundException {
124:
125: Ehcache ehcache = new net.sf.ehcache.Cache("test", 1, true,
126: false, 5, 2);
127: manager.addCache(ehcache);
128: Cache cache = new JCache(ehcache, null);
129: cache.put("key1", "value1");
130: cache.put("key2", "value1");
131: cache.get("key1");
132: cache.get("key1");
133:
134: CacheStatistics statistics = cache.getCacheStatistics();
135: assertEquals(2, statistics.getCacheHits());
136: assertEquals(0, statistics.getCacheMisses());
137: assertEquals(CacheStatistics.STATISTICS_ACCURACY_BEST_EFFORT,
138: statistics.getStatisticsAccuracy());
139: statistics.clearStatistics();
140:
141: ByteArrayOutputStream bout = new ByteArrayOutputStream();
142: ObjectOutputStream oos = new ObjectOutputStream(bout);
143: oos.writeObject(statistics);
144: byte[] serializedValue = bout.toByteArray();
145: oos.close();
146: CacheStatistics afterDeserializationStatistics = null;
147: ByteArrayInputStream bin = new ByteArrayInputStream(
148: serializedValue);
149: ObjectInputStream ois = new ObjectInputStream(bin);
150: afterDeserializationStatistics = (CacheStatistics) ois
151: .readObject();
152: ois.close();
153:
154: //Check after Serialization
155: assertEquals(2, afterDeserializationStatistics.getCacheHits());
156: assertEquals(0, afterDeserializationStatistics.getCacheMisses());
157: assertEquals(CacheStatistics.STATISTICS_ACCURACY_BEST_EFFORT,
158: statistics.getStatisticsAccuracy());
159: statistics.clearStatistics();
160:
161: }
162:
163: /**
164: * Test statistics directly from Statistics Object
165: */
166: public void testClearStatistics() throws InterruptedException {
167: //Set size so the second element overflows to disk.
168: Ehcache ehcache = new net.sf.ehcache.Cache("test", 1, true,
169: false, 5, 2);
170: manager.addCache(ehcache);
171: Cache cache = new JCache(ehcache, null);
172:
173: cache.put("key1", "value1");
174: cache.put("key2", "value1");
175: //key1 should be in the Disk Store
176: cache.get("key1");
177:
178: CacheStatistics statistics = cache.getCacheStatistics();
179: assertEquals(1, statistics.getCacheHits());
180: assertEquals(0, statistics.getCacheMisses());
181:
182: //clear stats
183: statistics.clearStatistics();
184: statistics = cache.getCacheStatistics();
185: assertEquals(0, statistics.getCacheHits());
186: assertEquals(0, statistics.getCacheMisses());
187: }
188:
189: /**
190: * Tests average get time
191: */
192: public void testAverageGetTime() {
193: //set to 0 to make it run slow
194: Ehcache ehcache = new net.sf.ehcache.Cache("test", 0, true,
195: false, 5, 2);
196: manager.addCache(ehcache);
197: Cache cache = new JCache(ehcache, null);
198: JCacheStatistics statistics = (JCacheStatistics) cache
199: .getCacheStatistics();
200: float averageGetTime = statistics.getAverageGetTime();
201: assertTrue(0 == statistics.getAverageGetTime());
202:
203: for (int i = 0; i < 10000; i++) {
204: ehcache.put(new Element("" + i, "value1"));
205: }
206: ehcache.put(new Element("key1", "value1"));
207: ehcache.put(new Element("key2", "value1"));
208: for (int i = 0; i < 110000; i++) {
209: ehcache.get("" + i);
210: }
211:
212: statistics = (JCacheStatistics) cache.getCacheStatistics();
213: averageGetTime = statistics.getAverageGetTime();
214: assertTrue(averageGetTime >= .05);
215: statistics.clearStatistics();
216: statistics = (JCacheStatistics) cache.getCacheStatistics();
217: assertTrue(0 == statistics.getAverageGetTime());
218: }
219:
220: /**
221: * Tests eviction statistics
222: */
223: public void testEvictionStatistics() throws InterruptedException {
224: //set to 0 to make it run slow
225: Ehcache ehcache = new net.sf.ehcache.Cache("test", 10, false,
226: false, 2, 2);
227: manager.addCache(ehcache);
228: Cache cache = new JCache(ehcache, null);
229: JCacheStatistics statistics = (JCacheStatistics) cache
230: .getCacheStatistics();
231: assertEquals(0, statistics.getEvictionCount());
232:
233: for (int i = 0; i < 10000; i++) {
234: ehcache.put(new Element("" + i, "value1"));
235: }
236: statistics = (JCacheStatistics) cache.getCacheStatistics();
237: assertEquals(9990, statistics.getEvictionCount());
238:
239: Thread.sleep(2010);
240:
241: //expiries do not count
242: statistics = (JCacheStatistics) cache.getCacheStatistics();
243: assertEquals(9990, statistics.getEvictionCount());
244:
245: statistics.clearStatistics();
246:
247: statistics = (JCacheStatistics) cache.getCacheStatistics();
248: assertEquals(0, statistics.getEvictionCount());
249:
250: }
251:
252: }
|