| 1 |
from itertools import * |
| 2 |
|
| 3 |
import benchbase |
| 4 |
from benchbase import onlylib, children, nochange |
| 5 |
|
| 6 |
############################################################ |
| 7 |
# Benchmarks |
| 8 |
############################################################ |
| 9 |
|
| 10 |
class XPathBenchMark(benchbase.TreeBenchMark): |
| 11 |
@nochange |
| 12 |
@onlylib('lxe') |
| 13 |
@children |
| 14 |
def bench_xpath_class(self, children): |
| 15 |
xpath = self.etree.XPath("./*[1]") |
| 16 |
for child in children: |
| 17 |
xpath(child) |
| 18 |
|
| 19 |
@nochange |
| 20 |
@onlylib('lxe') |
| 21 |
@children |
| 22 |
def bench_xpath_class_repeat(self, children): |
| 23 |
for child in children: |
| 24 |
xpath = self.etree.XPath("./*[1]") |
| 25 |
xpath(child) |
| 26 |
|
| 27 |
@nochange |
| 28 |
@onlylib('lxe') |
| 29 |
def bench_xpath_element(self, root): |
| 30 |
xpath = self.etree.XPathElementEvaluator(root) |
| 31 |
for child in root: |
| 32 |
xpath("./*[1]") |
| 33 |
|
| 34 |
@nochange |
| 35 |
@onlylib('lxe') |
| 36 |
@children |
| 37 |
def bench_xpath_method(self, children): |
| 38 |
for child in children: |
| 39 |
child.xpath("./*[1]") |
| 40 |
|
| 41 |
@nochange |
| 42 |
@onlylib('lxe') |
| 43 |
@children |
| 44 |
def bench_multiple_xpath_or(self, children): |
| 45 |
xpath = self.etree.XPath(".//p:a00001|.//p:b00001|.//p:c00001", |
| 46 |
namespaces={'p':'cdefg'}) |
| 47 |
for child in children: |
| 48 |
xpath(child) |
| 49 |
|
| 50 |
@nochange |
| 51 |
@onlylib('lxe') |
| 52 |
@children |
| 53 |
def bench_multiple_iter_tag(self, children): |
| 54 |
for child in children: |
| 55 |
list(child.iter("{cdefg}a00001")) |
| 56 |
list(child.iter("{cdefg}b00001")) |
| 57 |
list(child.iter("{cdefg}c00001")) |
| 58 |
|
| 59 |
@nochange |
| 60 |
@onlylib('lxe') |
| 61 |
@children |
| 62 |
def bench_xpath_old_extensions(self, children): |
| 63 |
def return_child(_, elements): |
| 64 |
if elements: |
| 65 |
return elements[0][0] |
| 66 |
else: |
| 67 |
return () |
| 68 |
extensions = {("test", "child") : return_child} |
| 69 |
xpath = self.etree.XPath("t:child(.)", namespaces={"t":"test"}, |
| 70 |
extensions=extensions) |
| 71 |
for child in children: |
| 72 |
xpath(child) |
| 73 |
|
| 74 |
@nochange |
| 75 |
@onlylib('lxe') |
| 76 |
@children |
| 77 |
def bench_xpath_extensions(self, children): |
| 78 |
def return_child(_, elements): |
| 79 |
if elements: |
| 80 |
return elements[0][0] |
| 81 |
else: |
| 82 |
return () |
| 83 |
self.etree.FunctionNamespace("testns")["t"] = return_child |
| 84 |
|
| 85 |
try: |
| 86 |
xpath = self.etree.XPath("test:t(.)", namespaces={"test":"testns"}) |
| 87 |
for child in children: |
| 88 |
xpath(child) |
| 89 |
finally: |
| 90 |
del self.etree.FunctionNamespace("testns")["t"] |
| 91 |
|
| 92 |
if __name__ == '__main__': |
| 93 |
benchbase.main(XPathBenchMark) |