001: /**
002: * InstantJ
003: *
004: * Copyright (C) 2002 Nils Meier
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */package instantj.tst;
017:
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: import instantj.compile.CompilationFailedException;
022: import instantj.expression.Expression;
023: import instantj.expression.ExpressionInstance;
024: import instantj.reflect.ReflectAccess;
025:
026: /**
027: * This Test will compare the performance of dynamic expressions
028: * with 'normal' Java code
029: */
030: public class PerformanceTest {
031:
032: /** the loop amount */
033: private final static int TEST_LOOP_COUNT = 10000;
034:
035: /** the text used in the test */
036: private final static String TEST_MESSAGE = "the quick brown fox jumped over the lazy dog."
037: + "the quick brown fox jumped over the lazy dog."
038: + "the quick brown fox jumped over the lazy dog."
039: + "the quick brown fox jumped over the lazy dog."
040: + "the quick brown fox jumped over the lazy dog."
041: + "the quick brown fox jumped over the lazy dog."
042: + "the quick brown fox jumped over the lazy dog."
043: + "the quick brown fox jumped over the lazy dog."
044: + "the quick brown fox jumped over the lazy dog."
045: + "the quick brown fox jumped over the lazy dog.";
046:
047: /**
048: * MAIN
049: */
050: public static void main(String[] args) {
051:
052: // Disclaimer
053: System.out.println("InstantJ - "
054: + ReflectAccess.getInstance().calcClassNameOf(
055: PerformanceTest.class));
056:
057: // Run it
058: new PerformanceTest().test();
059:
060: // Done
061: }
062:
063: /**
064: * testing
065: */
066: private void test() {
067:
068: testStatic();
069: testDynamic();
070: }
071:
072: /**
073: * testing
074: */
075: private void testStatic() {
076:
077: System.out
078: .println("Creating instance of static code and running it "
079: + TEST_LOOP_COUNT + " times");
080:
081: // Create an instance of StaticCode and run n times
082: StaticCode staticCode = new StaticCode();
083:
084: long start = System.currentTimeMillis();
085:
086: for (int i = 0; i < TEST_LOOP_COUNT; i++) {
087: staticCode.setMessage(TEST_MESSAGE);
088: staticCode.evaluate();
089: }
090:
091: // Done
092: System.out.println("... done in "
093: + (System.currentTimeMillis() - start) + " ms");
094: }
095:
096: /**
097: * testing
098: */
099: private void testDynamic() {
100:
101: System.out
102: .println("Creating instance of dynamic code and running it "
103: + TEST_LOOP_COUNT + " times");
104:
105: // Create an instance of DynamicCode and run n times
106: try {
107: DynamicCode dynamicCode = new DynamicCode();
108:
109: ExpressionInstance e = dynamicCode.getInstance();
110:
111: long start = System.currentTimeMillis();
112:
113: for (int i = 0; i < TEST_LOOP_COUNT; i++) {
114: e.set("message", TEST_MESSAGE);
115: e.evaluate();
116: }
117:
118: System.out.println("... done in "
119: + (System.currentTimeMillis() - start) + " ms");
120:
121: } catch (Throwable t) {
122: System.out.println("O.K. this should not have happened : "
123: + t.getClass() + "#" + t.getMessage());
124: }
125:
126: // Done
127: }
128:
129: /**
130: * The test code as expression
131: */
132: static class DynamicCode extends Expression {
133:
134: /** Constructor */
135: DynamicCode() throws CompilationFailedException {
136: super (body, getPropertyMap());
137: }
138:
139: /** helper for property map */
140: static Map getPropertyMap() {
141: Map ps = new HashMap();
142: ps.put("message", String.class);
143: return ps;
144: }
145:
146: /** functionality */
147: static String body = " for (int i=0;i<message.length();i++) { "
148: + " message.charAt(i); "
149: + " }; "
150: + " message ";
151:
152: } // EOC
153:
154: /**
155: * The test code compiled
156: */
157: static class StaticCode {
158:
159: /** the message property */
160: private String message;
161:
162: /** Constructor */
163: StaticCode() {
164: super ();
165: }
166:
167: /** setter */
168: void setMessage(String message) {
169: this .message = message;
170: }
171:
172: /** functionality */
173: String evaluate() {
174: for (int i = 0; i < message.length(); i++) {
175: message.charAt(i);
176: }
177: return message;
178: }
179:
180: } // EOC
181:
182: }
|