KickJava   Java API By Example, From Geeks To Geeks.

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


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/InstanceofPerformanceTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/31 16:42:33 $
10
// $Revision: 1.22 $
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 java.util.Enumeration JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import org.htmlparser.Node;
33 import org.htmlparser.Parser;
34 import org.htmlparser.tags.FormTag;
35 import org.htmlparser.util.NodeIterator;
36 import org.htmlparser.util.SimpleNodeIterator;
37
38 public class InstanceofPerformanceTest {
39
40     public static final String JavaDoc FORM_HTML =
41     "<FORM METHOD=\""+FormTag.POST+"\" ACTION=\"do_login.php\" NAME=\"login_form\" onSubmit=\"return CheckData()\">\n"+
42         "<TR><TD ALIGN=\"center\">&nbsp;</TD></TR>\n"+
43         "<TR><TD ALIGN=\"center\"><FONT face=\"Arial, verdana\" size=2><b>User Name</b></font></TD></TR>\n"+
44         "<TR><TD ALIGN=\"center\"><INPUT TYPE=\"text\" NAME=\"name\" SIZE=\"20\"></TD></TR>\n"+
45         "<TR><TD ALIGN=\"center\"><FONT face=\"Arial, verdana\" size=2><b>Password</b></font></TD></TR>\n"+
46         "<TR><TD ALIGN=\"center\"><INPUT TYPE=\"password\" NAME=\"passwd\" SIZE=\"20\"></TD></TR>\n"+
47         "<TR><TD ALIGN=\"center\">&nbsp;</TD></TR>\n"+
48         "<TR><TD ALIGN=\"center\"><INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Login\"></TD></TR>\n"+
49         "<TR><TD ALIGN=\"center\">&nbsp;</TD></TR>\n"+
50         "<TEXTAREA name=\"Description\" rows=\"15\" cols=\"55\" wrap=\"virtual\" class=\"composef\" tabindex=\"5\">Contents of TextArea</TEXTAREA>\n"+
51 // "<TEXTAREA name=\"AnotherDescription\" rows=\"15\" cols=\"55\" wrap=\"virtual\" class=\"composef\" tabindex=\"5\">\n"+
52
"<INPUT TYPE=\"hidden\" NAME=\"password\" SIZE=\"20\">\n"+
53         "<INPUT TYPE=\"submit\">\n"+
54         "</FORM>";
55
56     FormTag formTag;
57     Vector JavaDoc formChildren;
58     public void setUp() throws Exception JavaDoc {
59         Parser parser =
60             Parser.createParser(
61                 FORM_HTML,
62                 null
63             );
64         NodeIterator e = parser.elements();
65         Node node = e.nextNode();
66         formTag = (FormTag)node;
67         formChildren = new Vector JavaDoc();
68         for (SimpleNodeIterator se = formTag.children();se.hasMoreNodes();) {
69             formChildren.addElement(se.nextNode());
70         }
71     }
72
73     public void doInstanceofTest(long [] time,int index, long numTimes) {
74         System.out.println("doInstanceofTest("+index+")");
75         long start = System.currentTimeMillis();
76         for (long i=0;i<numTimes;i++) {
77             for (Enumeration JavaDoc e = formChildren.elements();e.hasMoreElements();) {
78                 e.nextElement();
79             }
80         }
81         long end = System.currentTimeMillis();
82         time[index] = end-start;
83     }
84
85     public void doGetTypeTest(long [] time,int index, long numTimes) {
86         System.out.println("doGetTypeTest("+index+")");
87         long start = System.currentTimeMillis();
88         for (long i=0;i<numTimes;i++) {
89             for (SimpleNodeIterator e = formTag.children();e.hasMoreNodes();) {
90                 e.nextNode();
91             }
92         }
93         long end = System.currentTimeMillis();
94         time[index] = end-start;
95     }
96
97     public void perform() {
98         int numTimes = 30;
99         long time1[] = new long[numTimes],
100         time2[] = new long[numTimes];
101
102         for (int i=0;i<numTimes;i++)
103             doInstanceofTest(time1,i,i*10000);
104
105         for (int i=0;i<numTimes;i++)
106             doGetTypeTest(time2,i,i*10000);
107
108         print(time1,time2);
109     }
110
111     public void print(long [] time1, long [] time2) {
112         for (int i=0;i<time1.length;i++) {
113             System.out.println(i*1000000+":"+","+time1[i]+" "+time2[i]);
114         }
115     }
116     public static void main(String JavaDoc [] args) throws Exception JavaDoc {
117         InstanceofPerformanceTest test =
118             new InstanceofPerformanceTest();
119         test.setUp();
120         test.perform();
121     }
122 }
123
Popular Tags