1 8 9 package org.jboss.test.cache.perf.aop; 10 11 12 import junit.framework.Test; 13 import junit.framework.TestCase; 14 import junit.framework.TestSuite; 15 import org.jboss.cache.PropertyConfigurator; 16 import org.jboss.cache.TreeCache; 17 import org.jboss.cache.aop.TreeCacheAop; 18 import org.jboss.cache.lock.IsolationLevel; 19 import org.jboss.cache.lock.LockStrategyFactory; 20 import org.jboss.cache.transaction.DummyTransactionManager; 21 22 import javax.naming.Context ; 23 import javax.naming.InitialContext ; 24 import javax.transaction.UserTransaction ; 25 import java.text.DecimalFormat ; 26 import java.text.FieldPosition ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import java.util.Properties ; 31 32 38 public class ReplicatedSyncMapPerfAopTest extends TestCase 39 { 40 TreeCacheAop cache_; 41 int cachingMode_ = TreeCache.LOCAL; 42 final static Properties p_; 43 String oldFactory_ = null; 45 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 46 DummyTransactionManager tm_; 47 Map [] proxyMaps_ = null; 48 ArrayList nodeList_; 49 static final int depth_ = 3; 50 static final int children_ = 4; 51 static final int mapValueSize_ = 100; 52 static final String seed1_ = "This is a test. "; 53 static final String seed2_ = "THAT is a TEST. "; 54 StringBuffer originalStrBuf_; 55 StringBuffer newStrBuf_; 56 57 static 58 { 59 p_ = new Properties (); 60 p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 61 } 62 63 public ReplicatedSyncMapPerfAopTest(String name) 64 { 65 super(name); 66 } 67 68 public void setUp() throws Exception 69 { 70 super.setUp(); 71 72 oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 73 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 74 75 DummyTransactionManager.getInstance(); 76 initCaches(TreeCache.LOCAL); 77 tm_ = new DummyTransactionManager(); 78 79 originalStrBuf_ = new StringBuffer (); 80 newStrBuf_ = new StringBuffer (); 81 generateString(); 82 log("ReplicatedSyncPerfAopTest: cacheMode=ReplSync"); 83 nodeList_ = nodeGen(depth_, children_); 84 populateNode(); 85 } 86 87 private void generateString() 88 { 89 int length = seed1_.length(); 90 boolean isTrue = false; 91 while (originalStrBuf_.length() < mapValueSize_) { 92 originalStrBuf_.append(seed1_); 93 newStrBuf_.append(seed2_); 94 } 95 } 96 97 private void populateNode() throws Exception 98 { 99 DecimalFormat form = new DecimalFormat ("#.00"); 101 FieldPosition fieldPos = new FieldPosition (0); 102 StringBuffer dumbStr = new StringBuffer (); 103 proxyMaps_ = new Map [nodeList_.size()]; 104 105 long time1 = System.currentTimeMillis(); 106 int nOps = 0; 107 for (int i = 0; i < nodeList_.size(); i++) { 108 Map map = populateMap(); 111 cache_.putObject((String ) nodeList_.get(i), map); 112 proxyMaps_[i] = (Map ) cache_.getObject((String ) nodeList_.get(i)); 113 } 114 115 nOps = nodeList_.size(); 116 long time2 = System.currentTimeMillis(); 117 double d = (double) (time2 - time1) / nOps; 118 log("Time elapsed for one putObject and getObject entry is " + (time2 - time1) + " with " + nOps 119 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 120 " msec."); 121 } 122 123 private Map populateMap() 124 { 125 Map map = new HashMap (); 126 for (int i = 0; i < nodeList_.size(); i++) { 127 String key = Integer.toString(i) + "aop"; 128 String value = originalStrBuf_.toString(); 129 map.put(key, value); 130 } 131 return map; 132 } 133 134 public void tearDown() throws Exception 135 { 136 super.tearDown(); 137 138 DummyTransactionManager.destroy(); 139 destroyCaches(); 140 141 if (oldFactory_ != null) { 142 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_); 143 oldFactory_ = null; 144 } 145 146 proxyMaps_ = null; 147 } 148 149 void initCaches(int caching_mode) throws Exception 150 { 151 cachingMode_ = caching_mode; 152 cache_ = new TreeCacheAop(); 153 PropertyConfigurator config = new PropertyConfigurator(); 154 config.configure(cache_, "META-INF/replSync-service.xml"); cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 156 cache_.startService(); 157 } 159 160 void destroyCaches() throws Exception 161 { 162 cache_.stopService(); 163 cache_ = null; 164 } 165 166 public void testAll() throws Exception 167 { 168 169 log("=== Start ==="); 170 172 DecimalFormat form = new DecimalFormat ("#.00"); 174 FieldPosition fieldPos = new FieldPosition (0); 175 StringBuffer dumbStr = new StringBuffer (); 176 boolean hasTx = false; 177 178 long time1 = System.currentTimeMillis(); 180 int nOps = 0; 181 nOps = _put(hasTx); 182 long time2 = System.currentTimeMillis(); 183 double d = (double) (time2 - time1) / nOps; 184 log("Time elapsed for put entry is " + (time2 - time1) + " with " + nOps 185 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 186 " msec."); 187 dumbStr = new StringBuffer (); 188 189 time1 = System.currentTimeMillis(); 191 nOps = _get(hasTx); 192 time2 = System.currentTimeMillis(); 193 d = (double) (time2 - time1) / nOps; 194 log("Time elapsed for get entry is " + (time2 - time1) + " with " + nOps 195 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 196 " msec."); 197 dumbStr = new StringBuffer (); 198 199 time1 = System.currentTimeMillis(); 201 nOps = _remove(hasTx); 202 time2 = System.currentTimeMillis(); 203 d = (double) (time2 - time1) / nOps; 204 log("Time elapsed for remove entry is " + (time2 - time1) + " with " + nOps 205 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 206 " msec."); 207 log("=== End ==="); 208 } 209 210 protected void setLevelRW() 211 { 212 log("set lock level to RWUpgrade ..."); 213 LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 214 } 215 216 protected void setLevelSerial() 217 { 218 log("set lock level to SimpleLock ..."); 219 LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE); 220 } 221 222 private int _put(boolean hasTx) throws Exception 223 { 224 UserTransaction tx = null; 225 if (hasTx) { 226 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 227 } 228 229 String value = newStrBuf_.toString(); 230 for (int i = 0; i < nodeList_.size(); i++) { 231 String key = Integer.toString(i) + "aop"; 232 if (hasTx) { 233 tx.begin(); 234 proxyMaps_[i].put(key, value); 235 tx.commit(); 236 } else { 237 proxyMaps_[i].put(key, value); 238 } 239 } 240 241 return nodeList_.size(); 242 } 243 244 private int _get(boolean hasTx) throws Exception 245 { 246 UserTransaction tx = null; 247 if (hasTx) { 248 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 249 } 250 251 for (int i = 1; i < nodeList_.size(); i++) { 252 String key = Integer.toString(i) + "aop"; 253 if (hasTx) { 254 tx.begin(); 255 String str = (String ) proxyMaps_[i].get(key); 256 tx.commit(); 258 } else { 259 String str = (String ) proxyMaps_[i].get(key); 260 } 262 } 263 264 return nodeList_.size(); 265 } 266 267 private int _remove(boolean hasTx) throws Exception 268 { 269 UserTransaction tx = null; 270 if (hasTx) { 271 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 272 } 273 274 for (int i = 1; i < nodeList_.size(); i++) { 275 String key = Integer.toString(i) + "aop"; 276 if (hasTx) { 277 tx.begin(); 278 proxyMaps_[i].remove(key); 279 tx.commit(); 280 } else { 281 proxyMaps_[i].remove(key); 282 } 283 } 284 285 return nodeList_.size(); 286 } 287 288 293 private ArrayList nodeGen(int depth, int children) 294 { 295 ArrayList strList = new ArrayList (); 296 ArrayList oldList = new ArrayList (); 297 ArrayList newList = new ArrayList (); 298 299 oldList.add("/"); 300 newList.add("/"); 301 strList.add("/"); 302 303 while (depth > 0) { 304 newList = new ArrayList (); 306 for (int i = 0; i < oldList.size(); i++) { 307 for (int j = 0; j < children; j++) { 308 String tmp = (String ) oldList.get(i); 309 tmp += Integer.toString(j); 310 if (depth != 1) tmp += "/"; 311 newList.add(tmp); 312 } 313 } 314 strList.addAll(newList); 315 oldList = newList; 316 depth--; 317 } 318 319 log("Nodes generated: " + strList.size()); 320 return strList; 321 } 322 323 public static Test suite() throws Exception 324 { 325 return new TestSuite(ReplicatedSyncMapPerfAopTest.class); 326 } 327 328 private void log(String str) 329 { 330 System.out.println(str); 332 } 333 334 } 335 | Popular Tags |