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 | 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 | |
|
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 | |
|
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 | |
|
78 | |
|
79 | 0 | } |
80 | |
|
81 | |
public void onItemEnd() throws FeedParserException { |
82 | |
|
83 | |
|
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 | |
|
119 | |
|
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; |
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 | |
|