FileDocCategorySizeDatePackage
TestPerfTasksParse.javaAPI DocApache Lucene 2.2.09472Sat Jun 16 22:20:58 BST 2007org.apache.lucene.benchmark.byTask

TestPerfTasksParse

public class TestPerfTasksParse extends TestCase
Test very simply that perf tasks are parses as expected.

Fields Summary
private static final boolean
DEBUG
static final String
NEW_LINE
static final String
INDENT
static final String
propPart
static String[]
singleTaskAlgs
Constructors Summary
public TestPerfTasksParse(String name)

param
name test name

    super(name);
  
Methods Summary
private voiddoTestAllTasksSimpleParse(boolean parOrSeq, boolean par)

    for (int i = 0; i < singleTaskAlgs.length; i++) {
      String testedTask = singleTaskAlgs[i];
      if (parOrSeq) {
        if (par) {
          testedTask = "[ " + testedTask + " ] : 2";
        } else {
          testedTask = "{ " + testedTask + " } : 3";
        }
      }
      try {
        String algText = propPart+INDENT+testedTask;
        logTstParsing(algText);
        Benchmark benchmark = new Benchmark(new StringReader(algText));
        Algorithm alg = benchmark.getAlgorithm();
        ArrayList algTasks = alg.extractTasks();
        // must find a task with this name in the algorithm
        boolean foundName = false;
        boolean foundPar = false;
        String theTask = singleTaskAlgs[i].replaceAll(" +"," ").trim();
        for (Iterator iter = algTasks.iterator(); iter.hasNext();) {
          PerfTask task = (PerfTask) iter.next();
          foundName |= (task.toString().indexOf(theTask)>=0);
          foundPar |= (task instanceof TaskSequence && ((TaskSequence)task).isParallel());
        }
        assertTrue("Task "+testedTask+" was not found in "+alg.toString(),foundName);
        if (parOrSeq) {
          if (par) {
            assertTrue("Task "+testedTask+" was supposed to be parallel in "+alg.toString(),foundPar);
          } else {
            assertFalse("Task "+testedTask+" was not supposed to be parallel in "+alg.toString(),foundPar);
          }
        }
      } catch (Exception e) {
        System.out.flush();
        e.printStackTrace();
        fail(e.getMessage());
      }
    }
  
static java.lang.String[]findTasks()

    ArrayList tsks = new ArrayList();
    // init with tasks we know about
    tsks.add(  " AddDoc                   "  );
    tsks.add(  " AddDoc(1000.0)           "  );
    tsks.add(  " ClearStats               "  );
    tsks.add(  " CloseIndex               "  );
    tsks.add(  " CloseReader              "  );
    tsks.add(  " CreateIndex              "  );
    tsks.add(  " DeleteDoc                "  );
    tsks.add(  " DeleteDoc(500.0)         "  );
    tsks.add(  " NewRound                 "  );
    tsks.add(  " OpenIndex                "  );
    tsks.add(  " OpenReader               "  );
    tsks.add(  " Optimize                 "  );
    tsks.add(  " RepAll                   "  );
    tsks.add(  " RepSelectByPref prefix   "  );
    tsks.add(  " RepSumByNameRound        "  );
    tsks.add(  " RepSumByName             "  );
    tsks.add(  " RepSumByPrefRound prefix "  );
    tsks.add(  " RepSumByPref   prefix    "  );
    tsks.add(  " ResetInputs              "  );
    tsks.add(  " ResetSystemErase         "  );
    tsks.add(  " ResetSystemSoft          "  );
    tsks.add(  " Search                   "  );
    tsks.add(  " SearchTravRet            "  );
    tsks.add(  " SearchTravRet(100.0)     "  );
    tsks.add(  " SearchTrav               "  );
    tsks.add(  " SearchTrav(50.0)         "  );
    tsks.add(  " SetProp                  "  );
    tsks.add(  " SetProp(name,value)      "  );
    tsks.add(  " Warm                     "  );
    tsks.add(  "SearchTravRetLoadFieldSelector");
    tsks.add("SearchTravRetLoadFieldSelector(body,title)");
    
    // if tasks.dir property is defined, look for additional tasks.
    // this somewhat covers tasks that would be added in the future, in case
    // the list above is not updated to cover them.
    // some tasks would be tested more than once this way, but that's ok.
    String tasksDir = System.getProperty("tasks.dir");
    if (tasksDir !=null) {
      String pkgPrefix = PerfTask.class.getPackage().getName()+".";
      String taskNames[] = new File(tasksDir).list();
      for (int i = 0; i < taskNames.length; i++) {
        String name = taskNames[i].trim();
        if (!name.endsWith("Task.class"))
          continue; // Task class file only
        name = name.substring(0,name.length()-6);
        Class cls = Class.forName(pkgPrefix+name);
        if (Modifier.isAbstract(cls.getModifiers()) || Modifier.isInterface(cls.getModifiers())) 
          continue; // skip sbstract classes
        if (!PerfTask.class.isAssignableFrom(cls))
          continue; // not a task
        name = name.substring(0,name.length()-4);
        if (name.startsWith("Rep") && name.indexOf("Pref")>=0)
          name += " prefix";
        tsks.add(" "+name+" ");
      }
    }
    return (String[]) tsks.toArray(new String[0]);
  
private voidlogTstParsing(java.lang.String txt)

    if (!DEBUG) 
      return;
    System.out.println("Test parsing of");
    System.out.println(txt);
  
protected voidsetUp()

  
  /* (non-Javadoc)
   * @see junit.framework.TestCase#setUp()
   */
       
    super.setUp();
    if (singleTaskAlgs==null) {
      singleTaskAlgs = findTasks();
    }
  
public voidtestAllTasksSimpleParse()
Test the parsing of very simple tasks, for all tasks

    doTestAllTasksSimpleParse(false,false);
  
public voidtestAllTasksSimpleParseParallel()
Test the parsing of simple parallel sequences, for all tasks

    doTestAllTasksSimpleParse(true,true);
  
public voidtestAllTasksSimpleParseSequntial()
Test the parsing of simple sequential sequences, for all tasks

    doTestAllTasksSimpleParse(true,false);
  
public voidtestParseParallelTaskSequenceRepetition()
Test the repetiotion parsing for parallel tasks

    String taskStr = "AddDoc";
    String parsedTasks = "[ "+taskStr+" ] : 1000";
    Benchmark benchmark = new Benchmark(new StringReader(propPart+parsedTasks));
    Algorithm alg = benchmark.getAlgorithm();
    ArrayList algTasks = alg.extractTasks();
    boolean foundAdd = false;
    for (Iterator iter = algTasks.iterator(); iter.hasNext();) {
       PerfTask task = (PerfTask) iter.next();
       if (task.toString().indexOf(taskStr)>=0) {
          foundAdd = true;
       }
       if (task instanceof TaskSequence) {
         assertEquals("repetions should be 1000 for "+parsedTasks, 1000, ((TaskSequence) task).getRepetitions());
         assertTrue("sequence for "+parsedTasks+" should be parallel!", ((TaskSequence) task).isParallel());
       }
       assertTrue("Task "+taskStr+" was not found in "+alg.toString(),foundAdd);
    }
  
public voidtestParseTaskSequenceRepetition()
Test the repetiotion parsing for sequential tasks

    String taskStr = "AddDoc";
    String parsedTasks = "{ "+taskStr+" } : 1000";
    Benchmark benchmark = new Benchmark(new StringReader(propPart+parsedTasks));
    Algorithm alg = benchmark.getAlgorithm();
    ArrayList algTasks = alg.extractTasks();
    boolean foundAdd = false;
    for (Iterator iter = algTasks.iterator(); iter.hasNext();) {
       PerfTask task = (PerfTask) iter.next();
       if (task.toString().indexOf(taskStr)>=0) {
          foundAdd = true;
       }
       if (task instanceof TaskSequence) {
         assertEquals("repetions should be 1000 for "+parsedTasks, 1000, ((TaskSequence) task).getRepetitions());
         assertFalse("sequence for "+parsedTasks+" should be sequential!", ((TaskSequence) task).isParallel());
       }
       assertTrue("Task "+taskStr+" was not found in "+alg.toString(),foundAdd);
    }