001    /*
002     * Copyright 1999,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.apache.commons.feedparser.test;
018    
019    import java.lang.reflect.Method;
020    
021    import javax.xml.parsers.SAXParser;
022    import javax.xml.parsers.SAXParserFactory;
023    
024    import junit.framework.TestCase;
025    
026    import org.apache.commons.feedparser.DefaultFeedParserListener;
027    import org.apache.commons.feedparser.FeedParser;
028    import org.apache.commons.feedparser.FeedParserException;
029    import org.apache.commons.feedparser.FeedParserFactory;
030    import org.apache.commons.feedparser.FeedParserListener;
031    import org.apache.commons.feedparser.FeedParserState;
032    import org.apache.commons.feedparser.network.ResourceRequest;
033    import org.apache.commons.feedparser.network.ResourceRequestFactory;
034    
035    /**
036     *
037     * @author <a href="mailto:burton@peerfear.org">Kevin A. Burton</a>
038     * @version $Id: TestPerformance.java 373622 2006-01-30 22:53:00Z mvdb $
039     */
040    public class TestPerformance extends TestCase {
041    
042        public TestPerformance( String name ) {
043            super( name );
044        }
045    
046        static SAXParser parser = null;
047    
048        public static void testSAX() throws Exception {
049    
050            if ( parser == null ) {
051                parser = SAXParserFactory.newInstance().newSAXParser();
052    
053                //need to enable SAX2 locals and namespace
054                parser.getXMLReader().setFeature( "http://xml.org/sax/features/namespaces", true );
055            }
056    
057             org.apache.commons.feedparser.sax.RSSFeedParser handler =
058                  new org.apache.commons.feedparser.sax.RSSFeedParser();
059    
060             handler.listener = new DefaultFeedParserListener() {
061    
062                     public void onChannel( FeedParserState state,
063                                            String title,
064                                            String link,
065                                            String description ) throws FeedParserException {
066    
067    //                      System.out.println( "onChannel: title: " + title );
068                        
069                     }
070    
071                     public void onItem( FeedParserState state,
072                                         String title,
073                                         String link,
074                                         String description,
075                                         String permalink ) throws FeedParserException {
076    
077    //                     System.out.println( "onItem: title: " + title );
078                        
079                     }
080    
081                     public void onItemEnd() throws FeedParserException {
082    
083    //                     System.out.println( "onItemEnd");
084    
085                     }
086    
087                 };
088    
089            String resource = "file:/home/burton/index.rss";
090            
091            ResourceRequest request = ResourceRequestFactory
092                .getResourceRequest( resource );
093    
094            parser.parse( request.getInputStream(), handler );
095    
096        }
097    
098        public static void testDefault() throws Exception {
099    
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