paulb@63 | 1 | #!/usr/bin/env python |
paulb@63 | 2 | |
paulb@65 | 3 | """ |
paulb@65 | 4 | The controversial "begat" benchmark. This module must be compiled using |
paulb@65 | 5 | libxml2macro.py before use, and must then be invoked directly as a compiled |
paulb@65 | 6 | module - ie. as begat.pyc. |
paulb@65 | 7 | """ |
paulb@65 | 8 | |
paulb@63 | 9 | import libxml2macro as n_ |
paulb@74 | 10 | |
paulb@74 | 11 | def test_begat_libxml2macro(n_doc, full_xpath): |
paulb@74 | 12 | l = [] |
paulb@63 | 13 | |
paulb@74 | 14 | if full_xpath: |
paulb@74 | 15 | for n_node in n_doc.xpath("//v[contains(text(), 'begat')]"): |
paulb@74 | 16 | text = n_node.nodeValue |
paulb@74 | 17 | l.append(text) |
paulb@74 | 18 | else: |
paulb@74 | 19 | # NOTE: Code corresponding to this was suggested for cElementTree, but why not |
paulb@74 | 20 | # NOTE: take full advantage of XPath if you have most of the code written in C? |
paulb@63 | 21 | |
paulb@74 | 22 | for n_node in n_doc.xpath("//v"): |
paulb@74 | 23 | text = n_node.nodeValue |
paulb@74 | 24 | if text.find(u'begat') != -1: |
paulb@74 | 25 | l.append(text) |
paulb@74 | 26 | |
paulb@74 | 27 | return l |
paulb@74 | 28 | |
paulb@74 | 29 | def test_begat_libxml2dom(doc, full_xpath): |
paulb@74 | 30 | l = [] |
paulb@74 | 31 | |
paulb@74 | 32 | if full_xpath: |
paulb@74 | 33 | for node in doc.xpath("//v[contains(text(), 'begat')]"): |
paulb@74 | 34 | text = node.nodeValue |
paulb@74 | 35 | l.append(text) |
paulb@74 | 36 | else: |
paulb@74 | 37 | # NOTE: Code corresponding to this was suggested for cElementTree, but why not |
paulb@74 | 38 | # NOTE: take full advantage of XPath if you have most of the code written in C? |
paulb@63 | 39 | |
paulb@74 | 40 | for node in doc.xpath("//v"): |
paulb@74 | 41 | text = node.nodeValue |
paulb@74 | 42 | if text.find(u'begat') != -1: |
paulb@74 | 43 | l.append(text) |
paulb@74 | 44 | |
paulb@74 | 45 | return l |
paulb@74 | 46 | |
paulb@74 | 47 | if __name__ == "__main__": |
paulb@74 | 48 | import time, os |
paulb@74 | 49 | import sys |
paulb@74 | 50 | |
paulb@74 | 51 | ot_locations = [arg for arg in sys.argv if arg.endswith("ot.xml")] |
paulb@74 | 52 | full_xpath = "--full" in sys.argv |
paulb@74 | 53 | use_libxml2dom = "libxml2dom" in sys.argv |
paulb@74 | 54 | use_libxml2macro = "libxml2macro" in sys.argv |
paulb@76 | 55 | iterations = [int(arg.split("-")[0]) for arg in sys.argv if arg.endswith("-times")] |
paulb@63 | 56 | |
paulb@74 | 57 | if len(ot_locations) == 0: |
paulb@74 | 58 | print "Please specify the location of the ot.xml file." |
paulb@74 | 59 | sys.exit(1) |
paulb@74 | 60 | |
paulb@76 | 61 | if len(iterations) == 0: |
paulb@76 | 62 | iterations = 1 |
paulb@76 | 63 | else: |
paulb@76 | 64 | iterations = iterations[0] |
paulb@76 | 65 | |
paulb@74 | 66 | raw_input("Start your engines with ps -p %s -fv" % os.getpid()) |
paulb@74 | 67 | t = time.time() |
paulb@63 | 68 | |
paulb@76 | 69 | for i in range(0, iterations): |
paulb@76 | 70 | if use_libxml2macro: |
paulb@76 | 71 | n_doc = parseFile(ot_locations[0]) |
paulb@76 | 72 | l = test_begat_libxml2macro(n_doc, full_xpath) |
paulb@76 | 73 | else: # use_libxml2dom: |
paulb@76 | 74 | import libxml2dom |
paulb@76 | 75 | doc = libxml2dom.parse(ot_locations[0]) |
paulb@76 | 76 | l = test_begat_libxml2dom(doc, full_xpath) |
paulb@74 | 77 | |
paulb@74 | 78 | print "Time taken", time.time() - t |
paulb@74 | 79 | raw_input("Stop your engines!") |
paulb@74 | 80 | |
paulb@74 | 81 | print l |
paulb@63 | 82 | |
paulb@63 | 83 | # vim: tabstop=4 expandtab shiftwidth=4 |