1 17 package org.alfresco.repo.cache; 18 19 import java.io.Serializable ; 20 21 import javax.transaction.Status ; 22 import javax.transaction.UserTransaction ; 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 39 public class CacheTest extends TestCase 40 { 41 private static ApplicationContext ctx =new ClassPathXmlApplicationContext( 42 new String [] {"classpath:cache-test-context.xml", ApplicationContextHelper.CONFIG_LOCATIONS[0]} 43 ); 44 45 private ServiceRegistry serviceRegistry; 46 private SimpleCache<String , Serializable > standaloneCache; 47 private SimpleCache<String , Serializable > backingCache; 48 private SimpleCache<String , Serializable > transactionalCache; 49 50 @SuppressWarnings ("unchecked") 51 @Override 52 public void setUp() throws Exception 53 { 54 serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY); 55 standaloneCache = (SimpleCache<String , Serializable >) ctx.getBean("ehCache1"); 56 backingCache = (SimpleCache<String , Serializable >) ctx.getBean("backingCache"); 57 transactionalCache = (SimpleCache<String , Serializable >) ctx.getBean("transactionalCache"); 58 } 59 60 @Override 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 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 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 94 { 95 String key = "B"; 96 String value = "BBB"; 97 transactionalCache.put(key, value); 99 assertEquals("Backing cache not used for put when no transaction present", value, backingCache.get(key)); 101 102 backingCache.remove(key); 104 assertNull("Backing cache not used for removed when no transaction present", transactionalCache.get(key)); 105 106 backingCache.put(key, value); 108 transactionalCache.remove(key); 110 assertNull("Non-transactional remove didn't go to backing cache", backingCache.get(key)); 112 } 113 114 public void testTransactionalCacheWithTxn() throws Throwable 115 { 116 String newGlobalOne = "new_global_one"; 117 String newGlobalTwo = "new_global_two"; 118 String newGlobalThree = "new_global_three"; 119 String updatedTxnThree = "updated_txn_three"; 120 121 backingCache.put(newGlobalOne, newGlobalOne); 123 backingCache.put(newGlobalTwo, newGlobalTwo); 124 backingCache.put(newGlobalThree, newGlobalThree); 125 126 TransactionService transactionService = serviceRegistry.getTransactionService(); 127 UserTransaction txn = transactionService.getUserTransaction(); 128 try 129 { 130 txn.begin(); 132 133 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 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 txn.commit(); 146 147 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 e) 153 { 154 if (txn.getStatus() == Status.STATUS_ACTIVE) 155 { 156 txn.rollback(); 157 } 158 throw e; 159 } 160 } 161 162 170 public long runPerformanceTestOnCache(SimpleCache<String , Serializable > cache, int objectCount) 171 { 172 for (int i = 0; i < objectCount; i++) 174 { 175 String key = Integer.toString(i); 176 Integer value = new Integer (i); 177 cache.put(key, value); 178 } 179 180 long start = System.nanoTime(); 182 for (int i = 0; i < objectCount; i++) 183 { 184 String key = Integer.toString(i); 185 cache.remove(key); 186 key = Integer.toString(i + objectCount); 188 Integer value = new Integer (i + objectCount); 189 cache.put(key, value); 190 } 191 long stop = System.nanoTime(); 193 194 return (stop - start); 195 } 196 197 201 public void testPerformance() throws Exception 202 { 203 for (int i = 0; i < 5; i++) 204 { 205 int count = (int) Math.pow(10D, (double)i); 206 207 long timePlain = runPerformanceTestOnCache(standaloneCache, count); 209 210 TransactionService transactionService = serviceRegistry.getTransactionService(); 212 UserTransaction 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 timeTxn += commitTime; 221 222 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 |