1 18 package org.objectweb.speedo.runtime.query; 19 20 import org.objectweb.speedo.SpeedoTestHelper; 21 import org.objectweb.speedo.Alea; 22 import org.objectweb.speedo.api.ExceptionHelper; 23 import org.objectweb.util.monolog.api.BasicLevel; 24 import org.objectweb.util.monolog.api.Logger; 25 26 import java.util.ArrayList ; 27 import java.util.StringTokenizer ; 28 29 33 public class TestParallelQueries extends SpeedoTestHelper { 34 35 private static TestQueries test; 36 static { 37 test = new TestQueries("TestParallelQueries"); 38 } 39 40 private final static String [] METHOD_NAMES = { 41 "test0Parameter", 42 "test10rder", 43 "test20rder", 44 "test1Parameter", 45 "testFieldParameter", 46 "test2Parameters", 47 "test3Parameters", 48 "testMapParameters", 49 "testArrayParameters", 50 "testFieldRef", 51 "testThis", 52 "testParamRef", 53 "testBsContainsParamA0B0", 54 "testBsContainsParamA0B2", 55 "testBsContainsVarB3EmptyShortPath", 56 "testBsContainsVarB0ShortPath", 57 "testBsContainsVarB1ShortPath", 58 "testBsContainsVarB3EmptyLongPath", 59 "testBsContainsVarB0LongPath", 60 "testBsContainsVarB1LongPath", 61 "testBsIsEmptyA0", 62 "testBsIsEmptyA3", 63 "testBsIsEmptyA0WithNot", 64 "testBsIsEmptyA3WithNot", 65 "testBsContainsVarB3EmptyLongPath", 66 "testBsContainsVarB3EmptyLongPath", 67 "testBsIsNotEmptyA0Global", 68 "testBsIsNotEmptyA0Local", 69 "testBsIsNotEmptyA3Global", 70 "testNotA0", 71 "testExtentAMMBfalse", 72 "testExtentBMMBfalse", 73 "testQueryBasedOnExtent", 74 "testSequenceIdNavigateToPrimitive", 75 "testSequenceIdNavigateToPrimitive2", 76 "testMatches" 77 }; 79 80 public TestParallelQueries(String name) { 81 super(name); 82 } 83 84 protected String getLoggerName() { 85 return LOG_NAME + ".rt.query.TestParallelQueries"; 86 } 87 88 private void launchTest(int queryidx, int threadId, int cpt) { 89 String msg = "Thread " + threadId + "(" + cpt + "), method: " 90 + METHOD_NAMES[queryidx]; 91 logger.log(BasicLevel.DEBUG, msg); 92 try { 93 switch(queryidx) { 94 case 0: 95 test.test0Parameter(); 96 break; 97 case 1: 98 test.test10rder(); 99 break; 100 case 2: 101 test.test20rder(); 102 break; 103 case 3: 104 test.test1Parameter(); 105 break; 106 case 4: 107 test.testFieldParameter(); 108 break; 109 case 5: 110 test.test2Parameters(); 111 break; 112 case 6: 113 test.test3Parameters(); 114 break; 115 case 7: 116 test.testMapParameters(); 117 break; 118 case 8: 119 test.testArrayParameters(); 120 break; 121 case 9: 122 test.testFieldRef(); 123 break; 124 case 10: 125 test.testThis(); 126 break; 127 case 11: 128 test.testParamRef(); 129 break; 130 case 12: 131 test.testBsContainsParamA0B0(); 132 break; 133 case 13: 134 test.testBsContainsParamA0B2(); 135 break; 136 case 14: 137 test.testBsContainsVarB3EmptyShortPath(); 138 break; 139 case 15: 140 test.testBsContainsVarB0ShortPath(); 141 break; 142 case 16: 143 test.testBsContainsVarB1ShortPath(); 144 break; 145 case 17: 146 test.testBsContainsVarB3EmptyLongPath(); 147 break; 148 case 18: 149 test.testBsContainsVarB0LongPath(); 150 break; 151 case 19: 152 test.testBsContainsVarB1LongPath(); 153 break; 154 case 20: 155 test.testBsIsEmptyA0(); 156 break; 157 case 21: 158 test.testBsIsEmptyA3(); 159 break; 160 case 22: 161 test.testBsIsEmptyA0WithNot(); 162 break; 163 case 23: 164 test.testBsIsEmptyA3WithNot(); 165 break; 166 case 24: 167 test.testBsContainsVarB3EmptyLongPath(); 168 break; 169 case 25: 170 test.testBsContainsVarB3EmptyLongPath(); 171 break; 172 case 26: 173 test.testBsIsNotEmptyA0Global(); 174 break; 175 case 27: 176 test.testBsIsNotEmptyA0Local(); 177 break; 178 case 28: 179 test.testBsIsNotEmptyA3Global(); 180 break; 181 case 29: 182 test.testNotA0(); 183 break; 184 case 30: 185 test.testExtentAMMBfalse(); 186 break; 187 case 31: 188 test.testExtentBMMBfalse(); 189 break; 190 case 32: 191 test.testQueryBasedOnExtent(); 192 break; 193 case 33: 194 test.testSequenceIdNavigateToPrimitive(); 195 break; 196 case 34: 197 test.testSequenceIdNavigateToPrimitive2(); 198 break; 199 case 35: 200 test.testMatches(); 201 break; 202 case 36: 203 test.testBsContains2Param_0_3(); 204 break; 205 } 206 } catch (Exception e) { 207 Exception ie = ExceptionHelper.getNested(e); 208 logger.log(BasicLevel.ERROR, msg, ie); 209 fail(msg + ie.getMessage()); 210 } 211 } 212 213 private static long queryExecTime = 0; 214 private static long nbquery = 0; 215 private static long errors = 0; 216 private static long nbprint = 1000; 217 private synchronized static void queryError(Logger logger, Throwable t) { 218 logger.log(BasicLevel.ERROR, "", t); 219 errors ++; 220 } 221 private synchronized static void queryFinished(long time, Logger logger) { 222 nbquery++; 223 queryExecTime += time; 224 if ((nbquery % nbprint) == 0) { 225 logger.log(BasicLevel.INFO, "."); 226 } 227 } 228 229 public void test1() { 230 final int nbThread = getIntProperty(getLoggerName() +".nbthread", 10); 231 final int nbQuery = getIntProperty(getLoggerName() +".nbquery", 100); 232 nbprint = getIntProperty(getLoggerName() +".nbprint", nbThread * nbQuery + 1); 233 ArrayList al = new ArrayList (50); 234 String v = System.getProperty(getLoggerName() +".query"); 235 if (v != null) { 236 v = v.trim(); 237 if (!v.startsWith("$")) { 238 StringTokenizer st = new StringTokenizer (v, ", ", false); 239 while(st.hasMoreTokens()) { 240 try { 241 al.add(new Integer (st.nextToken())); 242 } catch (NumberFormatException e) { 243 } 244 } 245 } 246 } 247 if (al.size() == 0) { 248 for(int i=0;i<METHOD_NAMES.length; i++) { 249 al.add(new Integer (i)); 250 } 251 } 252 final int[] queries = new int[al.size()]; 253 for(int i=(al.size()-1); i>=0; i--) { 254 queries[i] = ((Integer ) al.get(i)).intValue(); 255 } 256 logger.log(BasicLevel.INFO, "Test: nbthread=" + nbThread 257 + ", nbquery=" + nbQuery 258 + ", queryIdx=" + al); 259 Thread [] ts = new Thread [nbThread]; 260 for(int i=0; i<nbThread; i++) { 261 final int threadId = i; 262 ts[i] = new Thread ( 263 new Runnable () { 264 public void run () { 265 if (nbQuery == 0) { 266 while(true) { 267 try { 268 launchTest(queries[Alea.rand(0, queries.length-1)], threadId, -1); 269 queryFinished(0, logger); 270 } catch (Throwable e) { 271 logger.log(BasicLevel.ERROR, "Thread " + threadId + " fail!", e); 272 break; 273 } 274 } 275 } else { 276 for(int j=0; j<nbQuery; j++) { 277 long exectime = System.currentTimeMillis(); 278 try { 279 launchTest(queries[Alea.rand(0, queries.length-1)], threadId, j); 280 queryFinished(System.currentTimeMillis() - exectime, logger); 281 } catch (Throwable e) { 282 queryError(logger, e); 283 } 284 } 285 } 286 } 287 } 288 ); 289 } 290 long exectime = System.currentTimeMillis(); 291 for(int i=0; i<nbThread; i++) { 292 ts[i].start(); 293 } 294 try { 295 for(int i=0; i<nbThread; i++) { 296 ts[i].join(); 297 } 298 } catch (InterruptedException e) { 299 fail(e.getMessage()); 300 } 301 if (nbQuery > 0) { 302 exectime = System.currentTimeMillis() - exectime; 303 int nbRunQuery = (nbThread * nbQuery); 304 logger.log(BasicLevel.INFO, "Query successed: " 305 + ((nbquery * 100) / nbRunQuery) + "% (" 306 + nbquery + "/" + nbRunQuery + ", " 307 + (nbRunQuery - nbquery) + " error)"); 308 logger.log(BasicLevel.INFO, "Query average execution time: " 309 + (queryExecTime / nbquery) +"ms"); 310 logger.log(BasicLevel.INFO, "Rate: " + ((nbquery * 1000) / exectime) + " query/sec"); 311 if (errors > 0) { 312 fail(errors + " errors occured!"); 313 } 314 } 315 } 316 } 317 | Popular Tags |