author | robin |
Fri, 23 Dec 2016 12:55:22 +0000 | |
changeset 56 | 51219ad2b0bd |
parent 53 | 3a3f172a62bf |
permissions | -rw-r--r-- |
1 | 1 |
# benchmark |
2 |
# MSXML: This can be downloaded from many places. You need 3.0 |
|
3 |
# which is NOT in most newly installed Windows boxes. (650kb) |
|
4 |
# http://download.microsoft.com/download/xml/Install/3.0/WIN98Me/EN-US/msxml3.exe |
|
5 |
# for a quick tutorial on MSXML 3.0, see |
|
6 |
# http://www.perfectxml.com/articles/xml/msxml30.asp |
|
7 |
||
8 |
# you should then run the COM MakePY utility on the Pythonwin menu. |
|
9 |
# to get it going as fast as possible. |
|
10 |
||
11 |
||
12 |
import sys |
|
13 |
import glob |
|
14 |
import time |
|
15 |
import string |
|
16 |
from types import TupleType |
|
17 |
import cStringIO |
|
18 |
||
19 |
def tupleTreeStats(node): |
|
20 |
# counts tags and attributes recursively |
|
21 |
# use for all reportlab parsers |
|
22 |
if node[1] is None: |
|
23 |
attrCount = 0 |
|
24 |
else: |
|
25 |
attrCount = len(node[1]) |
|
26 |
nodeCount = 1 |
|
27 |
if node[2] is not None: |
|
28 |
for child in node[2]: |
|
29 |
if type(child) is TupleType: |
|
30 |
a, n = tupleTreeStats(child) |
|
31 |
attrCount = attrCount + a |
|
32 |
nodeCount = nodeCount + n |
|
33 |
return attrCount, nodeCount |
|
34 |
||
35 |
### pyRXP - our wrapper around Univ of Edinburgh |
|
36 |
||
37 |
def getPyRXPParser(): |
|
53
3a3f172a62bf
benchmarks.py: import pyRXPU rather than pyRXP
Matthew Pitkin <pitkin@gmail.com>
parents:
11
diff
changeset
|
38 |
import pyRXPU |
3a3f172a62bf
benchmarks.py: import pyRXPU rather than pyRXP
Matthew Pitkin <pitkin@gmail.com>
parents:
11
diff
changeset
|
39 |
p = pyRXPU.Parser() |
1 | 40 |
return p |
41 |
||
42 |
def getNonValidatingPyRXPParser(): |
|
53
3a3f172a62bf
benchmarks.py: import pyRXPU rather than pyRXP
Matthew Pitkin <pitkin@gmail.com>
parents:
11
diff
changeset
|
43 |
import pyRXPU |
3a3f172a62bf
benchmarks.py: import pyRXPU rather than pyRXP
Matthew Pitkin <pitkin@gmail.com>
parents:
11
diff
changeset
|
44 |
p = pyRXPU.Parser(Validate=0) |
1 | 45 |
return p |
46 |
||
47 |
def parseWithPyRXP(parser, rawdata): |
|
48 |
return parser.parse(rawdata) |
|
49 |
||
50 |
### rparsexml - Aaron's very fast pure python parser |
|
51 |
||
52 |
def loadRparseXML(): |
|
53 |
#it's a module, what the heck |
|
54 |
from reportlab.lib import rparsexml |
|
55 |
return rparsexml |
|
56 |
||
57 |
def parseWithRParseXML(rparsexml, rawdata): |
|
58 |
#first argument is a dummy holding none |
|
59 |
return rparsexml.parsexml0(rawdata)[0] |
|
60 |
||
61 |
### expattree - tree-building wrapper around pyexpat |
|
62 |
def getExpatParser(): |
|
63 |
import expattree |
|
64 |
return expattree.ExpatTreeParser() |
|
65 |
||
66 |
def parseWithExpat(expatParser, rawdata): |
|
67 |
#first argument is a dummy holding none |
|
68 |
return expatParser.parse(rawdata) |
|
69 |
||
70 |
####### minidom - non-validating DOM parser in the Python distro |
|
71 |
||
72 |
def loadMiniDOM(): |
|
73 |
import xml.dom.minidom |
|
74 |
return xml.dom.minidom |
|
75 |
||
76 |
def parseWithMiniDOM(dom_module, rawdata): |
|
77 |
#parser is None |
|
78 |
return dom_module.parseString(rawdata) |
|
79 |
||
80 |
def statsWithMiniDOM(node): |
|
81 |
return (1, 0) |
|
82 |
||
83 |
######### Microsoft XML Parser via COM ###################### |
|
84 |
||
85 |
||
86 |
def loadMSXML30(): |
|
87 |
from win32com.client import Dispatch |
|
88 |
msx = Dispatch('Microsoft.XMLDOM') |
|
89 |
return msx |
|
90 |
||
91 |
def parseWithMSXML30(msx, rawdata): |
|
92 |
msx.loadXML(rawdata) |
|
93 |
return msx |
|
94 |
||
95 |
def statsWithMSXML30(node): |
|
96 |
#not done |
|
97 |
return (1,0) |
|
98 |
||
99 |
###########4DOM ############### |
|
100 |
def load4DOM(): |
|
101 |
from xml.dom.ext.reader import PyExpat |
|
102 |
from xml.dom import Node |
|
103 |
reader = PyExpat.Reader() |
|
104 |
return reader |
|
105 |
||
106 |
def parseWith4DOM(reader, rawdata): |
|
107 |
return reader.fromString(rawdata) |
|
108 |
||
109 |
||
110 |
def statsWith4DOM(node): |
|
111 |
#node |
|
112 |
return (1,0) |
|
113 |
||
114 |
def loadCDomlette(): |
|
115 |
from Ft.Lib import cDomlettec |
|
116 |
return cDomlettec |
|
117 |
||
118 |
def parseWithCDomlette(modul, rawdata): |
|
119 |
io = cStringIO.StringIO(rawdata) |
|
120 |
return modul.parse(io, '') |
|
121 |
||
122 |
def statsWithCDomlette(node): |
|
123 |
#node |
|
124 |
return (1,0) |
|
10 | 125 |
|
1 | 126 |
##########put them all together################ |
127 |
||
128 |
TESTMAP = [ |
|
129 |
# name of parser; function to initialize if needed; |
|
130 |
# function to parse; function to do stats |
|
131 |
('pyRXP', getPyRXPParser, parseWithPyRXP, tupleTreeStats), |
|
132 |
('pyRXP_nonvalidating', getNonValidatingPyRXPParser, parseWithPyRXP, tupleTreeStats), |
|
133 |
('rparsexml', loadRparseXML, parseWithRParseXML, tupleTreeStats), |
|
134 |
('expat', getExpatParser, parseWithExpat, tupleTreeStats), |
|
135 |
('minidom', loadMiniDOM, parseWithMiniDOM, statsWithMiniDOM), |
|
136 |
('msxml30', loadMSXML30, parseWithMSXML30, statsWithMSXML30), |
|
137 |
('4dom', load4DOM, parseWith4DOM, statsWith4DOM), |
|
138 |
('cdomlette', loadCDomlette, parseWithCDomlette, statsWithCDomlette) |
|
139 |
] |
|
140 |
||
141 |
def interact(testName=None, dtd=1, pause='unknown'): |
|
142 |
||
143 |
# if no DTD requested, trim off first 2 lines; the lack of |
|
144 |
# a DTD reference will put validating parsers into non- |
|
145 |
# validating mode |
|
146 |
if dtd: |
|
147 |
sampleText = open('rml_a.xml').read() |
|
148 |
else: |
|
149 |
print 'DTD declaration removed, non-validating' |
|
150 |
lines = open('rml_a.xml').readlines()[2:] |
|
151 |
sampleText = string.join(lines,'') |
|
152 |
||
153 |
if testName: |
|
154 |
found = 0 |
|
155 |
for row in TESTMAP: |
|
156 |
if row[0] == testName: |
|
157 |
found = 1 |
|
158 |
(name, loadFunc, parseFunc, statFunc) = row |
|
159 |
break |
|
160 |
if not found: |
|
161 |
print 'parser %s not found, please select' % testName |
|
162 |
||
163 |
if not testName: |
|
164 |
# interactive, show stuff |
|
165 |
print "Interactive benchmark suite for Python XML tree-parsers." |
|
166 |
print 'Using sample XML file %d bytes long' % len(sampleText) |
|
167 |
print "Parsers available:" |
|
168 |
i = 1 |
|
169 |
for (name, a, b, c) in TESTMAP: |
|
170 |
print '\t%d. %s' % (i, name) |
|
171 |
i = i + 1 |
|
172 |
||
173 |
inp = raw_input('Parser number (or x to exit) > ') |
|
174 |
if inp == 'x': |
|
175 |
print 'bye' |
|
176 |
return |
|
177 |
else: |
|
178 |
num = int(inp) |
|
179 |
(name, loadFunc, parseFunc, statFunc) = TESTMAP[num-1] |
|
180 |
||
181 |
# force pause to 1 or 0 by asking |
|
182 |
if pause == 'unknown': |
|
183 |
inp = raw_input("Shall we do memory tests? i.e. you look at Task Manager? y/n > ") |
|
184 |
assert inp in 'yn', 'enter "y" or "n". Please run again!' |
|
185 |
pause = (inp == 'y') |
|
186 |
||
187 |
||
188 |
||
189 |
print 'testing %s' % testName |
|
190 |
#load the parser |
|
191 |
t0 = time.clock() |
|
192 |
parser = loadFunc() |
|
193 |
loadTime = time.clock() - t0 |
|
194 |
if pause: |
|
195 |
baseMem = float(raw_input("Pre-parsing: please input python process memory in kb > ")) |
|
196 |
t1 = time.clock() |
|
197 |
parsedOutput = parseFunc(parser, sampleText) |
|
198 |
t2 = time.clock() |
|
199 |
parseTime = t2 - t1 |
|
200 |
||
201 |
if pause: |
|
202 |
totalMem = float(raw_input('Post-parsing: please input python process memory in kb > ')) |
|
203 |
usedMem = totalMem - baseMem |
|
204 |
memFactor = usedMem * 1024.0 / len(sampleText) |
|
205 |
t3 = time.clock() |
|
206 |
n, a = statFunc(parsedOutput) |
|
207 |
t4 = time.clock() |
|
208 |
traverseTime = t4 - t3 |
|
209 |
print 'counted %d tags, %d attributes' % (n, a) |
|
210 |
if pause: |
|
211 |
print '%s: init %0.4f, parse %0.4f, traverse %0.4f, mem used %dkb, mem factor %0.2f' % ( |
|
212 |
name, loadTime, parseTime, traverseTime, usedMem, memFactor) |
|
213 |
else: |
|
214 |
print '%s: init %0.4f, parse %0.4f, traverse %0.4f' % ( |
|
215 |
name, loadTime, parseTime, traverseTime) |
|
216 |
||
217 |
||
218 |
||
219 |
if __name__=='__main__': |
|
220 |
import sys |
|
221 |
args = sys.argv[:] |
|
222 |
if '-nodtd' in args: |
|
223 |
dtd=0 |
|
224 |
args.remove('-nodtd') |
|
225 |
else: |
|
226 |
dtd=1 |
|
227 |
||
228 |
if '-pause' in args: |
|
229 |
pause = 1 |
|
230 |
args.remove('-pause') |
|
231 |
elif '-nopause' in args: |
|
232 |
pause = 0 |
|
233 |
args.remove('-nopause') |
|
234 |
else: |
|
235 |
pause = 'unknown' # it will ask |
|
236 |
if len(args) > 1: |
|
237 |
testName = args[1] |
|
238 |
else: |
|
239 |
testName = None |
|
240 |
interact(testName, dtd, pause=pause) |
|
241 |
||
242 |