1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36
37
38 public class DebugPerformance {
39
40 static SAXParser parser = null;
41
42 public static void doTestSAX() throws Exception {
43
44 if ( parser == null ) {
45 parser = SAXParserFactory.newInstance().newSAXParser();
46
47
48 parser.getXMLReader().setFeature( "http://xml.org/sax/features/namespaces", true );
49 }
50
51 org.apache.commons.feedparser.sax.RSSFeedParser handler =
52 new org.apache.commons.feedparser.sax.RSSFeedParser();
53
54 handler.listener = new DefaultFeedParserListener() {
55
56 public void onChannel( FeedParserState state,
57 String title,
58 String link,
59 String description ) throws FeedParserException {
60
61
62
63 }
64
65 public void onItem( FeedParserState state,
66 String title,
67 String link,
68 String description,
69 String permalink ) throws FeedParserException {
70
71
72
73 }
74
75 public void onItemEnd() throws FeedParserException {
76
77
78
79 }
80
81 };
82
83 String resource = "file:/home/burton/index.rss";
84
85 ResourceRequest request = ResourceRequestFactory
86 .getResourceRequest( resource );
87
88 parser.parse( request.getInputStream(), handler );
89
90 }
91
92 public static void doTestDefault() throws Exception {
93
94 FeedParser parser = FeedParserFactory.newFeedParser();
95 FeedParserListener listener = new DefaultFeedParserListener() {};
96
97 String resource = "file:/home/burton/index.rss";
98
99 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
113
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;
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