KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > perf > PerfRunner


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: PerfRunner.java,v 1.2 2005/01/26 08:29:25 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.perf;
25
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.PrintStream JavaDoc;
29 import java.math.BigDecimal JavaDoc;
30
31 import org.enhydra.xml.xmlc.XMLCFactory;
32 import org.enhydra.xml.xmlc.XMLCStdFactory;
33 import org.enhydra.xml.xmlc.XMLObject;
34
35 /**
36  * Run XMLC generated objects for performance testing.
37  * PerfRunner class
38  */

39 public class PerfRunner {
40     /**
41      * Constants controling number of tests.
42      */

43     private static final int TEST_COUNT = 6; //4;
44
private static final int REPEAT_COUNT = 10; //10;
45
private static final String JavaDoc TEST_RESULTS_FILE = "perf.out";
46     private static final String JavaDoc NOOP_EDITOR =
47     "tests.org.enhydra.xml.xmlc.perf.NoOpEditor";
48
49     class TimeResult {
50     // in seconds...
51
private double createTime;
52     private double toDocumentTime;
53     private double editTime;
54     private int iterations;
55     private int editedNodeCount;
56     TimeResult(long createTime, long editTime,
57            long toDocumentTime, int iterations,
58            int editedNodeCount) {
59         this.createTime = createTime;
60         this.editTime = editTime;
61         this.toDocumentTime = toDocumentTime;
62         this.iterations = iterations;
63         this.editedNodeCount = editedNodeCount;
64     }
65     double getTotalTime() {
66         return getCreateTime() + getToDocumentTime() + getEditTime();
67     }
68     Number JavaDoc getTotalTime(int scale) {
69         return getScaledNumber(getTotalTime(), scale);
70     }
71     Number JavaDoc getAverageTime(int scale) {
72         return getScaledNumber(getTotalTime()/iterations, scale);
73     }
74     double getCreateTime() {
75         return createTime/1000.0;
76     }
77     Number JavaDoc getAverageCreateTime(int scale) {
78         return getScaledNumber(getCreateTime()/iterations, scale);
79     }
80     double getToDocumentTime() {
81         return toDocumentTime/1000.0;
82     }
83     Number JavaDoc getAverageToDocumentTime(int scale) {
84         return getScaledNumber(getToDocumentTime()/iterations, scale);
85     }
86     double getEditTime() {
87         return editTime/1000.0;
88     }
89     Number JavaDoc getAverageEditTime(int scale) {
90         return getScaledNumber(getEditTime()/iterations, scale);
91     }
92     int getEditedNodeCount() {
93         return editedNodeCount/iterations;
94     }
95     }
96
97     /**
98      * @return a scaled version of a number. For presentation purposes.
99      */

100     Number JavaDoc getScaledNumber(double num, int scale)
101     throws NumberFormatException JavaDoc {
102     BigDecimal JavaDoc bd = new BigDecimal JavaDoc(num);
103     return bd.setScale(scale, BigDecimal.ROUND_HALF_UP);
104     }
105
106     /**
107      * @return a scaled version of a number. For presentation purposes.
108      */

109     Number JavaDoc calcRate(double num, int scale)
110     throws NumberFormatException JavaDoc {
111     if (num == 0) {
112         return new BigDecimal JavaDoc(-1);
113     }
114     BigDecimal JavaDoc bd = new BigDecimal JavaDoc(1.0/num);
115     return bd.setScale(scale, BigDecimal.ROUND_HALF_UP);
116     }
117
118     /**
119      * Loads an instance of a DOM editor.
120      *
121      * @param className the editor's class name.
122      * @return the editor.
123      */

124     static DOMEditor loadEditor(String JavaDoc className)
125     throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc {
126     return (DOMEditor)Class.forName(className).newInstance();
127     }
128
129     /**
130      * Create a XMLC document object and convert it to a string a specified
131      * number of times.
132      */

133     private TimeResult runTestIterations(Class JavaDoc docClass, DOMEditor editor,
134                      int repeatCount)
135     throws Exception JavaDoc {
136         XMLCFactory factory = new XMLCStdFactory(null, null);
137     long checkPoint;
138     long deltaCreate = 0, deltaEdit = 0, deltaToDocument = 0;
139         int editedNodeCount = 0;
140
141         for (int cnt = 0; cnt < repeatCount; cnt++) {
142
143         checkPoint = System.currentTimeMillis();
144             XMLObject xmlcObj = factory.create(docClass);
145         deltaCreate
146         += (System.currentTimeMillis() - checkPoint);
147
148         checkPoint = System.currentTimeMillis();
149             editedNodeCount += editor.edit(xmlcObj);
150         deltaEdit
151         += (System.currentTimeMillis() - checkPoint);
152
153         checkPoint = System.currentTimeMillis();
154             String JavaDoc docStr = xmlcObj.toDocument();
155         deltaToDocument
156         += (System.currentTimeMillis() - checkPoint);
157         }
158     return new TimeResult(deltaCreate, deltaEdit, deltaToDocument,
159                   repeatCount, editedNodeCount);
160     }
161
162     /**
163      * Run runTestIterations a number of times.
164      */

165     private void runTest(String JavaDoc docClassName,
166              int testCount,
167              int repeatCount,
168              DOMEditor editor,
169              PrintStream JavaDoc out)
170         throws Exception JavaDoc {
171     do {
172         Class JavaDoc docClass = Class.forName(docClassName);
173         TimeResult timeSec[] = new TimeResult[testCount];
174         
175         for (int cnt = 0; cnt < testCount; cnt++) {
176         timeSec[cnt] = runTestIterations(docClass, editor, repeatCount);
177         }
178
179         if (out != null) {
180         out.println("**Test class = " + docClass.getName());
181         out.println("**Test editor = " + editor);
182         out.println("**Test = " + TEST_COUNT
183                 + ", Repeat = " + REPEAT_COUNT);
184         out.println();
185         double totalSec = 0;
186         double totalCreateSec = 0;
187         double totalEditSec = 0;
188         double totalToDocumentSec = 0;
189
190         for (int cnt = 0; cnt < testCount; cnt++) {
191             out.println("Pass " + cnt + ":\t"
192                 + timeSec[cnt].getAverageTime(4)
193                 + " sec/iteration "
194                 + "(create="
195                 + timeSec[cnt].getAverageCreateTime(4)
196                 + ", edit="
197                 + timeSec[cnt].getAverageEditTime(5)
198                 + " (" + timeSec[cnt].getEditedNodeCount()
199                 + " nodes)"
200                 + ", toDocument="
201                 + timeSec[cnt].getAverageToDocumentTime(4)
202                 + ")");
203             totalSec += timeSec[cnt].getTotalTime();
204             totalCreateSec += timeSec[cnt].getCreateTime();
205             totalEditSec += timeSec[cnt].getEditTime();
206             totalToDocumentSec += timeSec[cnt].getToDocumentTime();
207         }
208         
209         double totalAveTimeSec =
210             totalSec/(repeatCount*testCount);
211         double totalAveCreateSec =
212             totalCreateSec/(repeatCount*testCount);
213         double totalAveEditSec =
214             totalEditSec/(repeatCount*testCount);
215         double totalAveToDocumentSec =
216             totalToDocumentSec/(repeatCount*testCount);
217         out.println();
218         out.println("Average: "
219                 + getScaledNumber(totalAveTimeSec, 4)
220                 + " sec/iteration "
221                 + "(create="
222                 + getScaledNumber(totalAveCreateSec, 4)
223                 + ", edit="
224                 + getScaledNumber(totalAveEditSec, 4)
225                 + ", toDocument="
226                 + getScaledNumber(totalAveToDocumentSec, 4)
227                 + ")");
228
229         out.println("Rate: "
230                 + calcRate(totalAveTimeSec, 2)
231                 + " iterations/sec "
232                 + "(create="
233                 + calcRate(totalAveCreateSec, 2)
234                 + ", edit="
235                 + calcRate(totalAveEditSec, 2)
236                 + ", toDocument="
237                 + calcRate(totalAveToDocumentSec, 2)
238                 + ")");
239
240         out.println();
241         out.println();
242         }
243     } while (editor.next());
244     }
245
246     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
247     PrintStream JavaDoc out = System.out;
248
249         if (args.length < 1) {
250             System.err.println("wrong # args: PerfRunner class [editors]");
251             System.exit(1);
252         }
253
254     try {
255         FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc(TEST_RESULTS_FILE, true);
256         out = new PrintStream JavaDoc(fout);
257     } catch (SecurityException JavaDoc e) {
258         System.err.println(TEST_RESULTS_FILE + ": permission denied: "
259                    + e.getMessage());
260     } catch (FileNotFoundException JavaDoc e) {
261         System.err.println(TEST_RESULTS_FILE + ": file not found: "
262                    + e.getMessage());
263     }
264     
265         PerfRunner tests = new PerfRunner();
266
267     // First time get JIT going...
268
tests.runTest(args[0], TEST_COUNT,
269                   REPEAT_COUNT,
270                   loadEditor(NOOP_EDITOR),
271                   null);
272
273     if (args.length == 1) {
274         // Now perform the real tests...
275
tests.runTest(args[0], TEST_COUNT,
276               REPEAT_COUNT,
277               loadEditor(NOOP_EDITOR),
278               out);
279     } else {
280         // Run test with each specified editor...
281
for (int i=1; i<args.length; i++) {
282         tests.runTest(args[0], TEST_COUNT,
283                   REPEAT_COUNT,
284                   loadEditor(args[i]),
285                   out);
286         }
287     }
288     }
289 }
290
291
292
293
294
295
296
Popular Tags