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 LocalMapPerfTestCase 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 54 static 55 { 56 p_ = new Properties (); 57 p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 58 } 59 60 public LocalMapPerfTestCase(String name) 61 { 62 super(name); 63 } 64 65 public void setUp() throws Exception 66 { 67 super.setUp(); 68 69 oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 70 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 71 72 DummyTransactionManager.getInstance(); 73 initCaches(TreeCache.LOCAL); 74 tm_ = new DummyTransactionManager(); 75 76 log("LocalPerfAopTest: cacheMode=LOCAL, one cache"); 77 populateMap(); 78 nodeList_ = nodeGen(depth_, children_); 79 } 80 81 private void populateMap() 82 { 83 int depth = depth_; 84 int children = children_; 85 int total = 0; 86 while (depth > 0) { 87 for (int i = 0; i < total; i++) { 88 for (int j = 0; j < children; j++) { 89 total++; 90 } 91 } 92 depth--; 93 } 94 95 for (int i = 0; i < total; i++) { 96 String key = Integer.toString(i); 97 String value = "This is a test for performance of get and set of regular treecache and aop"; 98 map_.put(key, value); 99 } 100 } 101 102 public void tearDown() throws Exception 103 { 104 super.tearDown(); 105 106 DummyTransactionManager.destroy(); 107 destroyCaches(); 108 109 if (oldFactory_ != null) { 110 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_); 111 oldFactory_ = null; 112 } 113 114 map_.clear(); 115 } 116 117 void initCaches(int caching_mode) throws Exception 118 { 119 cachingMode_ = caching_mode; 120 cache_ = new TreeCache(); 121 PropertyConfigurator config = new PropertyConfigurator(); 122 config.configure(cache_, "META-INF/local-service.xml"); cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 124 cache_.startService(); 125 } 126 127 void destroyCaches() throws Exception 128 { 129 cache_.stopService(); 130 cache_ = null; 131 } 132 133 public void testAll() throws Exception 134 { 135 log("=== Transaction ==="); 136 DecimalFormat form = new DecimalFormat ("#.00"); 138 FieldPosition fieldPos = new FieldPosition (0); 139 StringBuffer dumbStr = new StringBuffer (); 140 boolean hasTx = false; 141 142 long time1 = System.currentTimeMillis(); 144 int nOps = _put(hasTx); 145 long time2 = System.currentTimeMillis(); 146 double d = (double) (time2 - time1) / nOps; 147 log("Time elapsed for _put is " + (time2 - time1) + " with " + nOps 148 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 149 " msec."); 150 dumbStr = new StringBuffer (); 151 152 time1 = System.currentTimeMillis(); 154 nOps = _get(hasTx); 155 time2 = System.currentTimeMillis(); 156 d = (double) (time2 - time1) / nOps; 157 log("Time elapsed for _get is " + (time2 - time1) + " with " + nOps 158 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 159 " msec."); 160 dumbStr = new StringBuffer (); 161 162 time1 = System.currentTimeMillis(); 164 nOps = _remove(hasTx); 165 time2 = System.currentTimeMillis(); 166 d = (double) (time2 - time1) / nOps; 167 log("Time elapsed for _remove is " + (time2 - time1) + " with " + nOps 168 + " operations. Average per ops is: " + form.format(d, dumbStr, fieldPos) + 169 " msec."); 170 } 171 172 protected void setLevelRW() 173 { 174 log("set lock level to RWUpgrade ..."); 175 LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 176 } 177 178 protected void setLevelSerial() 179 { 180 log("set lock level to SimpleLock ..."); 181 LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE); 182 } 183 184 private int _put(boolean hasTx) throws Exception 185 { 186 UserTransaction tx = null; 187 if (hasTx) { 188 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 189 } 190 191 String value = "This is a modified value for the unit testing"; 192 for (int i = 0; i < nodeList_.size(); i++) { 193 String key = Integer.toString(i); 194 map_.put(key, value); 195 if (hasTx) { 196 tx.begin(); 197 cache_.put((String ) nodeList_.get(i), key, map_); 198 tx.commit(); 199 } else { 200 cache_.put((String ) nodeList_.get(i), key, map_); 201 } 202 } 203 204 return nodeList_.size(); 205 } 206 207 private int _get(boolean hasTx) throws Exception 208 { 209 UserTransaction tx = null; 210 if (hasTx) { 211 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 212 } 213 214 for (int i = 0; i < nodeList_.size(); i++) { 215 String key = Integer.toString(i); 216 if (hasTx) { 217 tx.begin(); 218 Map map = (Map ) cache_.get((String ) nodeList_.get(i), key); 219 String str = (String ) map.get(key); 220 tx.commit(); 222 } else { 223 Map map = (Map ) cache_.get((String ) nodeList_.get(i), key); 224 String str = (String ) map.get(key); 225 } 227 } 228 229 return nodeList_.size(); 230 } 231 232 private int _remove(boolean hasTx) throws Exception 233 { 234 UserTransaction tx = null; 235 if (hasTx) { 236 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 237 } 238 239 for (int i = 0; i < nodeList_.size(); i++) { 240 String key = Integer.toString(i); 241 if (hasTx) { 242 tx.begin(); 243 Map map = (Map ) cache_.get((String ) nodeList_.get(i), key); 244 map.remove(key); 245 tx.commit(); 246 } else { 247 Map map = (Map ) cache_.get((String ) nodeList_.get(i), key); 248 map.remove(key); 249 } 250 } 251 252 return nodeList_.size(); 253 } 254 255 256 261 private ArrayList nodeGen(int depth, int children) 262 { 263 ArrayList strList = new ArrayList (); 264 ArrayList oldList = new ArrayList (); 265 ArrayList newList = new ArrayList (); 266 267 oldList.add("/"); 268 newList.add("/"); 269 strList.add("/"); 270 271 while (depth > 0) { 272 newList = new ArrayList (); 274 for (int i = 0; i < oldList.size(); i++) { 275 for (int j = 0; j < children; j++) { 276 String tmp = (String ) oldList.get(i); 277 tmp += Integer.toString(j); 278 if (depth != 1) tmp += "/"; 279 newList.add(tmp); 280 } 281 } 282 strList.addAll(newList); 283 oldList = newList; 284 depth--; 285 } 286 287 log("Nodes generated: " + strList.size()); 288 return strList; 289 } 290 291 292 public static Test suite() throws Exception 293 { 294 return new TestSuite(LocalMapPerfTestCase.class); 295 } 296 297 private void log(String str) 298 { 299 System.out.println(str); 301 } 302 303 } 304 | Popular Tags |