KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > XMarkBenchmark


1
2 import com.saxonica.validate.SchemaAwareConfiguration;
3 import net.sf.saxon.event.Sink;
4 import com.saxonica.SchemaAwareTransformerFactory;
5 import net.sf.saxon.Configuration;
6 import net.sf.saxon.Version;
7 import net.sf.saxon.TransformerFactoryImpl;
8 import net.sf.saxon.om.DocumentInfo;
9 import net.sf.saxon.query.DynamicQueryContext;
10 import net.sf.saxon.query.StaticQueryContext;
11 import net.sf.saxon.query.XQueryExpression;
12
13 import javax.xml.transform.Result JavaDoc;
14 import javax.xml.transform.Source JavaDoc;
15 import javax.xml.transform.TransformerFactory JavaDoc;
16 import javax.xml.transform.Templates JavaDoc;
17 import javax.xml.transform.stream.StreamSource JavaDoc;
18 import java.io.File JavaDoc;
19 import java.io.FileReader JavaDoc;
20 import java.util.Properties JavaDoc;
21
22
23 /**
24  * Runs the XMark XQuery benchmark: see
25  * <a HREF="http://monetdb.cwi.nl/xml/index.html">http://monetdb.cwi.nl/xml/index.html</a
26  *
27  */

28 public class XMarkBenchmark {
29
30     /**
31      * Class is not instantiated, so give it a private constructor
32      */

33     private XMarkBenchmark() {
34     }
35
36     /**
37      * Method main
38      */

39     public static void main(String JavaDoc argv[]) throws Exception JavaDoc {
40
41         boolean pull = false;
42         boolean lazy = false;
43         boolean sa = false;
44         boolean xslt = false;
45         for (int i=0; i<argv.length; i++) {
46             if (argv[i].equals("-pull")) pull = true;
47             if (argv[i].equals("-lazy")) lazy = true;
48             if (argv[i].equals("-sa")) sa = true;
49             if (argv[i].equals("-xslt")) xslt = true;
50         }
51
52         if (xslt) {
53             transform(pull, lazy, sa);
54         } else {
55             query(pull, lazy, sa);
56         }
57     }
58
59     public static void query(boolean pull, boolean lazy, boolean sa) throws Exception JavaDoc {
60
61         String JavaDoc dir = "c:\\javalib\\xmark\\";
62         String JavaDoc[] tests = {"xmark1.xml", "xmark4.xml", "xmark10.xml"};
63
64         System.out.println("<xmark-results product='Saxon' lang='query' version='" + Version.getProductVersion() + "'>");
65
66         for (int f=0; f<tests.length; f++) {
67             File JavaDoc file = new File JavaDoc(dir + tests[f]);
68
69             Configuration config = (sa ? new SchemaAwareConfiguration() : new Configuration());
70             if (lazy) {
71                 config.setLazyConstructionMode(true);
72             }
73             StaticQueryContext env = new StaticQueryContext(config);
74
75             Source JavaDoc streamSource = new StreamSource JavaDoc(file);
76             long pstart = System.currentTimeMillis();
77             DocumentInfo doc = env.buildDocument(streamSource);
78             long pend = System.currentTimeMillis();
79
80             System.out.println(" <file name='" + tests[f] +
81                     "' size='" + file.length() +
82                     "' build-time='" + (pend-pstart) + "'>");
83             Properties JavaDoc props = new Properties JavaDoc();
84
85
86
87             for (int q=1; q<=20; q++) {
88                 //if (q==9) continue;
89
File JavaDoc query = new File JavaDoc(dir + 'q' + q + ".xq");
90                 StaticQueryContext qenv = new StaticQueryContext(config);
91                 XQueryExpression exp = qenv.compileQuery(new FileReader JavaDoc(query));
92                 int runs = 0;
93                 long totalTime = 0;
94                 long min = Integer.MAX_VALUE;
95                 long max = 0;
96                 for (int t=0; t<10; t++) {
97                     final DynamicQueryContext context = new DynamicQueryContext(config);
98                     context.setContextNode(doc);
99                     Result JavaDoc result = new Sink();
100                     long start, end;
101                     if (pull) {
102                         start = System.currentTimeMillis();
103                         exp.pull(context, result, props);
104                         end = System.currentTimeMillis();
105                     } else {
106                         start = System.currentTimeMillis();
107                         exp.run(context, result, props);
108                         end = System.currentTimeMillis();
109                     }
110                     runs++;
111                     long time = (end - start);
112                     if (time < min) min = time;
113                     if (time > max) max = time;
114                     totalTime += time;
115                     if (totalTime > 1000 && t>=2) break;
116                 }
117                 System.out.println(" <query q='" + q +
118                             "' avg='" + (totalTime / runs) +
119                             "' runs='" + runs +
120                             "' min='" + min +
121                             "' max='" + max + "'/>");
122             }
123             System.out.println(" </file>");
124         }
125         System.out.println("</xmark-results>");
126     }
127
128     public static void transform(boolean pull, boolean lazy, boolean sa) throws Exception JavaDoc {
129
130         String JavaDoc dir = "c:\\javalib\\xmark\\";
131         String JavaDoc[] tests = {"xmark1.xml", "xmark4.xml", "xmark10.xml"};
132
133         System.out.println("<xmark-results product='Saxon' lang='xslt' version='" + Version.getProductVersion() + "'>");
134
135         for (int f=0; f<tests.length; f++) {
136             File JavaDoc file = new File JavaDoc(dir + tests[f]);
137
138 // String factoryName = (sa ? "com.saxonica.SchemaAwareTransformerFactory" : "net.sf.saxon.TransformerFactoryImpl");
139
// System.setProperty("java.xml.transform.TransformerFactory", factoryName);
140
TransformerFactory factory;
141             if (sa) {
142                 factory = new SchemaAwareTransformerFactory();
143             } else {
144                 factory = new TransformerFactoryImpl();
145             }
146             Configuration config = ((TransformerFactoryImpl)factory).getConfiguration();
147             if (lazy) {
148                 config.setLazyConstructionMode(true);
149             }
150             StaticQueryContext env = new StaticQueryContext(config);
151
152             Source JavaDoc streamSource = new StreamSource JavaDoc(file);
153             long pstart = System.currentTimeMillis();
154             DocumentInfo doc = env.buildDocument(streamSource);
155             long pend = System.currentTimeMillis();
156
157             System.out.println(" <file name='" + tests[f] +
158                     "' size='" + file.length() +
159                     "' schema-aware='" + config.isSchemaAware(Configuration.XSLT) +
160                     "' build-time='" + (pend-pstart) + "'>");
161             Properties JavaDoc props = new Properties JavaDoc();
162
163
164
165             for (int q=1; q<=20; q++) {
166                 //if (q==9) continue;
167
File JavaDoc sheet = new File JavaDoc(dir + 'q' + q + ".xsl");
168                 if (!sheet.exists()) {
169                     continue;
170                 }
171                 Templates JavaDoc templates = factory.newTemplates(new StreamSource JavaDoc(sheet));
172
173                 int runs = 0;
174                 long totalTime = 0;
175                 long min = Integer.MAX_VALUE;
176                 long max = 0;
177                 for (int t=0; t<10; t++) {
178                     Result JavaDoc result = new Sink();
179                     long start, end;
180
181                     start = System.currentTimeMillis();
182                     templates.newTransformer().transform(doc, result);
183                     end = System.currentTimeMillis();
184
185                     runs++;
186                     long time = (end - start);
187                     if (time < min) min = time;
188                     if (time > max) max = time;
189                     totalTime += time;
190                     if (totalTime > 1000 && t>=2) break;
191                 }
192                 System.out.println(" <query q='" + q +
193                             "' avg='" + (totalTime / runs) +
194                             "' runs='" + runs +
195                             "' min='" + min +
196                             "' max='" + max + "'/>");
197             }
198             System.out.println(" </file>");
199         }
200         System.out.println("</xmark-results>");
201     }
202
203
204 }
205
206 //
207
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
208
// you may not use this file except in compliance with the License. You may obtain a copy of the
209
// License at http://www.mozilla.org/MPL/
210
//
211
// Software distributed under the License is distributed on an "AS IS" basis,
212
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
213
// See the License for the specific language governing rights and limitations under the License.
214
//
215
// The Original Code is: all this file.
216
//
217
// The Initial Developer of the Original Code is Michael H. Kay.
218
//
219
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
220
//
221
// Contributor(s): none.
222
//
223
Popular Tags