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 ; 14 import javax.xml.transform.Source ; 15 import javax.xml.transform.TransformerFactory ; 16 import javax.xml.transform.Templates ; 17 import javax.xml.transform.stream.StreamSource ; 18 import java.io.File ; 19 import java.io.FileReader ; 20 import java.util.Properties ; 21 22 23 28 public class XMarkBenchmark { 29 30 33 private XMarkBenchmark() { 34 } 35 36 39 public static void main(String argv[]) throws Exception { 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 { 60 61 String dir = "c:\\javalib\\xmark\\"; 62 String [] 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 file = new File (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 streamSource = new StreamSource (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 props = new Properties (); 84 85 86 87 for (int q=1; q<=20; q++) { 88 File query = new File (dir + 'q' + q + ".xq"); 90 StaticQueryContext qenv = new StaticQueryContext(config); 91 XQueryExpression exp = qenv.compileQuery(new FileReader (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 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 { 129 130 String dir = "c:\\javalib\\xmark\\"; 131 String [] 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 file = new File (dir + tests[f]); 137 138 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 streamSource = new StreamSource (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 props = new Properties (); 162 163 164 165 for (int q=1; q<=20; q++) { 166 File sheet = new File (dir + 'q' + q + ".xsl"); 168 if (!sheet.exists()) { 169 continue; 170 } 171 Templates templates = factory.newTemplates(new StreamSource (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 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 | Popular Tags |