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