Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » benchmark » byTask » utils » [javadoc | source]
    1   package org.apache.lucene.benchmark.byTask.utils;
    2   
    3   /**
    4    * Licensed to the Apache Software Foundation (ASF) under one or more
    5    * contributor license agreements.  See the NOTICE file distributed with
    6    * this work for additional information regarding copyright ownership.
    7    * The ASF licenses this file to You under the Apache License, Version 2.0
    8    * (the "License"); you may not use this file except in compliance with
    9    * the License.  You may obtain a copy of the License at
   10    *
   11    *     http://www.apache.org/licenses/LICENSE-2.0
   12    *
   13    * Unless required by applicable law or agreed to in writing, software
   14    * distributed under the License is distributed on an "AS IS" BASIS,
   15    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   16    * See the License for the specific language governing permissions and
   17    * limitations under the License.
   18    */
   19   
   20   import java.io.StreamTokenizer;
   21   import java.io.StringReader;
   22   import java.lang.reflect.Constructor;
   23   import java.util.ArrayList;
   24   
   25   import org.apache.lucene.benchmark.byTask.PerfRunData;
   26   import org.apache.lucene.benchmark.byTask.tasks.PerfTask;
   27   import org.apache.lucene.benchmark.byTask.tasks.RepSumByPrefTask;
   28   import org.apache.lucene.benchmark.byTask.tasks.TaskSequence;
   29   
   30   /**
   31    * Test algorithm, as read from file
   32    */
   33   public class Algorithm {
   34     
   35     private TaskSequence sequence;
   36     
   37     /**
   38      * Read algorithm from file
   39      * @param runData perf-run-data used at running the tasks.
   40      * @throws Exception if errors while parsing the algorithm 
   41      */
   42     public Algorithm (PerfRunData runData) throws Exception {
   43       String algTxt = runData.getConfig().getAlgorithmText();
   44       sequence = new TaskSequence(runData,null,null,false);
   45       TaskSequence currSequence = sequence;
   46       PerfTask prevTask = null;
   47       StreamTokenizer stok = new StreamTokenizer(new StringReader(algTxt));
   48       stok.commentChar('#');
   49       stok.eolIsSignificant(false);
   50       stok.ordinaryChar('"');
   51       stok.ordinaryChar('/');
   52       stok.ordinaryChar('(');
   53       stok.ordinaryChar(')');
   54       stok.ordinaryChar('-');
   55       boolean colonOk = false; 
   56       boolean isDisableCountNextTask = false; // only for primitive tasks
   57       currSequence.setDepth(0);
   58       String taskPackage = PerfTask.class.getPackage().getName() + ".";
   59       
   60       while (stok.nextToken() != StreamTokenizer.TT_EOF) { 
   61         switch(stok.ttype) {
   62     
   63           case StreamTokenizer.TT_WORD:
   64             String s = stok.sval;
   65             Constructor<? extends PerfTask> cnstr = Class.forName(taskPackage+s+"Task")
   66               .asSubclass(PerfTask.class).getConstructor(PerfRunData.class);
   67             PerfTask task = cnstr.newInstance(runData);
   68             task.setDisableCounting(isDisableCountNextTask);
   69             isDisableCountNextTask = false;
   70             currSequence.addTask(task);
   71             if (task instanceof RepSumByPrefTask) {
   72               stok.nextToken();
   73               String prefix = stok.sval;
   74               if (prefix==null || prefix.length()==0) { 
   75                 throw new Exception("named report prefix problem - "+stok.toString()); 
   76               }
   77               ((RepSumByPrefTask) task).setPrefix(prefix);
   78             }
   79             // check for task param: '(' someParam ')'
   80             stok.nextToken();
   81             if (stok.ttype!='(') {
   82               stok.pushBack();
   83             } else {
   84               // get params, for tasks that supports them, - anything until next ')'
   85               StringBuffer params = new StringBuffer();
   86               stok.nextToken();
   87               while (stok.ttype!=')') { 
   88                 switch (stok.ttype) {
   89                   case StreamTokenizer.TT_NUMBER:  
   90                     params.append(stok.nval);
   91                     break;
   92                   case StreamTokenizer.TT_WORD:    
   93                     params.append(stok.sval);             
   94                     break;
   95                   case StreamTokenizer.TT_EOF:     
   96                     throw new Exception("unexpexted EOF: - "+stok.toString());
   97                   default:
   98                     params.append((char)stok.ttype);
   99                 }
  100                 stok.nextToken();
  101               }
  102               String prm = params.toString().trim();
  103               if (prm.length()>0) {
  104                 task.setParams(prm);
  105               }
  106             }
  107   
  108             // ---------------------------------------
  109             colonOk = false; prevTask = task;
  110             break;
  111     
  112           default:
  113             char c = (char)stok.ttype;
  114             
  115             switch(c) {
  116             
  117               case ':' :
  118                 if (!colonOk) throw new Exception("colon unexpexted: - "+stok.toString());
  119                 colonOk = false;
  120                 // get repetitions number
  121                 stok.nextToken();
  122                 if ((char)stok.ttype == '*') {
  123                   ((TaskSequence)prevTask).setRepetitions(TaskSequence.REPEAT_EXHAUST);
  124                 } else {
  125                   if (stok.ttype!=StreamTokenizer.TT_NUMBER)  {
  126                     throw new Exception("expected repetitions number or XXXs: - "+stok.toString());
  127                   } else {
  128                     double num = stok.nval;
  129                     stok.nextToken();
  130                     if (stok.ttype == StreamTokenizer.TT_WORD && stok.sval.equals("s")) {
  131                       ((TaskSequence) prevTask).setRunTime(num);
  132                     } else {
  133                       stok.pushBack();
  134                       ((TaskSequence) prevTask).setRepetitions((int) num);
  135                     }
  136                   }
  137                 }
  138                 // check for rate specification (ops/min)
  139                 stok.nextToken();
  140                 if (stok.ttype!=':') {
  141                   stok.pushBack();
  142                 } else {
  143                   // get rate number
  144                   stok.nextToken();
  145                   if (stok.ttype!=StreamTokenizer.TT_NUMBER) throw new Exception("expected rate number: - "+stok.toString());
  146                   // check for unit - min or sec, sec is default
  147                   stok.nextToken();
  148                   if (stok.ttype!='/') {
  149                     stok.pushBack();
  150                     ((TaskSequence)prevTask).setRate((int)stok.nval,false); // set rate per sec
  151                   } else {
  152                     stok.nextToken();
  153                     if (stok.ttype!=StreamTokenizer.TT_WORD) throw new Exception("expected rate unit: 'min' or 'sec' - "+stok.toString());
  154                     String unit = stok.sval.toLowerCase();
  155                     if ("min".equals(unit)) {
  156                       ((TaskSequence)prevTask).setRate((int)stok.nval,true); // set rate per min
  157                     } else if ("sec".equals(unit)) {
  158                       ((TaskSequence)prevTask).setRate((int)stok.nval,false); // set rate per sec
  159                     } else {
  160                       throw new Exception("expected rate unit: 'min' or 'sec' - "+stok.toString());
  161                     }
  162                   }
  163                 }
  164                 colonOk = false;
  165                 break;
  166       
  167               case '{' : 
  168               case '[' :  
  169                 // a sequence
  170                 // check for sequence name
  171                 String name = null;
  172                 stok.nextToken();
  173                 if (stok.ttype!='"') {
  174                   stok.pushBack();
  175                 } else {
  176                   stok.nextToken();
  177                   name = stok.sval;
  178                   stok.nextToken();
  179                   if (stok.ttype!='"' || name==null || name.length()==0) { 
  180                     throw new Exception("sequence name problem - "+stok.toString()); 
  181                   }
  182                 }
  183                 // start the sequence
  184                 TaskSequence seq2 = new TaskSequence(runData, name, currSequence, c=='[');
  185                 currSequence.addTask(seq2);
  186                 currSequence = seq2;
  187                 colonOk = false;
  188                 break;
  189   
  190               case '&' :
  191                 if (currSequence.isParallel()) {
  192                   throw new Exception("Can only create background tasks within a serial task");
  193                 }
  194                 if (prevTask == null) {
  195                   throw new Exception("& was unexpected");
  196                 } else if (prevTask.getRunInBackground()) {
  197                   throw new Exception("double & was unexpected");
  198                 } else {
  199                   prevTask.setRunInBackground();
  200                 }
  201                 break;
  202       
  203               case '>' :
  204                 currSequence.setNoChildReport();
  205               case '}' : 
  206               case ']' : 
  207                 // end sequence
  208                 colonOk = true; prevTask = currSequence;
  209                 currSequence = currSequence.getParent();
  210                 break;
  211             
  212               case '-' :
  213                 isDisableCountNextTask = true;
  214                 break;
  215                 
  216             } //switch(c)
  217             break;
  218             
  219         } //switch(stok.ttype)
  220         
  221       }
  222       
  223       if (sequence != currSequence) {
  224         throw new Exception("Unmatched sequences");
  225       }
  226       
  227       // remove redundant top level enclosing sequences
  228       while (sequence.isCollapsable() && sequence.getRepetitions()==1 && sequence.getRate()==0) {
  229         ArrayList<PerfTask> t = sequence.getTasks();
  230         if (t!=null && t.size()==1) {
  231           PerfTask p = t.get(0);
  232           if (p instanceof TaskSequence) {
  233             sequence = (TaskSequence) p;
  234             continue;
  235           }
  236         }
  237         break;
  238       }
  239     }
  240   
  241     /* (non-Javadoc)
  242      * @see java.lang.Object#toString()
  243      */
  244     @Override
  245     public String toString() {
  246       String newline = System.getProperty("line.separator");
  247       StringBuffer sb = new StringBuffer();
  248       sb.append(sequence.toString());
  249       sb.append(newline);
  250       return sb.toString();
  251     }
  252   
  253     /**
  254      * Execute this algorithm
  255      * @throws Exception 
  256      */
  257     public void execute() throws Exception {
  258       try {
  259         sequence.runAndMaybeStats(true);
  260       } finally {
  261         sequence.close();
  262       }
  263     }
  264   
  265     /**
  266      * Expert: for test purposes, return all tasks participating in this algorithm.
  267      * @return all tasks participating in this algorithm.
  268      */
  269     public ArrayList<PerfTask> extractTasks() {
  270       ArrayList<PerfTask> res = new ArrayList<PerfTask>();
  271       extractTasks(res, sequence);
  272       return res;
  273     }
  274     private void extractTasks (ArrayList<PerfTask> extrct, TaskSequence seq) {
  275       if (seq==null) 
  276         return;
  277       extrct.add(seq);
  278       ArrayList<PerfTask> t = sequence.getTasks();
  279       if (t==null) 
  280         return;
  281       for (final PerfTask p : t) {
  282         if (p instanceof TaskSequence) {
  283           extractTasks(extrct, (TaskSequence)p);
  284         } else {
  285           extrct.add(p);
  286         }
  287       }
  288     }
  289     
  290   }
  291   

Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » benchmark » byTask » utils » [javadoc | source]