Coverage Report - org.apache.commons.feedparser.test.TestPerformance
 
Classes in this File Line Coverage Branch Coverage Complexity
TestPerformance
0%
0/40
0%
0/10
1.375
TestPerformance$1
0%
0/4
N/A
1.375
TestPerformance$2
0%
0/1
N/A
1.375
 
 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  0
         super( name );
 44  0
     }
 45  
 
 46  0
     static SAXParser parser = null;
 47  
 
 48  
     public static void testSAX() throws Exception {
 49  
 
 50  0
         if ( parser == null ) {
 51  0
             parser = SAXParserFactory.newInstance().newSAXParser();
 52  
 
 53  
             //need to enable SAX2 locals and namespace
 54  0
             parser.getXMLReader().setFeature( "http://xml.org/sax/features/namespaces", true );
 55  
         }
 56  
 
 57  0
          org.apache.commons.feedparser.sax.RSSFeedParser handler =
 58  
               new org.apache.commons.feedparser.sax.RSSFeedParser();
 59  
 
 60  0
          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  0
                  }
 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  0
                  }
 80  
 
 81  
                  public void onItemEnd() throws FeedParserException {
 82  
 
 83  
 //                     System.out.println( "onItemEnd");
 84  
 
 85  0
                  }
 86  
 
 87  
              };
 88  
 
 89  0
         String resource = "file:/home/burton/index.rss";
 90  
         
 91  0
         ResourceRequest request = ResourceRequestFactory
 92  
             .getResourceRequest( resource );
 93  
 
 94  0
         parser.parse( request.getInputStream(), handler );
 95  
 
 96  0
     }
 97  
 
 98  
     public static void testDefault() throws Exception {
 99  
 
 100  0
         FeedParser parser = FeedParserFactory.newFeedParser();
 101  0
         FeedParserListener listener = new DefaultFeedParserListener() {};
 102  
 
 103  0
         String resource = "file:/home/burton/index.rss";
 104  
         
 105  0
         ResourceRequest request = ResourceRequestFactory
 106  
             .getResourceRequest( resource );
 107  
         
 108  0
         parser.parse( listener,
 109  
                       request.getInputStream(),
 110  
                       resource );
 111  
 
 112  0
     }
 113  
 
 114  
     public static void main( String[] args ) throws Exception {
 115  
 
 116  0
         TestPerformance test = new TestPerformance( null );
 117  
         
 118  
         //test.testGetWeblogLinkForResource();
 119  
         //test.test1();
 120  
 
 121  0
         doTestMethod( "testSAX", TestPerformance.class, 100 );
 122  0
         doTestMethod( "testDefault", TestPerformance.class, 100 );
 123  
         
 124  0
     }
 125  
 
 126  
     public static void  doTestMethod( String name, Class clazz, int max ) throws Exception {
 127  
 
 128  0
         Method method = clazz.getMethod( name, null );
 129  
 
 130  0
         System.out.println( "Testing method: " + name );
 131  
 
 132  0
         long duration = 0;
 133  
         
 134  0
         for ( int i = 0; i <= max; ++i ) {
 135  
 
 136  0
             long before = System.currentTimeMillis();
 137  
             
 138  0
             method.invoke( null, null );
 139  
 
 140  0
             if ( i == 0 )
 141  0
                 continue; //don't measure the first call
 142  
 
 143  0
             long after = System.currentTimeMillis();
 144  0
             duration += after-before;
 145  
             
 146  
         }
 147  
 
 148  0
         System.out.println( "----------------" );
 149  0
         System.out.println( "Total parse count: " + max );
 150  
 
 151  0
         System.out.println( "Total duration: " + duration + "  milliseconds" );
 152  
 
 153  0
         float totalAvgDuration = (float)duration / (float)max;
 154  
 
 155  0
         System.out.println( "Total avg duration: " + totalAvgDuration + "  milliseconds" );
 156  
 
 157  0
         float totalPerSecond = 1000 / totalAvgDuration;
 158  
 
 159  0
         System.out.println( "Total per second: " + totalPerSecond );
 160  
 
 161  0
     }
 162  
 
 163  
 }
 164