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 junit.framework.TestCase;
25  
26  import org.apache.commons.feedparser.DefaultFeedParserListener;
27  import org.apache.commons.feedparser.FeedParser;
28  import org.apache.commons.feedparser.FeedParserException;
29  import org.apache.commons.feedparser.FeedParserFactory;
30  import org.apache.commons.feedparser.FeedParserListener;
31  import org.apache.commons.feedparser.FeedParserState;
32  import org.apache.commons.feedparser.network.ResourceRequest;
33  import org.apache.commons.feedparser.network.ResourceRequestFactory;
34  
35  /**
36   *
37   * @author <a href="mailto:burton@peerfear.org">Kevin A. Burton</a>
38   * @version $Id: TestPerformance.java 373622 2006-01-30 22:53:00Z mvdb $
39   */
40  public class TestPerformance extends TestCase {
41  
42      public TestPerformance( String name ) {
43          super( name );
44      }
45  
46      static SAXParser parser = null;
47  
48      public static void testSAX() throws Exception {
49  
50          if ( parser == null ) {
51              parser = SAXParserFactory.newInstance().newSAXParser();
52  
53              //need to enable SAX2 locals and namespace
54              parser.getXMLReader().setFeature( "http://xml.org/sax/features/namespaces", true );
55          }
56  
57           org.apache.commons.feedparser.sax.RSSFeedParser handler =
58                new org.apache.commons.feedparser.sax.RSSFeedParser();
59  
60           handler.listener = new DefaultFeedParserListener() {
61  
62                   public void onChannel( FeedParserState state,
63                                          String title,
64                                          String link,
65                                          String description ) throws FeedParserException {
66  
67  //                      System.out.println( "onChannel: title: " + title );
68                      
69                   }
70  
71                   public void onItem( FeedParserState state,
72                                       String title,
73                                       String link,
74                                       String description,
75                                       String permalink ) throws FeedParserException {
76  
77  //                     System.out.println( "onItem: title: " + title );
78                      
79                   }
80  
81                   public void onItemEnd() throws FeedParserException {
82  
83  //                     System.out.println( "onItemEnd");
84  
85                   }
86  
87               };
88  
89          String resource = "file:/home/burton/index.rss";
90          
91          ResourceRequest request = ResourceRequestFactory
92              .getResourceRequest( resource );
93  
94          parser.parse( request.getInputStream(), handler );
95  
96      }
97  
98      public static void testDefault() throws Exception {
99  
100         FeedParser parser = FeedParserFactory.newFeedParser();
101         FeedParserListener listener = new DefaultFeedParserListener() {};
102 
103         String resource = "file:/home/burton/index.rss";
104         
105         ResourceRequest request = ResourceRequestFactory
106             .getResourceRequest( resource );
107         
108         parser.parse( listener,
109                       request.getInputStream(),
110                       resource );
111 
112     }
113 
114     public static void main( String[] args ) throws Exception {
115 
116         TestPerformance test = new TestPerformance( null );
117         
118         //test.testGetWeblogLinkForResource();
119         //test.test1();
120 
121         doTestMethod( "testSAX", TestPerformance.class, 100 );
122         doTestMethod( "testDefault", TestPerformance.class, 100 );
123         
124     }
125 
126     public static void  doTestMethod( String name, Class clazz, int max ) throws Exception {
127 
128         Method method = clazz.getMethod( name, null );
129 
130         System.out.println( "Testing method: " + name );
131 
132         long duration = 0;
133         
134         for ( int i = 0; i <= max; ++i ) {
135 
136             long before = System.currentTimeMillis();
137             
138             method.invoke( null, null );
139 
140             if ( i == 0 )
141                 continue; //don't measure the first call
142 
143             long after = System.currentTimeMillis();
144             duration += after-before;
145             
146         }
147 
148         System.out.println( "----------------" );
149         System.out.println( "Total parse count: " + max );
150 
151         System.out.println( "Total duration: " + duration + "  milliseconds" );
152 
153         float totalAvgDuration = (float)duration / (float)max;
154 
155         System.out.println( "Total avg duration: " + totalAvgDuration + "  milliseconds" );
156 
157         float totalPerSecond = 1000 / totalAvgDuration;
158 
159         System.out.println( "Total per second: " + totalPerSecond );
160 
161     }
162 
163 }
164