001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.runtime.query;
018:
019: import org.objectweb.speedo.SpeedoTestHelper;
020: import org.objectweb.speedo.Alea;
021: import org.objectweb.speedo.api.ExceptionHelper;
022: import org.objectweb.util.monolog.api.BasicLevel;
023: import org.objectweb.util.monolog.api.Logger;
024:
025: import java.util.ArrayList;
026: import java.util.StringTokenizer;
027:
028: /**
029: *
030: * @author S.Chassande-Barrioz
031: */
032: public class TestParallelQueries extends SpeedoTestHelper {
033:
034: private static TestQueries test;
035: static {
036: test = new TestQueries("TestParallelQueries");
037: }
038:
039: private final static String[] METHOD_NAMES = { "test0Parameter",
040: "test10rder", "test20rder", "test1Parameter",
041: "testFieldParameter", "test2Parameters", "test3Parameters",
042: "testMapParameters", "testArrayParameters", "testFieldRef",
043: "testThis", "testParamRef", "testBsContainsParamA0B0",
044: "testBsContainsParamA0B2",
045: "testBsContainsVarB3EmptyShortPath",
046: "testBsContainsVarB0ShortPath",
047: "testBsContainsVarB1ShortPath",
048: "testBsContainsVarB3EmptyLongPath",
049: "testBsContainsVarB0LongPath",
050: "testBsContainsVarB1LongPath", "testBsIsEmptyA0",
051: "testBsIsEmptyA3", "testBsIsEmptyA0WithNot",
052: "testBsIsEmptyA3WithNot",
053: "testBsContainsVarB3EmptyLongPath",
054: "testBsContainsVarB3EmptyLongPath",
055: "testBsIsNotEmptyA0Global", "testBsIsNotEmptyA0Local",
056: "testBsIsNotEmptyA3Global", "testNotA0",
057: "testExtentAMMBfalse", "testExtentBMMBfalse",
058: "testQueryBasedOnExtent",
059: "testSequenceIdNavigateToPrimitive",
060: "testSequenceIdNavigateToPrimitive2", "testMatches"
061: //"testBsContains2Param_0_3"
062: };
063:
064: public TestParallelQueries(String name) {
065: super (name);
066: }
067:
068: protected String getLoggerName() {
069: return LOG_NAME + ".rt.query.TestParallelQueries";
070: }
071:
072: private void launchTest(int queryidx, int threadId, int cpt) {
073: String msg = "Thread " + threadId + "(" + cpt + "), method: "
074: + METHOD_NAMES[queryidx];
075: logger.log(BasicLevel.DEBUG, msg);
076: try {
077: switch (queryidx) {
078: case 0:
079: test.test0Parameter();
080: break;
081: case 1:
082: test.test10rder();
083: break;
084: case 2:
085: test.test20rder();
086: break;
087: case 3:
088: test.test1Parameter();
089: break;
090: case 4:
091: test.testFieldParameter();
092: break;
093: case 5:
094: test.test2Parameters();
095: break;
096: case 6:
097: test.test3Parameters();
098: break;
099: case 7:
100: test.testMapParameters();
101: break;
102: case 8:
103: test.testArrayParameters();
104: break;
105: case 9:
106: test.testFieldRef();
107: break;
108: case 10:
109: test.testThis();
110: break;
111: case 11:
112: test.testParamRef();
113: break;
114: case 12:
115: test.testBsContainsParamA0B0();
116: break;
117: case 13:
118: test.testBsContainsParamA0B2();
119: break;
120: case 14:
121: test.testBsContainsVarB3EmptyShortPath();
122: break;
123: case 15:
124: test.testBsContainsVarB0ShortPath();
125: break;
126: case 16:
127: test.testBsContainsVarB1ShortPath();
128: break;
129: case 17:
130: test.testBsContainsVarB3EmptyLongPath();
131: break;
132: case 18:
133: test.testBsContainsVarB0LongPath();
134: break;
135: case 19:
136: test.testBsContainsVarB1LongPath();
137: break;
138: case 20:
139: test.testBsIsEmptyA0();
140: break;
141: case 21:
142: test.testBsIsEmptyA3();
143: break;
144: case 22:
145: test.testBsIsEmptyA0WithNot();
146: break;
147: case 23:
148: test.testBsIsEmptyA3WithNot();
149: break;
150: case 24:
151: test.testBsContainsVarB3EmptyLongPath();
152: break;
153: case 25:
154: test.testBsContainsVarB3EmptyLongPath();
155: break;
156: case 26:
157: test.testBsIsNotEmptyA0Global();
158: break;
159: case 27:
160: test.testBsIsNotEmptyA0Local();
161: break;
162: case 28:
163: test.testBsIsNotEmptyA3Global();
164: break;
165: case 29:
166: test.testNotA0();
167: break;
168: case 30:
169: test.testExtentAMMBfalse();
170: break;
171: case 31:
172: test.testExtentBMMBfalse();
173: break;
174: case 32:
175: test.testQueryBasedOnExtent();
176: break;
177: case 33:
178: test.testSequenceIdNavigateToPrimitive();
179: break;
180: case 34:
181: test.testSequenceIdNavigateToPrimitive2();
182: break;
183: case 35:
184: test.testMatches();
185: break;
186: case 36:
187: test.testBsContains2Param_0_3();
188: break;
189: }
190: } catch (Exception e) {
191: Exception ie = ExceptionHelper.getNested(e);
192: logger.log(BasicLevel.ERROR, msg, ie);
193: fail(msg + ie.getMessage());
194: }
195: }
196:
197: private static long queryExecTime = 0;
198: private static long nbquery = 0;
199: private static long errors = 0;
200: private static long nbprint = 1000;
201:
202: private synchronized static void queryError(Logger logger,
203: Throwable t) {
204: logger.log(BasicLevel.ERROR, "", t);
205: errors++;
206: }
207:
208: private synchronized static void queryFinished(long time,
209: Logger logger) {
210: nbquery++;
211: queryExecTime += time;
212: if ((nbquery % nbprint) == 0) {
213: logger.log(BasicLevel.INFO, ".");
214: }
215: }
216:
217: public void test1() {
218: final int nbThread = getIntProperty(getLoggerName()
219: + ".nbthread", 15);
220: logger.log(BasicLevel.DEBUG, getLoggerName() + ".nbthread="
221: + nbThread);
222: final int nbQuery = getIntProperty(
223: getLoggerName() + ".nbquery", 500);
224: logger.log(BasicLevel.DEBUG, getLoggerName() + ".nbquery="
225: + nbQuery);
226: nbprint = getIntProperty(getLoggerName() + ".nbprint", nbQuery);
227: ArrayList al = new ArrayList(50);
228: String v = System.getProperty(getLoggerName() + ".query");
229: if (v != null) {
230: v = v.trim();
231: if (!v.startsWith("$")) {
232: StringTokenizer st = new StringTokenizer(v, ", ", false);
233: while (st.hasMoreTokens()) {
234: try {
235: al.add(new Integer(st.nextToken()));
236: } catch (NumberFormatException e) {
237: }
238: }
239: }
240: }
241: if (al.size() == 0) {
242: for (int i = 0; i < METHOD_NAMES.length; i++) {
243: al.add(new Integer(i));
244: }
245: }
246: final int[] queries = new int[al.size()];
247: for (int i = (al.size() - 1); i >= 0; i--) {
248: queries[i] = ((Integer) al.get(i)).intValue();
249: }
250: logger.log(BasicLevel.INFO, "Test: nbthread=" + nbThread
251: + ", nbquery=" + nbQuery + ", queryIdx=" + al);
252: Thread[] ts = new Thread[nbThread];
253: for (int i = 0; i < nbThread; i++) {
254: final int threadId = i;
255: ts[i] = new Thread(new Runnable() {
256: public void run() {
257: if (nbQuery == 0) {
258: while (true) {
259: try {
260: launchTest(queries[Alea.rand(0,
261: queries.length - 1)], threadId,
262: -1);
263: queryFinished(0, logger);
264: } catch (Throwable e) {
265: logger.log(BasicLevel.ERROR, "Thread "
266: + threadId + " fail!", e);
267: break;
268: }
269: }
270: } else {
271: for (int j = 0; j < nbQuery; j++) {
272: long exectime = System.currentTimeMillis();
273: try {
274: launchTest(queries[Alea.rand(0,
275: queries.length - 1)], threadId,
276: j);
277: queryFinished(System
278: .currentTimeMillis()
279: - exectime, logger);
280: } catch (Throwable e) {
281: queryError(logger, e);
282: }
283: }
284: }
285: }
286: });
287: }
288: long exectime = System.currentTimeMillis();
289: for (int i = 0; i < nbThread; i++) {
290: ts[i].start();
291: }
292: try {
293: for (int i = 0; i < nbThread; i++) {
294: ts[i].join();
295: }
296: } catch (InterruptedException e) {
297: fail(e.getMessage());
298: }
299: if (nbQuery > 0) {
300: exectime = System.currentTimeMillis() - exectime;
301: int nbRunQuery = (nbThread * nbQuery);
302: logger.log(BasicLevel.INFO, "Query successed: "
303: + ((nbquery * 100) / nbRunQuery) + "% (" + nbquery
304: + "/" + nbRunQuery + ", " + (nbRunQuery - nbquery)
305: + " error)");
306: logger.log(BasicLevel.INFO,
307: "Query average execution time: "
308: + (queryExecTime / nbquery) + "ms");
309: logger.log(BasicLevel.INFO, "Rate: "
310: + ((nbquery * 1000) / exectime) + " query/sec");
311: if (errors > 0) {
312: fail(errors + " errors occured!");
313: }
314: }
315: }
316: }
|