FileDocCategorySizeDatePackage
TaskSequence.javaAPI DocApache Lucene 2.2.08636Sat Jun 16 22:20:58 BST 2007org.apache.lucene.benchmark.byTask.tasks

TaskSequence

public class TaskSequence extends PerfTask
Sequence of parallel or sequential tasks.

Fields Summary
public static int
REPEAT_EXHAUST
private ArrayList
tasks
private int
repetitions
private boolean
parallel
private TaskSequence
parent
private boolean
letChildReport
private int
rate
private boolean
perMin
private String
seqName
Constructors Summary
public TaskSequence(org.apache.lucene.benchmark.byTask.PerfRunData runData, String name, TaskSequence parent, boolean parallel)

 
  
            
    super(runData);
    name = (name!=null ? name : (parallel ? "Par" : "Seq"));
    setName(name);
    setSequenceName();
    this.parent = parent;
    this.parallel = parallel;
    tasks = new ArrayList();
  
Methods Summary
public voidaddTask(PerfTask task)

    tasks.add(task);
    task.setDepth(getDepth()+1);
  
protected java.lang.Objectclone()

    TaskSequence res = (TaskSequence) super.clone();
    res.tasks = new ArrayList();
    for (int i = 0; i < tasks.size(); i++) {
      res.tasks.add(((PerfTask)tasks.get(i)).clone());
    }
    return res;
  
public intdoLogic()

    return ( parallel ? doParallelTasks() : doSerialTasks());
  
private intdoParallelTasks()

    final int count [] = {0};
    Thread t[] = new Thread [repetitions * tasks.size()];
    // prepare threads
    int indx = 0;
    for (int k=0; k<repetitions; k++) {
      for (int i = 0; i < tasks.size(); i++) {
        final PerfTask task = (PerfTask) ((PerfTask) tasks.get(i)).clone();
        t[indx++] = new Thread() {
          public void run() {
            int n;
            try {
              n = task.runAndMaybeStats(letChildReport);
            } catch (Exception e) {
              throw new RuntimeException(e);
            }
            synchronized (count) {
              count[0] += n;
            }
          }
        };
      }
    }
    // run threads
    startThreads(t);
    // wait for all threads to complete
    for (int i = 0; i < t.length; i++) {
      t[i].join();
    }
    // return total count
    return count[0];
  
private intdoSerialTasks()

    if (rate > 0) {
      return doSerialTasksWithRate();
    }
    
    int count = 0;
    boolean exhausted = false;
    for (int k=0; (repetitions==REPEAT_EXHAUST && !exhausted) || k<repetitions; k++) {
      for (Iterator it = tasks.iterator(); it.hasNext();) {
        PerfTask task = (PerfTask) it.next();
        try {
          count += task.runAndMaybeStats(letChildReport);
        } catch (NoMoreDataException e) {
          exhausted = true;
        }
      }
    }
    return count;
  
private intdoSerialTasksWithRate()

    long delayStep = (perMin ? 60000 : 1000) /rate;
    long nextStartTime = System.currentTimeMillis();
    int count = 0;
    boolean exhausted = false;
    for (int k=0; (repetitions==REPEAT_EXHAUST && !exhausted) || k<repetitions; k++) {
      for (Iterator it = tasks.iterator(); it.hasNext();) {
        PerfTask task = (PerfTask) it.next();
        long waitMore = nextStartTime - System.currentTimeMillis();
        if (waitMore > 0) {
          //System.out.println("wait: "+waitMore+" for rate: "+ratePerMin+" (delayStep="+delayStep+")");
          Thread.sleep(waitMore);
        }
        nextStartTime += delayStep; // this aims at avarage rate. 
        try {
          count += task.runAndMaybeStats(letChildReport);
        } catch (NoMoreDataException e) {
          exhausted = true;
        }
      }
    }
    return count;
  
public java.lang.StringgetName()

    return seqName; // overide to include more info 
  
public org.apache.lucene.benchmark.byTask.tasks.TaskSequencegetParent()

return
Returns the parent.

    return parent;
  
public intgetRate()
Returns the rate per minute: how many operations should be performed in a minute. If 0 this has no effect.

return
the rate per min: how many operations should be performed in a minute.

    return (perMin ? rate : 60*rate);
  
public intgetRepetitions()

return
Returns the repetitions.

    return repetitions;
  
public java.util.ArrayListgetTasks()

return
Returns the tasks.

    return tasks;
  
public booleanisParallel()

return
Returns the parallel.

    return parallel;
  
public voidsetNoChildReport()
Execute child tasks in a way that they do not report their time separately.

    letChildReport  = false;
    for (Iterator it = tasks.iterator(); it.hasNext();) {
      PerfTask task = (PerfTask) it.next();
      if (task instanceof TaskSequence) {
        ((TaskSequence)task).setNoChildReport();
  }
    }
  
public voidsetRate(int rate, boolean perMin)

param
rate The rate to set.

    this.rate = rate;
    this.perMin = perMin;
    setSequenceName();
  
public voidsetRepetitions(int repetitions)

param
repetitions The repetitions to set.
throws
Exception

    this.repetitions = repetitions;
    if (repetitions==REPEAT_EXHAUST) {
      if (isParallel()) {
        throw new Exception("REPEAT_EXHAUST is not allowed for parallel tasks");
      }
      if (getRunData().getConfig().get("doc.maker.forever",true)) {
        throw new Exception("REPEAT_EXHAUST requires setting doc.maker.forever=false");
      }
    }
    setSequenceName();
  
private voidsetSequenceName()

    seqName = super.getName();
    if (repetitions==REPEAT_EXHAUST) {
      seqName += "_Exhaust";
    } else if (repetitions>1) {
      seqName += "_"+repetitions;
    }
    if (rate>0) {
      seqName += "_" + rate + (perMin?"/min":"/sec"); 
    }
    if (parallel && seqName.toLowerCase().indexOf("par")<0) {
      seqName += "_Par";
    }
  
private voidstartThreads(java.lang.Thread[] t)

    if (rate > 0) {
      startlThreadsWithRate(t);
      return;
    }
    for (int i = 0; i < t.length; i++) {
      t[i].start();
    }
  
private voidstartlThreadsWithRate(java.lang.Thread[] t)

    long delayStep = (perMin ? 60000 : 1000) /rate;
    long nextStartTime = System.currentTimeMillis();
    for (int i = 0; i < t.length; i++) {
      long waitMore = nextStartTime - System.currentTimeMillis();
      if (waitMore > 0) {
        //System.out.println("thread wait: "+waitMore+" for rate: "+ratePerMin+" (delayStep="+delayStep+")");
        Thread.sleep(waitMore);
      }
      nextStartTime += delayStep; // this aims at avarage rate of starting threads. 
      t[i].start();
    }
  
public java.lang.StringtoString()

    String padd = getPadding();
    StringBuffer sb = new StringBuffer(super.toString());
    sb.append(parallel ? " [" : " {");
    sb.append(NEW_LINE);
    for (Iterator it = tasks.iterator(); it.hasNext();) {
      PerfTask task = (PerfTask) it.next();
      sb.append(task.toString());
      sb.append(NEW_LINE);
    }
    sb.append(padd);
    sb.append(!letChildReport ? ">" : (parallel ? "]" : "}"));
    if (repetitions>1) {
      sb.append(" * " + repetitions);
    }
    if (repetitions==REPEAT_EXHAUST) {
      sb.append(" * EXHAUST");
    }
    if (rate>0) {
      sb.append(",  rate: " + rate+"/"+(perMin?"min":"sec"));
    }
    return sb.toString();