View Javadoc

1   /*
2    * Copyright 1999,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.feedparser.test;
18  
19  import java.lang.reflect.Method;
20  
21  import javax.xml.parsers.SAXParser;
22  import javax.xml.parsers.SAXParserFactory;
23  
24  import org.apache.commons.feedparser.DefaultFeedParserListener;
25  import org.apache.commons.feedparser.FeedParser;
26  import org.apache.commons.feedparser.FeedParserException;
27  import org.apache.commons.feedparser.FeedParserFactory;
28  import org.apache.commons.feedparser.FeedParserListener;
29  import org.apache.commons.feedparser.FeedParserState;
30  import org.apache.commons.feedparser.network.ResourceRequest;
31  import org.apache.commons.feedparser.network.ResourceRequestFactory;
32  
33  /**
34   *
35   * @author <a href="mailto:burton@peerfear.org">Kevin A. Burton</a>
36   * @version $Id: DebugPerformance.java 373622 2006-01-30 22:53:00Z mvdb $
37   */
38  public class DebugPerformance {
39  
40      static SAXParser parser = null;
41  
42      public static void doTestSAX() throws Exception {
43  
44          if ( parser == null ) {
45              parser = SAXParserFactory.newInstance().newSAXParser();
46  
47              //need to enable SAX2 locals and namespace
48              parser.getXMLReader().setFeature( "http://xml.org/sax/features/namespaces", true );
49          }
50  
51           org.apache.commons.feedparser.sax.RSSFeedParser handler =
52                new org.apache.commons.feedparser.sax.RSSFeedParser();
53  
54           handler.listener = new DefaultFeedParserListener() {
55  
56                   public void onChannel( FeedParserState state,
57                                          String title,
58                                          String link,
59                                          String description ) throws FeedParserException {
60  
61  //                      System.out.println( "onChannel: title: " + title );
62                      
63                   }
64  
65                   public void onItem( FeedParserState state,
66                                       String title,
67                                       String link,
68                                       String description,
69                                       String permalink ) throws FeedParserException {
70  
71  //                     System.out.println( "onItem: title: " + title );
72                      
73                   }
74  
75                   public void onItemEnd() throws FeedParserException {
76  
77  //                     System.out.println( "onItemEnd");
78  
79                   }
80  
81               };
82  
83          String resource = "file:/home/burton/index.rss";
84          
85          ResourceRequest request = ResourceRequestFactory
86              .getResourceRequest( resource );
87  
88          parser.parse( request.getInputStream(), handler );
89  
90      }
91  
92      public static void doTestDefault() throws Exception {
93  
94          FeedParser parser = FeedParserFactory.newFeedParser();
95          FeedParserListener listener = new DefaultFeedParserListener() {};
96  
97          String resource = "file:/home/burton/index.rss";
98          
99          ResourceRequest request = ResourceRequestFactory
100             .getResourceRequest( resource );
101         
102         parser.parse( listener,
103                       request.getInputStream(),
104                       resource );
105 
106     }
107 
108     public static void main( String[] args ) throws Exception {
109 
110         DebugPerformance test = new DebugPerformance();
111         
112         //test.testGetWeblogLinkForResource();
113         //test.test1();
114 
115         doTestMethod( "doTestSAX", DebugPerformance.class, 100 );
116         doTestMethod( "doTestDefault", DebugPerformance.class, 100 );
117         
118     }
119 
120     public static void  doTestMethod( String name, Class clazz, int max ) throws Exception {
121 
122         Method method = clazz.getMethod( name, null );
123 
124         System.out.println( "Testing method: " + name );
125 
126         long duration = 0;
127         
128         for ( int i = 0; i <= max; ++i ) {
129 
130             long before = System.currentTimeMillis();
131             
132             method.invoke( null, null );
133 
134             if ( i == 0 )
135                 continue; //don't measure the first call
136 
137             long after = System.currentTimeMillis();
138             duration += after-before;
139             
140         }
141 
142         System.out.println( "----------------" );
143         System.out.println( "Total parse count: " + max );
144 
145         System.out.println( "Total duration: " + duration + "  milliseconds" );
146 
147         float totalAvgDuration = (float)duration / (float)max;
148 
149         System.out.println( "Total avg duration: " + totalAvgDuration + "  milliseconds" );
150 
151         float totalPerSecond = 1000 / totalAvgDuration;
152 
153         System.out.println( "Total per second: " + totalPerSecond );
154 
155     }
156 
157 }
158