1 9 10 package org.dom4j.samples.performance; 11 12 import java.io.BufferedReader ; 13 import java.io.FileReader ; 14 import java.io.StringReader ; 15 16 import org.dom4j.Document; 17 import org.dom4j.io.SAXReader; 18 19 25 public class ParseLoop { 26 27 private static int bufferSize = 128 * 1024; 28 29 public static void main(String [] args) throws Exception { 30 if (args.length <= 0) { 31 System.out.println("arguments: <XML file> [<loopCount>]"); 32 return; 33 } 34 String xmlFile = args[0]; 35 int loops = 40; 36 if (args.length > 1) { 37 loops = Integer.parseInt(args[1]); 38 } 39 40 StringBuffer buffer = new StringBuffer (64 * 1024); 41 BufferedReader reader = new BufferedReader (new FileReader (xmlFile)); 42 while (true) { 43 String text = reader.readLine(); 44 if (text == null) { 45 break; 46 } 47 buffer.append(text); 48 buffer.append("\n"); 49 } 50 long start = System.currentTimeMillis(); 51 52 parse(buffer.toString(), loops); 53 54 long elapsed = System.currentTimeMillis() - start; 55 56 System.out.println("Parsed: " + xmlFile + " " + loops + " times in: " 57 + elapsed + " (ms)"); 58 } 59 60 protected static void parse(String text, int loops) throws Exception { 61 SAXReader xmlReader = new SAXReader(); 62 for (int i = 0; i < loops; i++) { 63 Document document = xmlReader.read(new StringReader (text)); 64 } 65 } 66 } 67 68 106 | Popular Tags |