KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > cache > CacheTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.cache;
18
19 import java.io.Serializable JavaDoc;
20
21 import javax.transaction.Status JavaDoc;
22 import javax.transaction.UserTransaction JavaDoc;
23
24 import net.sf.ehcache.CacheManager;
25
26 import org.alfresco.service.ServiceRegistry;
27 import org.alfresco.service.transaction.TransactionService;
28 import org.alfresco.util.ApplicationContextHelper;
29 import org.springframework.context.ApplicationContext;
30 import org.springframework.context.support.ClassPathXmlApplicationContext;
31
32 import junit.framework.TestCase;
33
34 /**
35  * @see org.alfresco.repo.cache.EhCacheAdapter
36  *
37  * @author Derek Hulley
38  */

39 public class CacheTest extends TestCase
40 {
41     private static ApplicationContext ctx =new ClassPathXmlApplicationContext(
42             new String JavaDoc[] {"classpath:cache-test-context.xml", ApplicationContextHelper.CONFIG_LOCATIONS[0]}
43             );
44     
45     private ServiceRegistry serviceRegistry;
46     private SimpleCache<String JavaDoc, Serializable JavaDoc> standaloneCache;
47     private SimpleCache<String JavaDoc, Serializable JavaDoc> backingCache;
48     private SimpleCache<String JavaDoc, Serializable JavaDoc> transactionalCache;
49     
50     @SuppressWarnings JavaDoc("unchecked")
51     @Override JavaDoc
52     public void setUp() throws Exception JavaDoc
53     {
54         serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
55         standaloneCache = (SimpleCache<String JavaDoc, Serializable JavaDoc>) ctx.getBean("ehCache1");
56         backingCache = (SimpleCache<String JavaDoc, Serializable JavaDoc>) ctx.getBean("backingCache");
57         transactionalCache = (SimpleCache<String JavaDoc, Serializable JavaDoc>) ctx.getBean("transactionalCache");
58     }
59     
60     @Override JavaDoc
61     public void tearDown()
62     {
63         serviceRegistry = null;
64         standaloneCache = null;
65         backingCache = null;
66         transactionalCache = null;
67     }
68     
69     public void testSetUp() throws Exception JavaDoc
70     {
71         CacheManager cacheManager = (CacheManager) ctx.getBean("ehCacheManager");
72         assertNotNull(cacheManager);
73         CacheManager cacheManagerCheck = (CacheManager) ctx.getBean("ehCacheManager");
74         assertTrue(cacheManager == cacheManagerCheck);
75     
76         assertNotNull(serviceRegistry);
77         assertNotNull(backingCache);
78         assertNotNull(standaloneCache);
79         assertNotNull(transactionalCache);
80     }
81     
82     public void testEhcacheAdaptors() throws Exception JavaDoc
83     {
84         backingCache.put("A", "AAA");
85         assertNull("Second cache should not have first's present", standaloneCache.get("A"));
86         
87         assertEquals("AAA", backingCache.get("A"));
88         
89         backingCache.remove("A");
90         assertNull(backingCache.get("A"));
91     }
92     
93     public void testTransactionalCacheNoTxn() throws Exception JavaDoc
94     {
95         String JavaDoc key = "B";
96         String JavaDoc value = "BBB";
97         // no transaction - do a put
98
transactionalCache.put(key, value);
99         // check that the value appears in the backing cache, backingCache
100
assertEquals("Backing cache not used for put when no transaction present", value, backingCache.get(key));
101         
102         // remove the value from the backing cache and check that it is removed from the transaction cache
103
backingCache.remove(key);
104         assertNull("Backing cache not used for removed when no transaction present", transactionalCache.get(key));
105         
106         // add value into backing cache
107
backingCache.put(key, value);
108         // remove it from the transactional cache
109
transactionalCache.remove(key);
110         // check that it is gone from the backing cache
111
assertNull("Non-transactional remove didn't go to backing cache", backingCache.get(key));
112     }
113     
114     public void testTransactionalCacheWithTxn() throws Throwable JavaDoc
115     {
116         String JavaDoc newGlobalOne = "new_global_one";
117         String JavaDoc newGlobalTwo = "new_global_two";
118         String JavaDoc newGlobalThree = "new_global_three";
119         String JavaDoc updatedTxnThree = "updated_txn_three";
120         
121         // add item to global cache
122
backingCache.put(newGlobalOne, newGlobalOne);
123         backingCache.put(newGlobalTwo, newGlobalTwo);
124         backingCache.put(newGlobalThree, newGlobalThree);
125         
126         TransactionService transactionService = serviceRegistry.getTransactionService();
127         UserTransaction JavaDoc txn = transactionService.getUserTransaction();
128         try
129         {
130             // begin a transaction
131
txn.begin();
132             
133             // remove 1 from the cache
134
transactionalCache.remove(newGlobalOne);
135             assertFalse("Item was not removed from txn cache", transactionalCache.contains(newGlobalOne));
136             assertNull("Get didn't return null", transactionalCache.get(newGlobalOne));
137             assertTrue("Item was removed from backing cache", backingCache.contains(newGlobalOne));
138             
139             // update 3 in the cache
140
transactionalCache.put(updatedTxnThree, "XXX");
141             assertEquals("Item not updated in txn cache", "XXX", transactionalCache.get(updatedTxnThree));
142             assertFalse("Item was put into backing cache", backingCache.contains(updatedTxnThree));
143             
144             // commit the transaction
145
txn.commit();
146             
147             // check that backing cache was updated with the in-transaction changes
148
assertFalse("Item was not removed from backing cache", backingCache.contains(newGlobalOne));
149             assertNull("Item could still be fetched from backing cache", backingCache.get(newGlobalOne));
150             assertEquals("Item not updated in backing cache", "XXX", backingCache.get(updatedTxnThree));
151         }
152         catch (Throwable JavaDoc e)
153         {
154             if (txn.getStatus() == Status.STATUS_ACTIVE)
155             {
156                 txn.rollback();
157             }
158             throw e;
159         }
160     }
161     
162     /**
163      * Preloads the cache, then performs a simultaneous addition of N new values and
164      * removal of the N preloaded values.
165      *
166      * @param cache
167      * @param objectCount
168      * @return Returns the time it took in <b>nanoseconds</b>.
169      */

170     public long runPerformanceTestOnCache(SimpleCache<String JavaDoc, Serializable JavaDoc> cache, int objectCount)
171     {
172         // preload
173
for (int i = 0; i < objectCount; i++)
174         {
175             String JavaDoc key = Integer.toString(i);
176             Integer JavaDoc value = new Integer JavaDoc(i);
177             cache.put(key, value);
178         }
179         
180         // start timer
181
long start = System.nanoTime();
182         for (int i = 0; i < objectCount; i++)
183         {
184             String JavaDoc key = Integer.toString(i);
185             cache.remove(key);
186             // add a new value
187
key = Integer.toString(i + objectCount);
188             Integer JavaDoc value = new Integer JavaDoc(i + objectCount);
189             cache.put(key, value);
190         }
191         // stop
192
long stop = System.nanoTime();
193         
194         return (stop - start);
195     }
196     
197     /**
198      * Tests a straight Ehcache adapter against a transactional cache both in and out
199      * of a transaction. This is done repeatedly, pushing the count up.
200      */

201     public void testPerformance() throws Exception JavaDoc
202     {
203         for (int i = 0; i < 5; i++)
204         {
205             int count = (int) Math.pow(10D, (double)i);
206             
207             // test standalone
208
long timePlain = runPerformanceTestOnCache(standaloneCache, count);
209             
210             // do transactional cache in a transaction
211
TransactionService transactionService = serviceRegistry.getTransactionService();
212             UserTransaction JavaDoc txn = transactionService.getUserTransaction();
213             txn.begin();
214             long timeTxn = runPerformanceTestOnCache(transactionalCache, count);
215             long commitStart = System.nanoTime();
216             txn.commit();
217             long commitEnd = System.nanoTime();
218             long commitTime = (commitEnd - commitStart);
219             // add this to the cache's performance overhead
220
timeTxn += commitTime;
221             
222             // report
223
System.out.println("Cache performance test: \n" +
224                     " count: " + count + "\n" +
225                     " direct: " + timePlain/((long)count) + " ns\\count \n" +
226                     " transaction: " + timeTxn/((long)count) + " ns\\count");
227         }
228     }
229 }
230
Popular Tags