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