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.BufferedReader;
   21   import java.io.ByteArrayInputStream;
   22   import java.io.IOException;
   23   import java.io.Reader;
   24   import java.util.ArrayList;
   25   import java.util.List;
   26   import java.util.Collections;
   27   import java.util.HashMap;
   28   import java.util.Properties;
   29   import java.util.StringTokenizer;
   30   
   31   /**
   32    * Perf run configuration properties.
   33    * <p>
   34    * Numeric property containing ":", e.g. "10:100:5" is interpreted 
   35    * as array of numeric values. It is extracted once, on first use, and 
   36    * maintain a round number to return the appropriate value.
   37    * <p>
   38    * The config property "work.dir" tells where is the root of 
   39    * docs data dirs and indexes dirs. It is set to either of: <ul>
   40    * <li>value supplied for it in the alg file;</li>
   41    * <li>otherwise, value of System property "benchmark.work.dir";</li>
   42    * <li>otherwise, "work".</li>
   43    * </ul>
   44    */
   45   public class Config {
   46   
   47     private static final String NEW_LINE = System.getProperty("line.separator");
   48   
   49     private int roundNumber = 0;
   50     private Properties props;
   51     private HashMap<String,Object> valByRound = new HashMap<String,Object>();
   52     private HashMap<String,String> colForValByRound = new HashMap<String,String>();
   53     private String algorithmText;
   54   
   55     /**
   56      * Read both algorithm and config properties.
   57      * @param algReader from where to read algorithm and config properties.
   58      * @throws IOException
   59      */
   60     public Config (Reader algReader) throws IOException {
   61       // read alg file to array of lines
   62       ArrayList<String> lines = new ArrayList<String>();
   63       BufferedReader r = new BufferedReader(algReader);
   64       int lastConfigLine=0;
   65       for (String line = r.readLine(); line!=null; line=r.readLine()) {
   66         lines.add(line);
   67         if (line.indexOf('=')>0) {
   68           lastConfigLine = lines.size();
   69         }
   70       }
   71       r.close();
   72       // copy props lines to string
   73       StringBuffer sb = new StringBuffer();
   74       for (int i=0; i<lastConfigLine; i++) {
   75         sb.append(lines.get(i));
   76         sb.append(NEW_LINE);
   77       }
   78       // read props from string
   79       this.props = new Properties();
   80       props.load(new ByteArrayInputStream(sb.toString().getBytes()));
   81   
   82       // make sure work dir is set properly 
   83       if (props.get("work.dir")==null) {
   84         props.setProperty("work.dir",System.getProperty("benchmark.work.dir","work"));
   85       }
   86       
   87       if (Boolean.valueOf(props.getProperty("print.props","true")).booleanValue()) {
   88         printProps();
   89       }
   90       
   91       // copy algorithm lines
   92       sb = new StringBuffer();
   93       for (int i=lastConfigLine; i<lines.size(); i++) {
   94         sb.append(lines.get(i));
   95         sb.append(NEW_LINE);
   96       }
   97       algorithmText = sb.toString();
   98     }
   99   
  100     /**
  101      * Create config without algorithm - useful for a programmatic perf test.
  102      * @param props - configuration properties.
  103      * @throws IOException
  104      */
  105     public Config (Properties props) {
  106       this.props = props;
  107       if (Boolean.valueOf(props.getProperty("print.props","true")).booleanValue()) {
  108         printProps();
  109       }
  110     }
  111   
  112     private void printProps() {
  113       System.out.println("------------> config properties:");
  114       List<String> propKeys = new ArrayList(props.keySet());
  115       Collections.sort(propKeys);
  116       for (final String propName : propKeys) {
  117         System.out.println(propName + " = " + props.getProperty(propName));
  118       }
  119       System.out.println("-------------------------------");
  120     }
  121   
  122     /**
  123      * Return a string property.
  124      * @param name name of property.
  125      * @param dflt default value.
  126      * @return a string property.
  127      */
  128     public String get (String name, String dflt) {
  129       return props.getProperty(name,dflt);
  130     }
  131   
  132     /**
  133      * Set a property.
  134      * Note: once a multiple values property is set, it can no longer be modified.
  135      * @param name name of property.
  136      * @param value either single or multiple property value (multiple values are separated by ":")
  137      * @throws Exception 
  138      */
  139     public void set (String name, String value) throws Exception {
  140       if (valByRound.get(name) != null) {
  141         throw new Exception("Cannot modify a multi value property!");
  142       }
  143       props.setProperty(name,value);
  144     }
  145   
  146     /**
  147      * Return an int property.
  148      * If the property contain ":", e.g. "10:100:5", it is interpreted 
  149      * as array of ints. It is extracted once, on first call
  150      * to get() it, and a by-round-value is returned. 
  151      * @param name name of property
  152      * @param dflt default value
  153      * @return a int property.
  154      */
  155     public int get (String name, int dflt) {
  156       // use value by round if already parsed
  157       int vals[] = (int[]) valByRound.get(name);
  158       if (vals != null) {
  159         return vals[roundNumber % vals.length];
  160       }
  161       // done if not by round 
  162       String sval = props.getProperty(name,""+dflt);
  163       if (sval.indexOf(":")<0) {
  164         return Integer.parseInt(sval);
  165       }
  166       // first time this prop is extracted by round
  167       int k = sval.indexOf(":");
  168       String colName = sval.substring(0,k);
  169       sval = sval.substring(k+1);
  170       colForValByRound.put(name,colName);
  171       vals = propToIntArray(sval);
  172       valByRound.put(name,vals);
  173       return vals[roundNumber % vals.length];
  174     }
  175     
  176     /**
  177      * Return a double property.
  178      * If the property contain ":", e.g. "10:100:5", it is interpreted 
  179      * as array of doubles. It is extracted once, on first call
  180      * to get() it, and a by-round-value is returned. 
  181      * @param name name of property
  182      * @param dflt default value
  183      * @return a double property.
  184      */
  185     public double get (String name, double dflt) {
  186       // use value by round if already parsed
  187       double vals[] = (double[]) valByRound.get(name);
  188       if (vals != null) {
  189         return vals[roundNumber % vals.length];
  190       }
  191       // done if not by round 
  192       String sval = props.getProperty(name,""+dflt);
  193       if (sval.indexOf(":")<0) {
  194         return Double.parseDouble(sval);
  195       }
  196       // first time this prop is extracted by round
  197       int k = sval.indexOf(":");
  198       String colName = sval.substring(0,k);
  199       sval = sval.substring(k+1);
  200       colForValByRound.put(name,colName);
  201       vals = propToDoubleArray(sval);
  202       valByRound.put(name,vals);
  203       return vals[roundNumber % vals.length];
  204     }
  205     
  206     /**
  207      * Return a boolean property.
  208      * If the property contain ":", e.g. "true.true.false", it is interpreted 
  209      * as array of booleans. It is extracted once, on first call
  210      * to get() it, and a by-round-value is returned. 
  211      * @param name name of property
  212      * @param dflt default value
  213      * @return a int property.
  214      */
  215     public boolean get (String name, boolean dflt) {
  216       // use value by round if already parsed
  217       boolean vals[] = (boolean[]) valByRound.get(name);
  218       if (vals != null) {
  219         return vals[roundNumber % vals.length];
  220       }
  221       // done if not by round 
  222       String sval = props.getProperty(name,""+dflt);
  223       if (sval.indexOf(":")<0) {
  224         return Boolean.valueOf(sval).booleanValue();
  225       }
  226       // first time this prop is extracted by round 
  227       int k = sval.indexOf(":");
  228       String colName = sval.substring(0,k);
  229       sval = sval.substring(k+1);
  230       colForValByRound.put(name,colName);
  231       vals = propToBooleanArray(sval);
  232       valByRound.put(name,vals);
  233       return vals[roundNumber % vals.length];
  234     }
  235     
  236     /**
  237      * Increment the round number, for config values that are extracted by round number. 
  238      * @return the new round number.
  239      */
  240     public int newRound () {
  241       roundNumber++;
  242       
  243       StringBuffer sb = new StringBuffer("--> Round ").append(roundNumber-1).append("-->").append(roundNumber);
  244   
  245       // log changes in values
  246       if (valByRound.size()>0) {
  247         sb.append(": ");
  248         for (final String name : valByRound.keySet()) {
  249           Object a = valByRound.get(name);
  250           if (a instanceof int[]) {
  251             int ai[] = (int[]) a;
  252             int n1 = (roundNumber-1)%ai.length;
  253             int n2 = roundNumber%ai.length;
  254             sb.append("  ").append(name).append(":").append(ai[n1]).append("-->").append(ai[n2]);
  255           } else if (a instanceof double[]){
  256             double ad[] = (double[]) a;
  257             int n1 = (roundNumber-1)%ad.length;
  258             int n2 = roundNumber%ad.length;
  259             sb.append("  ").append(name).append(":").append(ad[n1]).append("-->").append(ad[n2]);
  260           }
  261           else {
  262             boolean ab[] = (boolean[]) a;
  263             int n1 = (roundNumber-1)%ab.length;
  264             int n2 = roundNumber%ab.length;
  265             sb.append("  ").append(name).append(":").append(ab[n1]).append("-->").append(ab[n2]);
  266           }
  267         }
  268       }
  269   
  270       System.out.println();
  271       System.out.println(sb.toString());
  272       System.out.println();
  273       
  274       return roundNumber;
  275     }
  276     
  277     // extract properties to array, e.g. for "10:100:5" return int[]{10,100,5}. 
  278     private int[] propToIntArray (String s) {
  279       if (s.indexOf(":")<0) {
  280         return new int [] { Integer.parseInt(s) };
  281       }
  282       
  283       ArrayList<Integer> a = new ArrayList<Integer>();
  284       StringTokenizer st = new StringTokenizer(s,":");
  285       while (st.hasMoreTokens()) {
  286         String t = st.nextToken();
  287         a.add(Integer.valueOf(t));
  288       }
  289       int res[] = new int[a.size()]; 
  290       for (int i=0; i<a.size(); i++) {
  291         res[i] = a.get(i).intValue();
  292       }
  293       return res;
  294     }
  295       
  296     // extract properties to array, e.g. for "10.7:100.4:-2.3" return int[]{10.7,100.4,-2.3}. 
  297     private double[] propToDoubleArray (String s) {
  298       if (s.indexOf(":")<0) {
  299         return new double [] { Double.parseDouble(s) };
  300       }
  301       
  302       ArrayList<Double> a = new ArrayList<Double>();
  303       StringTokenizer st = new StringTokenizer(s,":");
  304       while (st.hasMoreTokens()) {
  305         String t = st.nextToken();
  306         a.add(Double.valueOf(t));
  307       }
  308       double res[] = new double[a.size()]; 
  309       for (int i=0; i<a.size(); i++) {
  310         res[i] = a.get(i).doubleValue();
  311       }
  312       return res;
  313     }
  314       
  315     // extract properties to array, e.g. for "true:true:false" return boolean[]{true,false,false}. 
  316     private boolean[] propToBooleanArray (String s) {
  317       if (s.indexOf(":")<0) {
  318         return new boolean [] { Boolean.valueOf(s).booleanValue() };
  319       }
  320       
  321       ArrayList<Boolean> a = new ArrayList<Boolean>();
  322       StringTokenizer st = new StringTokenizer(s,":");
  323       while (st.hasMoreTokens()) {
  324         String t = st.nextToken();
  325         a.add(new Boolean(t));
  326       }
  327       boolean res[] = new boolean[a.size()]; 
  328       for (int i=0; i<a.size(); i++) {
  329         res[i] = a.get(i).booleanValue();
  330       }
  331       return res;
  332     }
  333   
  334     /**
  335      * @return names of params set by round, for reports title
  336      */
  337     public String getColsNamesForValsByRound() {
  338       if (colForValByRound.size()==0) {
  339         return "";
  340       }
  341       StringBuffer sb = new StringBuffer(); 
  342       for (final String name : colForValByRound.keySet()) {
  343         String colName = colForValByRound.get(name);
  344         sb.append(" ").append(colName);
  345       }
  346       return sb.toString();
  347     }
  348   
  349     /**
  350      * @return values of params set by round, for reports lines.
  351      */
  352     public String getColsValuesForValsByRound(int roundNum) {
  353       if (colForValByRound.size()==0) {
  354         return "";
  355       }
  356       StringBuffer sb = new StringBuffer(); 
  357       for (final String name  : colForValByRound.keySet()) {
  358         String colName = colForValByRound.get(name);
  359         String template = " "+colName;
  360         if (roundNum<0) {
  361           // just append blanks
  362           sb.append(Format.formatPaddLeft("-",template));
  363         } else {
  364           // append actual values, for that round
  365           Object a = valByRound.get(name);
  366           if (a instanceof int[]) {
  367             int ai[] = (int[]) a;
  368             int n = roundNum % ai.length;
  369             sb.append(Format.format(ai[n],template));
  370           }
  371           else if (a instanceof double[]) {
  372             double ad[] = (double[]) a;
  373             int n = roundNum % ad.length;
  374             sb.append(Format.format(2, ad[n],template));
  375           }
  376           else {
  377             boolean ab[] = (boolean[]) a;
  378             int n = roundNum % ab.length;
  379             sb.append(Format.formatPaddLeft(""+ab[n],template));
  380           }
  381         }
  382       }
  383       return sb.toString();
  384     }
  385   
  386     /**
  387      * @return the round number.
  388      */
  389     public int getRoundNumber() {
  390       return roundNumber;
  391     }
  392   
  393     /**
  394      * @return Returns the algorithmText.
  395      */
  396     public String getAlgorithmText() {
  397       return algorithmText;
  398     }
  399   
  400   }

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