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