# HG changeset patch # User paulb # Date 1106584735 0 # Node ID f61be8ad99ff424443467867e58a38a9d479d3ea # Parent c52969ce52fbf9f457034adca4da9d5bb1adbfaa [project @ 2005-01-24 16:38:55 by paulb] Improved testing documentation. Removed macrotest dependency in the performance test, introducing a "mixed mode" module approach where some of the code will be transformed by libxml2macro.py, whereas the rest of the code is used to test the other libraries. diff -r c52969ce52fb -r f61be8ad99ff tests/begat.py --- a/tests/begat.py Mon Jan 24 16:37:46 2005 +0000 +++ b/tests/begat.py Mon Jan 24 16:38:55 2005 +0000 @@ -1,5 +1,11 @@ #!/usr/bin/env python +""" +The controversial "begat" benchmark. This module must be compiled using +libxml2macro.py before use, and must then be invoked directly as a compiled +module - ie. as begat.pyc. +""" + import libxml2macro as n_ import time, os @@ -12,10 +18,13 @@ text = n_node.nodeValue l.append(text) -#for n_node in n_doc.xpath("//v"): -# text = n_node.nodeValue -# if text.find(u'begat') != -1: -# print text +# NOTE: Code corresponding to this was suggested for cElementTree, but why not +# NOTE: take full advantage of XPath if you have most of the code written in C? +# +# for n_node in n_doc.xpath("//v"): +# text = n_node.nodeValue +# if text.find(u'begat') != -1: +# print text print "Time taken", time.time() - t raw_input("Stop your engines!") diff -r c52969ce52fb -r f61be8ad99ff tests/macrotest.py --- a/tests/macrotest.py Mon Jan 24 16:37:46 2005 +0000 +++ b/tests/macrotest.py Mon Jan 24 16:38:55 2005 +0000 @@ -1,6 +1,10 @@ #!/usr/bin/env python -"A test of macros." +""" +A test of macros. This file must be compiled using libxml2macro.py. It may then +be imported normally in Python, but if run then the compiled module should be +invoked directly - ie. as macrotest.pyc. +""" import libxml2macro as x2_ import xml.dom @@ -103,8 +107,10 @@ if __name__ == "__main__": import sys if len(sys.argv) > 1: + print "Running a simple test on", sys.argv[1] test_file(sys.argv[1]) else: + print "Running a simple test on some built-in string." test() # vim: tabstop=4 expandtab shiftwidth=4 diff -r c52969ce52fb -r f61be8ad99ff tests/performance.py --- a/tests/performance.py Mon Jan 24 16:37:46 2005 +0000 +++ b/tests/performance.py Mon Jan 24 16:38:55 2005 +0000 @@ -1,59 +1,83 @@ #!/usr/bin/env python -import macrotest -import xml.dom.minidom -import libxml2dom +""" +A performance test using libxml2dom.macrolib, libxml2dom and minidom. +This module must be compiled using libxml2macro.py and run only as a compiled +module - ie. as performance.pyc. +""" + import xml.dom -import time -import sys def find_root(d): root = None - for n in d.childNodes: if n.nodeType == xml.dom.Node.ELEMENT_NODE: root = n break - return root -def test_import(d): +import libxml2macro as x2_ + +def find_root_libxml2macro(x2_d): + x2_root = None + for x2_n in x2_d.childNodes: + if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE: + x2_root = x2_n + break + return x2_root + +def test_import_libxml2macro(x2_d): + x2_d2 = createDocument("nsD", "newdoc", None) + x2_imported = x2_d.importNode(find_root_libxml2macro(x2_d), 1) + x2_d2.replaceChild(x2_imported, find_root_libxml2macro(x2_d2)) + return x2_d, x2_d2 + +def test_import_minidom(d): d2 = xml.dom.minidom.getDOMImplementation().createDocument("nsD", "newdoc", None) imported = d2.importNode(find_root(d), 1) d2.replaceChild(imported, find_root(d2)) return d, d2 -def test_import2(d): +def test_import_libxml2dom(d): d2 = libxml2dom.createDocument("nsD", "newdoc", None) imported = d2.importNode(find_root(d), 1) d2.replaceChild(imported, find_root(d2)) return d, d2 if __name__ == "__main__": - if sys.argv[2] == "macrotest": + import sys + import time - x2_d = macrotest.parseFile(sys.argv[1]) + if len(sys.argv) < 3: + print "Please specify a filename (of a fairly large XML document) and the testing mode." + print "There are quite a few large files in the libxml2 distribution." + print "For the testing mode, choose one of libxml2macro, minidom, libxml2dom." + sys.exit(1) + + if sys.argv[2] == "libxml2macro": + + x2_d = parseFile(sys.argv[1]) t = time.time() - x2_d1, x2_d2 = macrotest.test_import(x2_d) - macrotest.toFile(x2_d2, "/tmp/xxx_macrotest.xml") + x2_d1, x2_d2 = test_import_libxml2macro(x2_d) + toFile(x2_d2, "/tmp/xxx_libxml2macro.xml") print "Time", time.time() - t, "seconds" elif sys.argv[2] == "minidom": - + import xml.dom.minidom d = xml.dom.minidom.parse(sys.argv[1]) t = time.time() - d1, d2 = test_import(d) + d1, d2 = test_import_minidom(d) open("/tmp/xxx_minidom.xml", "wb").write(d2.toxml("utf-8")) print "Time", time.time() - t, "seconds" elif sys.argv[2] == "libxml2dom": - + import libxml2dom d = libxml2dom.parse(sys.argv[1]) t = time.time() - d1, d2 = test_import2(d) + d1, d2 = test_import_libxml2dom(d) libxml2dom.toStream(d2, open("/tmp/xxx_libxml2dom.xml", "wb")) print "Time", time.time() - t, "seconds"