1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tests/libxml2macro/performance.py Sun Feb 27 18:55:07 2011 +0100
1.3 @@ -0,0 +1,84 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +A performance test using libxml2dom.macrolib, libxml2dom and minidom.
1.8 +This module must be compiled using libxml2macro.py and run only as a compiled
1.9 +module - ie. as performance.pyc.
1.10 +"""
1.11 +
1.12 +import xml.dom
1.13 +
1.14 +def find_root(d):
1.15 + root = None
1.16 + for n in d.childNodes:
1.17 + if n.nodeType == xml.dom.Node.ELEMENT_NODE:
1.18 + root = n
1.19 + break
1.20 + return root
1.21 +
1.22 +import libxml2macro as x2_
1.23 +
1.24 +def find_root_libxml2macro(x2_d):
1.25 + x2_root = None
1.26 + for x2_n in x2_d.childNodes:
1.27 + if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE:
1.28 + x2_root = x2_n
1.29 + break
1.30 + return x2_root
1.31 +
1.32 +def test_import_libxml2macro(x2_d):
1.33 + x2_d2 = createDocument("nsD", "newdoc", None)
1.34 + x2_imported = x2_d.importNode(find_root_libxml2macro(x2_d), 1)
1.35 + x2_d2.replaceChild(x2_imported, find_root_libxml2macro(x2_d2))
1.36 + return x2_d, x2_d2
1.37 +
1.38 +def test_import_minidom(d):
1.39 + d2 = xml.dom.minidom.getDOMImplementation().createDocument("nsD", "newdoc", None)
1.40 + imported = d2.importNode(find_root(d), 1)
1.41 + d2.replaceChild(imported, find_root(d2))
1.42 + return d, d2
1.43 +
1.44 +def test_import_libxml2dom(d):
1.45 + d2 = libxml2dom.createDocument("nsD", "newdoc", None)
1.46 + imported = d2.importNode(find_root(d), 1)
1.47 + d2.replaceChild(imported, find_root(d2))
1.48 + return d, d2
1.49 +
1.50 +if __name__ == "__main__":
1.51 + import sys
1.52 + import time
1.53 +
1.54 + if len(sys.argv) < 3:
1.55 + print "Please specify a filename (of a fairly large XML document) and the testing mode."
1.56 + print "There are quite a few large files in the libxml2 distribution."
1.57 + print "For the testing mode, choose one of libxml2macro, minidom, libxml2dom."
1.58 + sys.exit(1)
1.59 +
1.60 + if sys.argv[2] == "libxml2macro":
1.61 +
1.62 + x2_d = parseFile(sys.argv[1])
1.63 +
1.64 + t = time.time()
1.65 + x2_d1, x2_d2 = test_import_libxml2macro(x2_d)
1.66 + toFile(x2_d2, "/tmp/xxx_libxml2macro.xml")
1.67 + print "Time", time.time() - t, "seconds"
1.68 +
1.69 + elif sys.argv[2] == "minidom":
1.70 + import xml.dom.minidom
1.71 + d = xml.dom.minidom.parse(sys.argv[1])
1.72 +
1.73 + t = time.time()
1.74 + d1, d2 = test_import_minidom(d)
1.75 + open("/tmp/xxx_minidom.xml", "wb").write(d2.toxml("utf-8"))
1.76 + print "Time", time.time() - t, "seconds"
1.77 +
1.78 + elif sys.argv[2] == "libxml2dom":
1.79 + import libxml2dom
1.80 + d = libxml2dom.parse(sys.argv[1])
1.81 +
1.82 + t = time.time()
1.83 + d1, d2 = test_import_libxml2dom(d)
1.84 + libxml2dom.toStream(d2, open("/tmp/xxx_libxml2dom.xml", "wb"))
1.85 + print "Time", time.time() - t, "seconds"
1.86 +
1.87 +# vim: tabstop=4 expandtab shiftwidth=4