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 org.apache.commons.feedparser.DefaultFeedParserListener;
025 import org.apache.commons.feedparser.FeedParser;
026 import org.apache.commons.feedparser.FeedParserException;
027 import org.apache.commons.feedparser.FeedParserFactory;
028 import org.apache.commons.feedparser.FeedParserListener;
029 import org.apache.commons.feedparser.FeedParserState;
030 import org.apache.commons.feedparser.network.ResourceRequest;
031 import org.apache.commons.feedparser.network.ResourceRequestFactory;
032
033 /**
034 *
035 * @author <a href="mailto:burton@peerfear.org">Kevin A. Burton</a>
036 * @version $Id: DebugPerformance.java 373622 2006-01-30 22:53:00Z mvdb $
037 */
038 public class DebugPerformance {
039
040 static SAXParser parser = null;
041
042 public static void doTestSAX() throws Exception {
043
044 if ( parser == null ) {
045 parser = SAXParserFactory.newInstance().newSAXParser();
046
047 //need to enable SAX2 locals and namespace
048 parser.getXMLReader().setFeature( "http://xml.org/sax/features/namespaces", true );
049 }
050
051 org.apache.commons.feedparser.sax.RSSFeedParser handler =
052 new org.apache.commons.feedparser.sax.RSSFeedParser();
053
054 handler.listener = new DefaultFeedParserListener() {
055
056 public void onChannel( FeedParserState state,
057 String title,
058 String link,
059 String description ) throws FeedParserException {
060
061 // System.out.println( "onChannel: title: " + title );
062
063 }
064
065 public void onItem( FeedParserState state,
066 String title,
067 String link,
068 String description,
069 String permalink ) throws FeedParserException {
070
071 // System.out.println( "onItem: title: " + title );
072
073 }
074
075 public void onItemEnd() throws FeedParserException {
076
077 // System.out.println( "onItemEnd");
078
079 }
080
081 };
082
083 String resource = "file:/home/burton/index.rss";
084
085 ResourceRequest request = ResourceRequestFactory
086 .getResourceRequest( resource );
087
088 parser.parse( request.getInputStream(), handler );
089
090 }
091
092 public static void doTestDefault() throws Exception {
093
094 FeedParser parser = FeedParserFactory.newFeedParser();
095 FeedParserListener listener = new DefaultFeedParserListener() {};
096
097 String resource = "file:/home/burton/index.rss";
098
099 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