KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > PerformanceTest


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Somik Raha
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/PerformanceTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/31 16:42:33 $
10
// $Revision: 1.48 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

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 JavaDoc file;
37
38     /**
39      * Try to parse the given page the given no of times
40      * Print average time taken
41      * @param file File to be parsed
42      * @param numTimes number of times the test should be repeated
43      */

44     public PerformanceTest(String JavaDoc 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             // Create the parser object
58
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             // Create the parser object
84
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 JavaDoc[] 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 JavaDoc file = args[0];
108         String JavaDoc 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