FileDocCategorySizeDatePackage
Config.javaAPI DocApache Lucene 2.1.010782Wed Feb 14 10:46:14 GMT 2007org.apache.lucene.benchmark.byTask.utils

Config

public class Config extends Object
Perf run configuration properties. Numeric peroperty containing ":", e.g. "10:100:5" is interpreted as array of numeric values. It is extracted once, on first use, and maintain an round number to return the appropriate value.

Fields Summary
private static final String
NEW_LINE
private int
roundNumber
private Properties
props
private HashMap
valByRound
private HashMap
colForValByRound
private String
algorithmText
Constructors Summary
public Config(File algFile)
Read config from file containing both algorithm and config properties.

param
algFile file containing both algorithm and config properties.
throws
IOException


                          
        
    // read alg file to array of lines
    ArrayList lines = new ArrayList();
    BufferedReader r = new BufferedReader(new FileReader(algFile));
    int lastConfigLine=0;
    for (String line = r.readLine(); line!=null; line=r.readLine()) {
      lines.add(line);
      if (line.indexOf('=")>0) {
        lastConfigLine = lines.size();
      }
    }
    r.close();
    // copy props lines to string
    StringBuffer sb = new StringBuffer();
    for (int i=0; i<lastConfigLine; i++) {
      sb.append(lines.get(i));
      sb.append(NEW_LINE);
    }
    // read props from string
    this.props = new Properties();
    props.load(new ByteArrayInputStream(sb.toString().getBytes()));

    if (Boolean.valueOf(props.getProperty("print.props","true")).booleanValue()) {
      printProps();
    }
    
    // copy algorithm lines
    sb = new StringBuffer();
    for (int i=lastConfigLine; i<lines.size(); i++) {
      sb.append(lines.get(i));
      sb.append(NEW_LINE);
    }
    algorithmText = sb.toString();
  
public Config(Properties props)
Create config without algorithm - usefull for a programmatic perf test.

param
props - configuration properties.
throws
IOException

    this.props = props;
    if (Boolean.valueOf(props.getProperty("print.props","true")).booleanValue()) {
      printProps();
    }
  
Methods Summary
public java.lang.Stringget(java.lang.String name, java.lang.String dflt)
Return a string property.

param
name name of property.
param
dflt default value.
return
a string property.

    return props.getProperty(name,dflt);
  
public intget(java.lang.String name, int dflt)
Return an int property. If the property contain ":", e.g. "10:100:5", it is interpreted as array of ints. It is extracted once, on first call to get() it, and a by-round-value is returned.

param
name name of property
param
dflt default value
return
a int property.

    // use value by round if already parsed
    int vals[] = (int[]) valByRound.get(name);
    if (vals != null) {
      return vals[roundNumber % vals.length];
    }
    // done if not by round 
    String sval = props.getProperty(name,""+dflt);
    if (sval.indexOf(":")<0) {
      return Integer.parseInt(sval);
    }
    // first time this prop is extracted by round
    int k = sval.indexOf(":");
    String colName = sval.substring(0,k);
    sval = sval.substring(k+1);
    colForValByRound.put(name,colName);
    vals = propToIntArray(sval);
    valByRound.put(name,vals);
    return vals[roundNumber % vals.length];
  
public booleanget(java.lang.String name, boolean dflt)
Return a boolean property. If the property contain ":", e.g. "true.true.false", it is interpreted as array of boleans. It is extracted once, on first call to get() it, and a by-round-value is returned.

param
name name of property
param
dflt default value
return
a int property.

    // use value by round if already parsed
    boolean vals[] = (boolean[]) valByRound.get(name);
    if (vals != null) {
      return vals[roundNumber % vals.length];
    }
    // done if not by round 
    String sval = props.getProperty(name,""+dflt);
    if (sval.indexOf(":")<0) {
      return Boolean.valueOf(sval).booleanValue();
    }
    // first time this prop is extracted by round 
    int k = sval.indexOf(":");
    String colName = sval.substring(0,k);
    sval = sval.substring(k+1);
    colForValByRound.put(name,colName);
    vals = propToBooleanArray(sval);
    valByRound.put(name,vals);
    return vals[roundNumber % vals.length];
  
public java.lang.StringgetAlgorithmText()

return
Returns the algorithmText.

    return algorithmText;
  
public java.lang.StringgetColsNamesForValsByRound()

return
names of params set by round, for reports title

    if (colForValByRound.size()==0) {
      return "";
    }
    StringBuffer sb = new StringBuffer(); 
    for (Iterator it = colForValByRound.keySet().iterator(); it.hasNext();) {
      String name = (String) it.next();
      String colName = (String) colForValByRound.get(name);
      sb.append(" ").append(colName);
    }
    return sb.toString();
  
public java.lang.StringgetColsValuesForValsByRound(int roundNum)

return
values of params set by round, for reports lines.

    if (colForValByRound.size()==0) {
      return "";
    }
    StringBuffer sb = new StringBuffer(); 
    for (Iterator it = colForValByRound.keySet().iterator(); it.hasNext();) {
      String name = (String) it.next();
      String colName = (String) colForValByRound.get(name);
      String template = " "+colName;
      if (roundNum<0) {
        // just append blanks
        sb.append(Format.formatPaddLeft("-",template));
      } else {
        // append actual values, for that round
        Object a = valByRound.get(name);
        if (a instanceof int[]) {
          int ai[] = (int[]) a;
          int n = roundNum % ai.length;
          sb.append(Format.format(ai[n],template));
        } else {
          boolean ab[] = (boolean[]) a;
          int n = roundNum % ab.length;
          sb.append(Format.formatPaddLeft(""+ab[n],template));
        }
      }
    }
    return sb.toString();
  
public intgetRoundNumber()

return
the round number.

    return roundNumber;
  
public intnewRound()
Increment the round number, for config values that are extracted by round number.

return
the new round number.

    roundNumber++;
    
    // log changes in values
    if (valByRound.size()>0) {
      StringBuffer sb = new StringBuffer("--> Round ").append(roundNumber-1).append("-->").append(roundNumber).append(": ");
      for (Iterator iter = valByRound.keySet().iterator(); iter.hasNext();) {
        String name = (String) iter.next();
        Object a = valByRound.get(name);
        if (a instanceof int[]) {
          int ai[] = (int[]) a;
          int n1 = (roundNumber-1)%ai.length;
          int n2 = roundNumber%ai.length;
          sb.append("  ").append(name).append(":").append(ai[n1]).append("-->").append(ai[n2]);
        } else {
          boolean ab[] = (boolean[]) a;
          int n1 = (roundNumber-1)%ab.length;
          int n2 = roundNumber%ab.length;
          sb.append("  ").append(name).append(":").append(ab[n1]).append("-->").append(ab[n2]);
        }
      }
      System.out.println();
      System.out.println(sb.toString());
      System.out.println();
    }
    
    return roundNumber;
  
private voidprintProps()

    System.out.println("------------> config properties:");
    for (Iterator it = props.keySet().iterator(); it.hasNext();) {
      String propName = (String) it.next();
      System.out.println(propName + " = " + props.getProperty(propName));
    }
    System.out.println("-------------------------------");
  
private boolean[]propToBooleanArray(java.lang.String s)

    if (s.indexOf(":")<0) {
      return new boolean [] { Boolean.valueOf(s).booleanValue() };
    }
    
    ArrayList a = new ArrayList();
    StringTokenizer st = new StringTokenizer(s,":");
    while (st.hasMoreTokens()) {
      String t = st.nextToken();
      a.add(new Boolean(t));
    }
    boolean res[] = new boolean[a.size()]; 
    for (int i=0; i<a.size(); i++) {
      res[i] = ((Boolean) a.get(i)).booleanValue();
    }
    return res;
  
private int[]propToIntArray(java.lang.String s)

    if (s.indexOf(":")<0) {
      return new int [] { Integer.parseInt(s) };
    }
    
    ArrayList a = new ArrayList();
    StringTokenizer st = new StringTokenizer(s,":");
    while (st.hasMoreTokens()) {
      String t = st.nextToken();
      a.add(new Integer(t));
    }
    int res[] = new int[a.size()]; 
    for (int i=0; i<a.size(); i++) {
      res[i] = ((Integer) a.get(i)).intValue();
    }
    return res;
  
public voidset(java.lang.String name, java.lang.String value)
Set a property. Note: once a multiple values property is set, it can no longer be modified.

param
name name of property.
param
value either single or multiple propery value (multple values are separated by ":")
throws
Exception

    if (valByRound.get(name) != null) {
      throw new Exception("Cannot modify a multi value property!");
    }
    props.setProperty(name,value);