summaryrefslogtreecommitdiff
path: root/trunk/users/metalab/AoI/plugins/CSGEvaluator/src/org/cheffo/jeplite/test/Tests.java
blob: c14ad33fb89e996ad9285f63c7864423ef33efc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package org.cheffo.jeplite.test;

import java.util.*;
import java.io.*;

import org.cheffo.jeplite.*;
import org.cheffo.jeplite.util.*;
import org.cheffo.jeplite.optimizer.*;

/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:
 * @author
 * @version 1.0
 */

/**
 * Provided to check for backward compability and performance.
 */
public class Tests {

  public Tests() {
  }

  static void parseParams(String[] args) {
    for(int i=0; i<args.length; i+=2) {
      params.put(args[i].toLowerCase().trim(), args[i+1]);
    }
  }

  static JEP jep = new JEP();
  static HashMap params = new HashMap();

  static void doIt(String toParse, BufferedWriter fw, SimpleNode optNode) throws Exception {
    long[] testArray = new long[10];
    double d = 0;
    DoubleStack stack = new DoubleStack();
    for(int j=0; j<10; j++) {
      Thread.yield();
      long start = System.currentTimeMillis();
      for(int i=0; i<100000; i++){
	d = optNode.getValue();
      }
      testArray[j] = System.currentTimeMillis()-start;
    }
    fw.write("100000 evaluations: ");
    for(int j=0; j<10; j++) {
      fw.write(testArray[j]+", ");
    }
    fw.write("\n");
  }

  public static void main(String[] args) throws Exception {
    Date startTime = new Date();
    parseParams(args);
    File inFile = new File((String)params.get("-file"));
    File logFile = new File((String)params.get("-logfile"));

    BufferedReader fr = new BufferedReader(new FileReader(inFile));
    BufferedWriter fw = new BufferedWriter(new FileWriter(logFile));


    jep.addStandardConstants();
    jep.addStandardFunctions();

    String curLine = null;
    int lines = 0;
    while(null!=(curLine=fr.readLine())) {
      if(curLine.startsWith("#"))
	continue;
      String description = curLine;
      String toParse = fr.readLine();
      double result = Double.parseDouble(fr.readLine().trim());

      fw.write("Processing:"+toParse+",\n");
      fw.write("Expected: "+result+"\n");
      fw.flush();

      // Give enought time to the jit compiler.
      double d = 0;
      DoubleStack stack = new DoubleStack();
      for(int i=0; i<1000; i++) {
	jep.parseExpression(toParse);
	d = jep.getValue(stack);
      }
      SimpleNode optNode = jep.getTopNode();
      //if(Boolean.getBoolean("noopt")) {
	fw.write("Not Optimized: ");
	doIt(toParse, fw, optNode);
      //}

      //if(Boolean.getBoolean("opt")) {
	ExpressionOptimizer optimizer = new ExpressionOptimizer(jep.getTopNode());
	//optimizer.addConst("pi");
	//optimizer.addConst("e");
	optNode = optimizer.optimize();

	fw.write("Optimized    : ");

	doIt(toParse, fw, optNode);

	long[] testArray = new long[10];
	for(int j=0; j<10; j++) {
	  Thread.yield();
	  long start = System.currentTimeMillis();
	  for(int i=0; i<1000; i++){
	    jep.parseExpression(toParse);
	  }
	  testArray[j] = System.currentTimeMillis()-start;
	}
	fw.write("1000 parses: ");
	for(int j=0; j<10; j++) {
	  fw.write(testArray[j]+", ");
	}
	fw.write("\n");
	fw.write(d+"\n_____________________________________________________\n");
	fw.flush();
      //}
    }
    Date endTime = new Date();
    fw.write("Start time: "+startTime+"\n");
    fw.write("End time: "+endTime+"\n");
    fw.write("Total time: "+(endTime.getTime()-startTime.getTime())+ " ms");
    fw.close();
  }
}