001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.lucene.benchmark.byTask;
017:
018: import junit.framework.TestCase;
019: import org.apache.lucene.benchmark.byTask.tasks.PerfTask;
020: import org.apache.lucene.benchmark.byTask.tasks.TaskSequence;
021: import org.apache.lucene.benchmark.byTask.utils.Algorithm;
022:
023: import java.io.File;
024: import java.io.StringReader;
025: import java.lang.reflect.Modifier;
026: import java.util.ArrayList;
027: import java.util.Iterator;
028:
029: /**
030: * Test very simply that perf tasks are parses as expected.
031: */
032: public class TestPerfTasksParse extends TestCase {
033:
034: private static final boolean DEBUG = false;
035: static final String NEW_LINE = System.getProperty("line.separator");
036: static final String INDENT = " ";
037:
038: // properties in effect in all tests here
039: static final String propPart = INDENT + "directory=RAMDirectory"
040: + NEW_LINE + INDENT + "print.props=false" + NEW_LINE;
041:
042: /*
043: * All known tasks.
044: * As new tasks are added, add them here.
045: * It would be nice to do that automatically, unfortunately
046: * Java does not provide a "get all classes in package" or
047: * "get all sub-classes" functionality.
048: */
049: static String singleTaskAlgs[];
050:
051: /* (non-Javadoc)
052: * @see junit.framework.TestCase#setUp()
053: */
054: protected void setUp() throws Exception {
055: super .setUp();
056: if (singleTaskAlgs == null) {
057: singleTaskAlgs = findTasks();
058: }
059: }
060:
061: // one time initialization
062: static String[] findTasks() throws Exception {
063: ArrayList tsks = new ArrayList();
064: // init with tasks we know about
065: tsks.add(" AddDoc ");
066: tsks.add(" AddDoc(1000.0) ");
067: tsks.add(" ClearStats ");
068: tsks.add(" CloseIndex ");
069: tsks.add(" CloseReader ");
070: tsks.add(" CreateIndex ");
071: tsks.add(" DeleteDoc ");
072: tsks.add(" DeleteDoc(500.0) ");
073: tsks.add(" NewRound ");
074: tsks.add(" OpenIndex ");
075: tsks.add(" OpenReader ");
076: tsks.add(" Optimize ");
077: tsks.add(" RepAll ");
078: tsks.add(" RepSelectByPref prefix ");
079: tsks.add(" RepSumByNameRound ");
080: tsks.add(" RepSumByName ");
081: tsks.add(" RepSumByPrefRound prefix ");
082: tsks.add(" RepSumByPref prefix ");
083: tsks.add(" ResetInputs ");
084: tsks.add(" ResetSystemErase ");
085: tsks.add(" ResetSystemSoft ");
086: tsks.add(" Search ");
087: tsks.add(" SearchTravRet ");
088: tsks.add(" SearchTravRet(100.0) ");
089: tsks.add(" SearchTrav ");
090: tsks.add(" SearchTrav(50.0) ");
091: tsks.add(" SetProp ");
092: tsks.add(" SetProp(name,value) ");
093: tsks.add(" Warm ");
094: tsks.add("SearchTravRetLoadFieldSelector");
095: tsks.add("SearchTravRetLoadFieldSelector(body,title)");
096:
097: // if tasks.dir property is defined, look for additional tasks.
098: // this somewhat covers tasks that would be added in the future, in case
099: // the list above is not updated to cover them.
100: // some tasks would be tested more than once this way, but that's ok.
101: String tasksDir = System.getProperty("tasks.dir");
102: if (tasksDir != null) {
103: String pkgPrefix = PerfTask.class.getPackage().getName()
104: + ".";
105: String taskNames[] = new File(tasksDir).list();
106: for (int i = 0; i < taskNames.length; i++) {
107: String name = taskNames[i].trim();
108: if (!name.endsWith("Task.class"))
109: continue; // Task class file only
110: name = name.substring(0, name.length() - 6);
111: Class cls = Class.forName(pkgPrefix + name);
112: if (Modifier.isAbstract(cls.getModifiers())
113: || Modifier.isInterface(cls.getModifiers()))
114: continue; // skip sbstract classes
115: if (!PerfTask.class.isAssignableFrom(cls))
116: continue; // not a task
117: name = name.substring(0, name.length() - 4);
118: if (name.startsWith("Rep") && name.indexOf("Pref") >= 0)
119: name += " prefix";
120: tsks.add(" " + name + " ");
121: }
122: }
123: return (String[]) tsks.toArray(new String[0]);
124: }
125:
126: /**
127: * @param name test name
128: */
129: public TestPerfTasksParse(String name) {
130: super (name);
131: }
132:
133: /**
134: * Test the parsing of very simple tasks, for all tasks
135: */
136: public void testAllTasksSimpleParse() {
137: doTestAllTasksSimpleParse(false, false);
138: }
139:
140: /**
141: * Test the parsing of simple sequential sequences, for all tasks
142: */
143: public void testAllTasksSimpleParseSequntial() {
144: doTestAllTasksSimpleParse(true, false);
145: }
146:
147: /**
148: * Test the parsing of simple parallel sequences, for all tasks
149: */
150: public void testAllTasksSimpleParseParallel() {
151: doTestAllTasksSimpleParse(true, true);
152: }
153:
154: // utility for simple parsing testing of all tasks.
155: private void doTestAllTasksSimpleParse(boolean parOrSeq, boolean par) {
156: for (int i = 0; i < singleTaskAlgs.length; i++) {
157: String testedTask = singleTaskAlgs[i];
158: if (parOrSeq) {
159: if (par) {
160: testedTask = "[ " + testedTask + " ] : 2";
161: } else {
162: testedTask = "{ " + testedTask + " } : 3";
163: }
164: }
165: try {
166: String algText = propPart + INDENT + testedTask;
167: logTstParsing(algText);
168: Benchmark benchmark = new Benchmark(new StringReader(
169: algText));
170: Algorithm alg = benchmark.getAlgorithm();
171: ArrayList algTasks = alg.extractTasks();
172: // must find a task with this name in the algorithm
173: boolean foundName = false;
174: boolean foundPar = false;
175: String theTask = singleTaskAlgs[i]
176: .replaceAll(" +", " ").trim();
177: for (Iterator iter = algTasks.iterator(); iter
178: .hasNext();) {
179: PerfTask task = (PerfTask) iter.next();
180: foundName |= (task.toString().indexOf(theTask) >= 0);
181: foundPar |= (task instanceof TaskSequence && ((TaskSequence) task)
182: .isParallel());
183: }
184: assertTrue("Task " + testedTask + " was not found in "
185: + alg.toString(), foundName);
186: if (parOrSeq) {
187: if (par) {
188: assertTrue("Task " + testedTask
189: + " was supposed to be parallel in "
190: + alg.toString(), foundPar);
191: } else {
192: assertFalse(
193: "Task "
194: + testedTask
195: + " was not supposed to be parallel in "
196: + alg.toString(), foundPar);
197: }
198: }
199: } catch (Exception e) {
200: System.out.flush();
201: e.printStackTrace();
202: fail(e.getMessage());
203: }
204: }
205: }
206:
207: /**
208: * Test the repetiotion parsing for parallel tasks
209: */
210: public void testParseParallelTaskSequenceRepetition()
211: throws Exception {
212: String taskStr = "AddDoc";
213: String parsedTasks = "[ " + taskStr + " ] : 1000";
214: Benchmark benchmark = new Benchmark(new StringReader(propPart
215: + parsedTasks));
216: Algorithm alg = benchmark.getAlgorithm();
217: ArrayList algTasks = alg.extractTasks();
218: boolean foundAdd = false;
219: for (Iterator iter = algTasks.iterator(); iter.hasNext();) {
220: PerfTask task = (PerfTask) iter.next();
221: if (task.toString().indexOf(taskStr) >= 0) {
222: foundAdd = true;
223: }
224: if (task instanceof TaskSequence) {
225: assertEquals("repetions should be 1000 for "
226: + parsedTasks, 1000, ((TaskSequence) task)
227: .getRepetitions());
228: assertTrue("sequence for " + parsedTasks
229: + " should be parallel!", ((TaskSequence) task)
230: .isParallel());
231: }
232: assertTrue("Task " + taskStr + " was not found in "
233: + alg.toString(), foundAdd);
234: }
235: }
236:
237: /**
238: * Test the repetiotion parsing for sequential tasks
239: */
240: public void testParseTaskSequenceRepetition() throws Exception {
241: String taskStr = "AddDoc";
242: String parsedTasks = "{ " + taskStr + " } : 1000";
243: Benchmark benchmark = new Benchmark(new StringReader(propPart
244: + parsedTasks));
245: Algorithm alg = benchmark.getAlgorithm();
246: ArrayList algTasks = alg.extractTasks();
247: boolean foundAdd = false;
248: for (Iterator iter = algTasks.iterator(); iter.hasNext();) {
249: PerfTask task = (PerfTask) iter.next();
250: if (task.toString().indexOf(taskStr) >= 0) {
251: foundAdd = true;
252: }
253: if (task instanceof TaskSequence) {
254: assertEquals("repetions should be 1000 for "
255: + parsedTasks, 1000, ((TaskSequence) task)
256: .getRepetitions());
257: assertFalse("sequence for " + parsedTasks
258: + " should be sequential!",
259: ((TaskSequence) task).isParallel());
260: }
261: assertTrue("Task " + taskStr + " was not found in "
262: + alg.toString(), foundAdd);
263: }
264: }
265:
266: private void logTstParsing(String txt) {
267: if (!DEBUG)
268: return;
269: System.out.println("Test parsing of");
270: System.out.println(txt);
271: }
272:
273: }
|