Debian Codegraph @

benchmark/bench_etree.py

raw
1 import copy
2 from io import BytesIO
3 from itertools import *
4
5 import benchbase
6 from benchbase import (with_attributes, with_text, onlylib,
7 serialized, children, nochange,
8 anytree, widetree, widesubtree)
9
10 TEXT = "some ASCII text"
11 UTEXT = u"some klingon: \uF8D2"
12
13 ############################################################
14 # Benchmarks
15 ############################################################
16
17 class BenchMark(benchbase.TreeBenchMark):
18 @anytree
19 @nochange
20 def bench_iter_children(self, root):
21 for child in root:
22 pass
23
24 @anytree
25 @nochange
26 def bench_iter_children_reversed(self, root):
27 for child in reversed(root):
28 pass
29
30 @anytree
31 @nochange
32 def bench_first_child(self, root):
33 for i in self.repeat1000:
34 child = root[0]
35
36 @anytree
37 @nochange
38 def bench_last_child(self, root):
39 for i in self.repeat1000:
40 child = root[-1]
41
42 @widetree
43 @nochange
44 def bench_middle_child(self, root):
45 pos = len(root) // 2
46 for i in self.repeat1000:
47 child = root[pos]
48
49 @nochange
50 @with_attributes(False)
51 @with_text(text=True)
52 def bench_tostring_text_ascii(self, root):
53 self.etree.tostring(root, method="text")
54
55 @nochange
56 @with_attributes(False)
57 @with_text(text=True, utext=True)
58 def bench_tostring_text_unicode(self, root):
59 self.etree.tostring(root, method="text", encoding='unicode')
60
61 @nochange
62 @with_attributes(False)
63 @with_text(text=True, utext=True)
64 def bench_tostring_text_utf16(self, root):
65 self.etree.tostring(root, method="text", encoding='UTF-16')
66
67 @nochange
68 @with_attributes(False)
69 @with_text(text=True, utext=True)
70 @onlylib('lxe')
71 @children
72 def bench_tostring_text_utf8_with_tail(self, children):
73 for child in children:
74 self.etree.tostring(child, method="text",
75 encoding='UTF-8', with_tail=True)
76
77 @nochange
78 @with_attributes(True, False)
79 @with_text(text=True, utext=True)
80 def bench_tostring_utf8(self, root):
81 self.etree.tostring(root, encoding='UTF-8')
82
83 @nochange
84 @with_attributes(True, False)
85 @with_text(text=True, utext=True)
86 def bench_tostring_utf16(self, root):
87 self.etree.tostring(root, encoding='UTF-16')
88
89 @nochange
90 @with_attributes(True, False)
91 @with_text(text=True, utext=True)
92 def bench_tostring_utf8_unicode_XML(self, root):
93 xml = self.etree.tostring(root, encoding='UTF-8').decode('UTF-8')
94 self.etree.XML(xml)
95
96 @nochange
97 @with_attributes(True, False)
98 @with_text(text=True, utext=True)
99 def bench_write_utf8_parse_bytesIO(self, root):
100 f = BytesIO()
101 self.etree.ElementTree(root).write(f, encoding='UTF-8')
102 f.seek(0)
103 self.etree.parse(f)
104
105 @with_attributes(True, False)
106 @with_text(text=True, utext=True)
107 @serialized
108 def bench_parse_bytesIO(self, root_xml):
109 f = BytesIO(root_xml)
110 self.etree.parse(f)
111
112 @with_attributes(True, False)
113 @with_text(text=True, utext=True)
114 @serialized
115 def bench_XML(self, root_xml):
116 self.etree.XML(root_xml)
117
118 @with_attributes(True, False)
119 @with_text(text=True, utext=True)
120 @serialized
121 def bench_iterparse_bytesIO(self, root_xml):
122 f = BytesIO(root_xml)
123 for event, element in self.etree.iterparse(f):
124 pass
125
126 @with_attributes(True, False)
127 @with_text(text=True, utext=True)
128 @serialized
129 def bench_iterparse_bytesIO_clear(self, root_xml):
130 f = BytesIO(root_xml)
131 for event, element in self.etree.iterparse(f):
132 element.clear()
133
134 @anytree
135 def bench_append_from_document(self, root1, root2):
136 # == "1,2 2,3 1,3 3,1 3,2 2,1" # trees 1 and 2, or 2 and 3, or ...
137 for el in root2:
138 root1.append(el)
139
140 @anytree
141 def bench_insert_from_document(self, root1, root2):
142 pos = len(root1)//2
143 for el in root2:
144 root1.insert(pos, el)
145 pos = pos + 1
146
147 def bench_rotate_children(self, root):
148 # == "1 2 3" # runs on any single tree independently
149 for i in range(100):
150 el = root[0]
151 del root[0]
152 root.append(el)
153
154 @widetree
155 def bench_reorder(self, root):
156 for i in range(1,len(root)//2):
157 el = root[0]
158 del root[0]
159 root[-i:-i] = [ el ]
160
161 @widetree
162 def bench_reorder_slice(self, root):
163 for i in range(1,len(root)//2):
164 els = root[0:1]
165 del root[0]
166 root[-i:-i] = els
167
168 def bench_clear(self, root):
169 root.clear()
170
171 @widetree
172 @nochange
173 @children
174 def bench_len(self, children):
175 for child in children:
176 map(len, repeat(child, 20))
177
178 @widetree
179 @children
180 def bench_create_subelements(self, children):
181 SubElement = self.etree.SubElement
182 for child in children:
183 SubElement(child, '{test}test')
184
185 @widetree
186 @children
187 def bench_append_elements(self, children):
188 Element = self.etree.Element
189 for child in children:
190 el = Element('{test}test')
191 child.append(el)
192
193 @widetree
194 @nochange
195 @children
196 def bench_makeelement(self, children):
197 empty_attrib = {}
198 for child in children:
199 child.makeelement('{test}test', empty_attrib)
200
201 @widetree
202 @nochange
203 @children
204 def bench_create_elements(self, children):
205 Element = self.etree.Element
206 for child in children:
207 Element('{test}test')
208
209 @widetree
210 @children
211 def bench_replace_children_element(self, children):
212 Element = self.etree.Element
213 for child in children:
214 el = Element('{test}test')
215 child[:] = [el]
216
217 @widetree
218 @children
219 def bench_replace_children(self, children):
220 els = [ self.etree.Element("newchild") ]
221 for child in children:
222 child[:] = els
223
224 @widetree
225 def bench_remove_children(self, root):
226 for child in root:
227 root.remove(child)
228
229 @widetree
230 def bench_remove_children_reversed(self, root):
231 for child in reversed(root):
232 root.remove(child)
233
234 @widetree
235 @children
236 def bench_set_attributes(self, children):
237 for child in children:
238 child.set('a', 'bla')
239
240 @widetree
241 @with_attributes(True)
242 @children
243 @nochange
244 def bench_get_attributes(self, children):
245 for child in children:
246 child.get('bla1')
247 child.get('{attr}test1')
248
249 @widetree
250 @children
251 def bench_setget_attributes(self, children):
252 for child in children:
253 child.set('a', 'bla')
254 for child in children:
255 child.get('a')
256
257 @widetree
258 @nochange
259 def bench_root_getchildren(self, root):
260 root.getchildren()
261
262 @widetree
263 @nochange
264 def bench_root_list_children(self, root):
265 list(root)
266
267 @widesubtree
268 @nochange
269 @children
270 def bench_getchildren(self, children):
271 for child in children:
272 child.getchildren()
273
274 @widesubtree
275 @nochange
276 @children
277 def bench_get_children_slice(self, children):
278 for child in children:
279 child[:]
280
281 @widesubtree
282 @nochange
283 @children
284 def bench_get_children_slice_2x(self, children):
285 for child in children:
286 child[:]
287 child[:]
288
289 @nochange
290 @children
291 @with_attributes(True, False)
292 @with_text(utext=True, text=True, no_text=True)
293 def bench_deepcopy(self, children):
294 for child in children:
295 copy.deepcopy(child)
296
297 @nochange
298 @with_attributes(True, False)
299 @with_text(utext=True, text=True, no_text=True)
300 def bench_deepcopy_all(self, root):
301 copy.deepcopy(root)
302
303 @widetree
304 @nochange
305 @children
306 def bench_tag(self, children):
307 for child in children:
308 child.tag
309
310 @widetree
311 @nochange
312 @children
313 def bench_tag_repeat(self, children):
314 for child in children:
315 for i in self.repeat100:
316 child.tag
317
318 @widetree
319 @nochange
320 @with_text(utext=True, text=True, no_text=True)
321 @children
322 def bench_text(self, children):
323 for child in children:
324 child.text
325
326 @widetree
327 @nochange
328 @with_text(utext=True, text=True, no_text=True)
329 @children
330 def bench_text_repeat(self, children):
331 for child in children:
332 for i in self.repeat500:
333 child.text
334
335 @widetree
336 @children
337 def bench_set_text(self, children):
338 text = TEXT
339 for child in children:
340 child.text = text
341
342 @widetree
343 @children
344 def bench_set_utext(self, children):
345 text = UTEXT
346 for child in children:
347 child.text = text
348
349 @widetree
350 @nochange
351 @onlylib('lxe')
352 def bench_index(self, root):
353 for child in root:
354 root.index(child)
355
356 @widetree
357 @nochange
358 @onlylib('lxe')
359 def bench_index_slice(self, root):
360 for child in root[5:100]:
361 root.index(child, 5, 100)
362
363 @widetree
364 @nochange
365 @onlylib('lxe')
366 def bench_index_slice_neg(self, root):
367 for child in root[-100:-5]:
368 root.index(child, start=-100, stop=-5)
369
370 @nochange
371 def bench_iter_all(self, root):
372 list(root.iter())
373
374 @nochange
375 def bench_iter_one_at_a_time(self, root):
376 list(islice(root.iter(), 2**30, None))
377
378 @nochange
379 def bench_iter_islice(self, root):
380 list(islice(root.iter(), 10, 110))
381
382 @nochange
383 def bench_iter_tag(self, root):
384 list(islice(root.iter(self.SEARCH_TAG), 3, 10))
385
386 @nochange
387 def bench_iter_tag_all(self, root):
388 list(root.iter(self.SEARCH_TAG))
389
390 @nochange
391 def bench_iter_tag_one_at_a_time(self, root):
392 list(islice(root.iter(self.SEARCH_TAG), 2**30, None))
393
394 @nochange
395 def bench_iter_tag_none(self, root):
396 list(root.iter("{ThisShould}NeverExist"))
397
398 @nochange
399 def bench_iter_tag_text(self, root):
400 [ e.text for e in root.iter(self.SEARCH_TAG) ]
401
402 @nochange
403 def bench_findall(self, root):
404 root.findall(".//*")
405
406 @nochange
407 def bench_findall_child(self, root):
408 root.findall(".//*/" + self.SEARCH_TAG)
409
410 @nochange
411 def bench_findall_tag(self, root):
412 root.findall(".//" + self.SEARCH_TAG)
413
414 @nochange
415 def bench_findall_path(self, root):
416 root.findall(".//*[%s]/./%s/./*" % (self.SEARCH_TAG, self.SEARCH_TAG))
417
418 @nochange
419 @onlylib('lxe')
420 def bench_xpath_path(self, root):
421 ns, tag = self.SEARCH_TAG[1:].split('}')
422 root.xpath(".//*[p:%s]/./p:%s/./*" % (tag,tag),
423 namespaces = {'p':ns})
424
425 @nochange
426 def bench_iterfind(self, root):
427 list(root.iterfind(".//*"))
428
429 @nochange
430 def bench_iterfind_tag(self, root):
431 list(root.iterfind(".//" + self.SEARCH_TAG))
432
433 @nochange
434 def bench_iterfind_islice(self, root):
435 list(islice(root.iterfind(".//*"), 10, 110))
436
437 _bench_xpath_single_xpath = None
438
439 @nochange
440 @onlylib('lxe')
441 def bench_xpath_single(self, root):
442 xpath = self._bench_xpath_single_xpath
443 if xpath is None:
444 ns, tag = self.SEARCH_TAG[1:].split('}')
445 xpath = self._bench_xpath_single_xpath = self.etree.XPath(
446 './/p:%s[1]' % tag, namespaces={'p': ns})
447 xpath(root)
448
449 @nochange
450 def bench_find_single(self, root):
451 root.find(".//%s" % self.SEARCH_TAG)
452
453 @nochange
454 def bench_iter_single(self, root):
455 next(root.iter(self.SEARCH_TAG))
456
457 _bench_xpath_two_xpath = None
458
459 @nochange
460 @onlylib('lxe')
461 def bench_xpath_two(self, root):
462 xpath = self._bench_xpath_two_xpath
463 if xpath is None:
464 ns, tag = self.SEARCH_TAG[1:].split('}')
465 xpath = self._bench_xpath_two_xpath = self.etree.XPath(
466 './/p:%s[position() < 3]' % tag, namespaces={'p': ns})
467 xpath(root)
468
469 @nochange
470 def bench_iterfind_two(self, root):
471 it = root.iterfind(".//%s" % self.SEARCH_TAG)
472 next(it)
473 next(it)
474
475 @nochange
476 def bench_iter_two(self, root):
477 it = root.iter(self.SEARCH_TAG)
478 next(it)
479 next(it)
480
481
482 if __name__ == '__main__':
483 benchbase.main(BenchMark)