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 | 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 | |
|
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 | |
|
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 | |
|
72 | |
|
73 | 0 | } |
74 | |
|
75 | |
public void onItemEnd() throws FeedParserException { |
76 | |
|
77 | |
|
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 | |
|
113 | |
|
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; |
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 | |
|