libxml2dom

tests/performance.py

299:03817ecc20ed
2008-06-20 Paul Boddie Added tag rel-0-4-4 for changeset 3846b04d366b
     1 #!/usr/bin/env python     2      3 """     4 A performance test using libxml2dom.macrolib, libxml2dom and minidom.     5 This module must be compiled using libxml2macro.py and run only as a compiled     6 module - ie. as performance.pyc.     7 """     8      9 import xml.dom    10     11 def find_root(d):    12     root = None    13     for n in d.childNodes:    14         if n.nodeType == xml.dom.Node.ELEMENT_NODE:    15             root = n    16             break    17     return root    18     19 import libxml2macro as x2_    20     21 def find_root_libxml2macro(x2_d):    22     x2_root = None    23     for x2_n in x2_d.childNodes:    24         if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE:    25             x2_root = x2_n    26             break    27     return x2_root    28     29 def test_import_libxml2macro(x2_d):    30     x2_d2 = createDocument("nsD", "newdoc", None)    31     x2_imported = x2_d.importNode(find_root_libxml2macro(x2_d), 1)    32     x2_d2.replaceChild(x2_imported, find_root_libxml2macro(x2_d2))    33     return x2_d, x2_d2    34     35 def test_import_minidom(d):    36     d2 = xml.dom.minidom.getDOMImplementation().createDocument("nsD", "newdoc", None)    37     imported = d2.importNode(find_root(d), 1)    38     d2.replaceChild(imported, find_root(d2))    39     return d, d2    40     41 def test_import_libxml2dom(d):    42     d2 = libxml2dom.createDocument("nsD", "newdoc", None)    43     imported = d2.importNode(find_root(d), 1)    44     d2.replaceChild(imported, find_root(d2))    45     return d, d2    46     47 if __name__ == "__main__":    48     import sys    49     import time    50     51     if len(sys.argv) < 3:    52         print "Please specify a filename (of a fairly large XML document) and the testing mode."    53         print "There are quite a few large files in the libxml2 distribution."    54         print "For the testing mode, choose one of libxml2macro, minidom, libxml2dom."    55         sys.exit(1)    56     57     if sys.argv[2] == "libxml2macro":    58     59         x2_d = parseFile(sys.argv[1])    60     61         t = time.time()    62         x2_d1, x2_d2 = test_import_libxml2macro(x2_d)    63         toFile(x2_d2, "/tmp/xxx_libxml2macro.xml")    64         print "Time", time.time() - t, "seconds"    65     66     elif sys.argv[2] == "minidom":    67         import xml.dom.minidom    68         d = xml.dom.minidom.parse(sys.argv[1])    69     70         t = time.time()    71         d1, d2 = test_import_minidom(d)    72         open("/tmp/xxx_minidom.xml", "wb").write(d2.toxml("utf-8"))    73         print "Time", time.time() - t, "seconds"    74     75     elif sys.argv[2] == "libxml2dom":    76         import libxml2dom    77         d = libxml2dom.parse(sys.argv[1])    78     79         t = time.time()    80         d1, d2 = test_import_libxml2dom(d)    81         libxml2dom.toStream(d2, open("/tmp/xxx_libxml2dom.xml", "wb"))    82         print "Time", time.time() - t, "seconds"    83     84 # vim: tabstop=4 expandtab shiftwidth=4