1.1 --- a/tests/begat.py Mon Jan 24 16:37:46 2005 +0000
1.2 +++ b/tests/begat.py Mon Jan 24 16:38:55 2005 +0000
1.3 @@ -1,5 +1,11 @@
1.4 #!/usr/bin/env python
1.5
1.6 +"""
1.7 +The controversial "begat" benchmark. This module must be compiled using
1.8 +libxml2macro.py before use, and must then be invoked directly as a compiled
1.9 +module - ie. as begat.pyc.
1.10 +"""
1.11 +
1.12 import libxml2macro as n_
1.13 import time, os
1.14
1.15 @@ -12,10 +18,13 @@
1.16 text = n_node.nodeValue
1.17 l.append(text)
1.18
1.19 -#for n_node in n_doc.xpath("//v"):
1.20 -# text = n_node.nodeValue
1.21 -# if text.find(u'begat') != -1:
1.22 -# print text
1.23 +# NOTE: Code corresponding to this was suggested for cElementTree, but why not
1.24 +# NOTE: take full advantage of XPath if you have most of the code written in C?
1.25 +#
1.26 +# for n_node in n_doc.xpath("//v"):
1.27 +# text = n_node.nodeValue
1.28 +# if text.find(u'begat') != -1:
1.29 +# print text
1.30
1.31 print "Time taken", time.time() - t
1.32 raw_input("Stop your engines!")
2.1 --- a/tests/macrotest.py Mon Jan 24 16:37:46 2005 +0000
2.2 +++ b/tests/macrotest.py Mon Jan 24 16:38:55 2005 +0000
2.3 @@ -1,6 +1,10 @@
2.4 #!/usr/bin/env python
2.5
2.6 -"A test of macros."
2.7 +"""
2.8 +A test of macros. This file must be compiled using libxml2macro.py. It may then
2.9 +be imported normally in Python, but if run then the compiled module should be
2.10 +invoked directly - ie. as macrotest.pyc.
2.11 +"""
2.12
2.13 import libxml2macro as x2_
2.14 import xml.dom
2.15 @@ -103,8 +107,10 @@
2.16 if __name__ == "__main__":
2.17 import sys
2.18 if len(sys.argv) > 1:
2.19 + print "Running a simple test on", sys.argv[1]
2.20 test_file(sys.argv[1])
2.21 else:
2.22 + print "Running a simple test on some built-in string."
2.23 test()
2.24
2.25 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/tests/performance.py Mon Jan 24 16:37:46 2005 +0000
3.2 +++ b/tests/performance.py Mon Jan 24 16:38:55 2005 +0000
3.3 @@ -1,59 +1,83 @@
3.4 #!/usr/bin/env python
3.5
3.6 -import macrotest
3.7 -import xml.dom.minidom
3.8 -import libxml2dom
3.9 +"""
3.10 +A performance test using libxml2dom.macrolib, libxml2dom and minidom.
3.11 +This module must be compiled using libxml2macro.py and run only as a compiled
3.12 +module - ie. as performance.pyc.
3.13 +"""
3.14 +
3.15 import xml.dom
3.16 -import time
3.17 -import sys
3.18
3.19 def find_root(d):
3.20 root = None
3.21 -
3.22 for n in d.childNodes:
3.23 if n.nodeType == xml.dom.Node.ELEMENT_NODE:
3.24 root = n
3.25 break
3.26 -
3.27 return root
3.28
3.29 -def test_import(d):
3.30 +import libxml2macro as x2_
3.31 +
3.32 +def find_root_libxml2macro(x2_d):
3.33 + x2_root = None
3.34 + for x2_n in x2_d.childNodes:
3.35 + if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE:
3.36 + x2_root = x2_n
3.37 + break
3.38 + return x2_root
3.39 +
3.40 +def test_import_libxml2macro(x2_d):
3.41 + x2_d2 = createDocument("nsD", "newdoc", None)
3.42 + x2_imported = x2_d.importNode(find_root_libxml2macro(x2_d), 1)
3.43 + x2_d2.replaceChild(x2_imported, find_root_libxml2macro(x2_d2))
3.44 + return x2_d, x2_d2
3.45 +
3.46 +def test_import_minidom(d):
3.47 d2 = xml.dom.minidom.getDOMImplementation().createDocument("nsD", "newdoc", None)
3.48 imported = d2.importNode(find_root(d), 1)
3.49 d2.replaceChild(imported, find_root(d2))
3.50 return d, d2
3.51
3.52 -def test_import2(d):
3.53 +def test_import_libxml2dom(d):
3.54 d2 = libxml2dom.createDocument("nsD", "newdoc", None)
3.55 imported = d2.importNode(find_root(d), 1)
3.56 d2.replaceChild(imported, find_root(d2))
3.57 return d, d2
3.58
3.59 if __name__ == "__main__":
3.60 - if sys.argv[2] == "macrotest":
3.61 + import sys
3.62 + import time
3.63
3.64 - x2_d = macrotest.parseFile(sys.argv[1])
3.65 + if len(sys.argv) < 3:
3.66 + print "Please specify a filename (of a fairly large XML document) and the testing mode."
3.67 + print "There are quite a few large files in the libxml2 distribution."
3.68 + print "For the testing mode, choose one of libxml2macro, minidom, libxml2dom."
3.69 + sys.exit(1)
3.70 +
3.71 + if sys.argv[2] == "libxml2macro":
3.72 +
3.73 + x2_d = parseFile(sys.argv[1])
3.74
3.75 t = time.time()
3.76 - x2_d1, x2_d2 = macrotest.test_import(x2_d)
3.77 - macrotest.toFile(x2_d2, "/tmp/xxx_macrotest.xml")
3.78 + x2_d1, x2_d2 = test_import_libxml2macro(x2_d)
3.79 + toFile(x2_d2, "/tmp/xxx_libxml2macro.xml")
3.80 print "Time", time.time() - t, "seconds"
3.81
3.82 elif sys.argv[2] == "minidom":
3.83 -
3.84 + import xml.dom.minidom
3.85 d = xml.dom.minidom.parse(sys.argv[1])
3.86
3.87 t = time.time()
3.88 - d1, d2 = test_import(d)
3.89 + d1, d2 = test_import_minidom(d)
3.90 open("/tmp/xxx_minidom.xml", "wb").write(d2.toxml("utf-8"))
3.91 print "Time", time.time() - t, "seconds"
3.92
3.93 elif sys.argv[2] == "libxml2dom":
3.94 -
3.95 + import libxml2dom
3.96 d = libxml2dom.parse(sys.argv[1])
3.97
3.98 t = time.time()
3.99 - d1, d2 = test_import2(d)
3.100 + d1, d2 = test_import_libxml2dom(d)
3.101 libxml2dom.toStream(d2, open("/tmp/xxx_libxml2dom.xml", "wb"))
3.102 print "Time", time.time() - t, "seconds"
3.103