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