1 27 package org.htmlparser.tests; 28 29 import org.htmlparser.Parser; 30 import org.htmlparser.util.DefaultParserFeedback; 31 import org.htmlparser.util.NodeIterator; 32 import org.htmlparser.util.ParserException; 33 34 public class PerformanceTest { 35 private int numTimes; 36 private String file; 37 38 44 public PerformanceTest(String file, int numTimes) { 45 this.file = file; 46 this.numTimes = numTimes; 47 } 48 49 public void beginTestWithoutScanners() throws ParserException { 50 Parser parser; 51 long sumTimes=0; 52 double avg=0; 53 System.out.println("***************************************"); 54 System.out.println("* Test Without Scanners Registered *"); 55 System.out.println("***************************************"); 56 for (int i=0;i<=numTimes;i++) { 57 parser = new Parser(file,new DefaultParserFeedback()); 59 long start=System.currentTimeMillis(); 60 for (NodeIterator e = parser.elements();e.hasMoreNodes();) 61 e.nextNode(); 62 long elapsedTime=System.currentTimeMillis()-start; 63 if (i!=0) 64 sumTimes += elapsedTime; 65 System.out.print("Iteration "+i); 66 if (i==0) System.out.print(" (not counted)"); 67 System.out.println(" : time taken = "+elapsedTime+" ms"); 68 } 69 avg = sumTimes/(float)numTimes; 70 System.out.println("***************************************"); 71 System.out.println("Average Time : "+avg+" ms"); 72 System.out.println("***************************************"); 73 } 74 75 public void beginTestWithScanners() throws ParserException { 76 Parser parser; 77 long sumTimes=0; 78 double avg=0; 79 System.out.println("***************************************"); 80 System.out.println("* Test With Scanners Registered *"); 81 System.out.println("***************************************"); 82 for (int i=0;i<=numTimes;i++) { 83 parser = new Parser(file,new DefaultParserFeedback()); 85 long start=System.currentTimeMillis(); 86 for (NodeIterator e = parser.elements();e.hasMoreNodes();) 87 e.nextNode(); 88 long elapsedTime=System.currentTimeMillis()-start; 89 if (i!=0) 90 sumTimes += elapsedTime; 91 System.out.print("Iteration "+i); 92 if (i==0) System.out.print(" (not counted)"); 93 System.out.println(" : time taken = "+elapsedTime+" ms"); 94 } 95 avg = sumTimes/(float)numTimes; 96 System.out.println("***************************************"); 97 System.out.println("Average Time : "+avg+" ms"); 98 System.out.println("***************************************"); 99 } 100 101 public static void main(String [] args) { 102 if (args.length<2) { 103 System.err.println("Syntax Error."); 104 System.err.println("Params needed for test : <file/url to be parsed> <number of iterations>"); 105 System.exit(-1); 106 } 107 String file = args[0]; 108 String numTimesString = args[1]; 109 int numTimes = Integer.decode(numTimesString).intValue(); 110 PerformanceTest pt = new PerformanceTest(file,numTimes); 111 try { 112 pt.beginTestWithoutScanners(); 113 pt.beginTestWithScanners(); 114 } 115 catch (ParserException e) { 116 e.printStackTrace(); 117 } 118 } 119 } 120 121 | Popular Tags |