1 27 package org.htmlparser.tests; 28 29 import java.util.Enumeration ; 30 import java.util.Vector ; 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 FORM_HTML = 41 "<FORM METHOD=\""+FormTag.POST+"\" ACTION=\"do_login.php\" NAME=\"login_form\" onSubmit=\"return CheckData()\">\n"+ 42 "<TR><TD ALIGN=\"center\"> </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\"> </TD></TR>\n"+ 48 "<TR><TD ALIGN=\"center\"><INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Login\"></TD></TR>\n"+ 49 "<TR><TD ALIGN=\"center\"> </TD></TR>\n"+ 50 "<TEXTAREA name=\"Description\" rows=\"15\" cols=\"55\" wrap=\"virtual\" class=\"composef\" tabindex=\"5\">Contents of TextArea</TEXTAREA>\n"+ 51 "<INPUT TYPE=\"hidden\" NAME=\"password\" SIZE=\"20\">\n"+ 53 "<INPUT TYPE=\"submit\">\n"+ 54 "</FORM>"; 55 56 FormTag formTag; 57 Vector formChildren; 58 public void setUp() throws Exception { 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 (); 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 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 [] args) throws Exception { 117 InstanceofPerformanceTest test = 118 new InstanceofPerformanceTest(); 119 test.setUp(); 120 test.perform(); 121 } 122 } 123 | Popular Tags |