| 1 | |
| 2 | import sys |
| 3 | import os |
| 4 | from xml.dom.minidom import getDOMImplementation |
| 5 | from timeit import Timer |
| 6 | import subprocess |
| 7 | |
| 8 | sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), |
| 9 | '..', 'build', 'default', 'benchmarks')) |
| 10 | |
| 11 | |
| 12 | TIMES = 10000000 |
| 13 | TIMES1 = TIMES/4 |
| 14 | |
| 15 | import testapi_pybindgen |
| 16 | import testapi_boost |
| 17 | import testapi_swig |
| 18 | import testapi_sip |
| 19 | |
| 20 | |
| 21 | def bench(mod, dom, elem): |
| 22 | def bench(): |
| 23 | return mod.func1() |
| 24 | tst = elem.appendChild(dom.createElement('test')) |
| 25 | tst.setAttribute("description", "call function with no arguments") |
| 26 | tst.setAttribute("time", repr(Timer(bench).timeit(TIMES))) |
| 27 | print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time')) |
| 28 | |
| 29 | def bench(): |
| 30 | return mod.func2(1.0, 2.0, 3.0) |
| 31 | tst = elem.appendChild(dom.createElement('test')) |
| 32 | tst.setAttribute("description", "call function taking 3 doubles") |
| 33 | tst.setAttribute("time", repr(Timer(bench).timeit(TIMES))) |
| 34 | print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time')) |
| 35 | |
| 36 | def bench(): |
| 37 | return mod.Multiplier() |
| 38 | tst = elem.appendChild(dom.createElement('test')) |
| 39 | tst.setAttribute("description", "call class constructor with no arguments") |
| 40 | tst.setAttribute("time", repr(Timer(bench).timeit(TIMES))) |
| 41 | print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time')) |
| 42 | |
| 43 | def bench(): |
| 44 | return mod.Multiplier(3.0) |
| 45 | tst = elem.appendChild(dom.createElement('test')) |
| 46 | tst.setAttribute("description", "call class constructor with double") |
| 47 | tst.setAttribute("time", repr(Timer(bench).timeit(TIMES))) |
| 48 | print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time')) |
| 49 | |
| 50 | obj = mod.Multiplier(3.0) |
| 51 | def bench(): |
| 52 | return obj.GetFactor() |
| 53 | tst = elem.appendChild(dom.createElement('test')) |
| 54 | tst.setAttribute("description", "call simple method") |
| 55 | tst.setAttribute("time", repr(Timer(bench).timeit(TIMES))) |
| 56 | print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time')) |
| 57 | |
| 58 | obj = mod.Multiplier(3.0) |
| 59 | def bench(): |
| 60 | return obj.SetFactor() |
| 61 | tst = elem.appendChild(dom.createElement('test')) |
| 62 | tst.setAttribute("description", "call overloaded method 1") |
| 63 | tst.setAttribute("time", repr(Timer(bench).timeit(TIMES))) |
| 64 | print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time')) |
| 65 | |
| 66 | obj = mod.Multiplier(3.0) |
| 67 | def bench(): |
| 68 | return obj.SetFactor(1.0) |
| 69 | tst = elem.appendChild(dom.createElement('test')) |
| 70 | tst.setAttribute("description", "call overloaded method 2") |
| 71 | tst.setAttribute("time", repr(Timer(bench).timeit(TIMES))) |
| 72 | print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time')) |
| 73 | |
| 74 | obj = mod.Multiplier(3.0) |
| 75 | def bench(): |
| 76 | return obj.Multiply(5.0) |
| 77 | tst = elem.appendChild(dom.createElement('test')) |
| 78 | tst.setAttribute("description", "call non-overridden virtual method with double") |
| 79 | tst.setAttribute("time", repr(Timer(bench).timeit(TIMES))) |
| 80 | print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time')) |
| 81 | |
| 82 | class M(mod.Multiplier): |
| 83 | def Multiply(self, value): |
| 84 | return super(M, self).Multiply(value) |
| 85 | obj = M(2.0) |
| 86 | def bench(): |
| 87 | return obj.Multiply(5.0) |
| 88 | |
| 89 | tst = elem.appendChild(dom.createElement('test')) |
| 90 | tst.setAttribute("description", "call python-overridden virtual method from Python") |
| 91 | tst.setAttribute("time", repr(Timer(bench).timeit(TIMES1))) |
| 92 | print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time')) |
| 93 | |
| 94 | def bench(): |
| 95 | return mod.call_virtual_from_cpp(obj, 5.0) |
| 96 | tst = elem.appendChild(dom.createElement('test')) |
| 97 | tst.setAttribute("description", "call python-overridden virtual method from C++") |
| 98 | tst.setAttribute("time", repr(Timer(bench).timeit(TIMES1))) |
| 99 | print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time')) |
| 100 | |
| 101 | |
| 102 | def main(): |
| 103 | impl = getDOMImplementation() |
| 104 | dom = impl.createDocument(None, "pybindgen-benchmarks", None) |
| 105 | top = dom.documentElement |
| 106 | |
| 107 | env = top.appendChild(dom.createElement('environment')) |
| 108 | env.appendChild(dom.createElement('compiler')).appendChild(dom.createTextNode( |
| 109 | subprocess.Popen(["g++", "-v"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0])) |
| 110 | env.appendChild(dom.createElement('python')).appendChild(dom.createTextNode( |
| 111 | sys.version)) |
| 112 | env.appendChild(dom.createElement('swig')).appendChild(dom.createTextNode( |
| 113 | subprocess.Popen(["swig", "-version"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0])) |
| 114 | env.appendChild(dom.createElement('boost_python')).appendChild(dom.createTextNode( |
| 115 | subprocess.Popen(["dpkg", "-s", "libboost-python-dev"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0])) |
| 116 | env.appendChild(dom.createElement('pybindgen')).appendChild(dom.createTextNode( |
| 117 | subprocess.Popen(["bzr", "version-info", '--check-clean'], |
| 118 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0])) |
| 119 | |
| 120 | env.appendChild(dom.createElement('cpu')).appendChild(dom.createTextNode( |
| 121 | file("/proc/cpuinfo").read())) |
| 122 | |
| 123 | if len(sys.argv) == 3: |
| 124 | env.appendChild(dom.createElement('CXXFLAGS')).appendChild(dom.createTextNode(sys.argv[2])) |
| 125 | |
| 126 | |
| 127 | res = top.appendChild(dom.createElement('results')) |
| 128 | |
| 129 | |
| 130 | print "pybindgen results:" |
| 131 | pbg = res.appendChild(dom.createElement('pybindgen')) |
| 132 | pbg.setAttribute("module-file-size", repr(os.stat("build/default/benchmarks/testapi_pybindgen.so").st_size)) |
| 133 | bench(testapi_pybindgen, dom, pbg) |
| 134 | |
| 135 | print "boost_python results:" |
| 136 | bp = res.appendChild(dom.createElement('boost_python')) |
| 137 | bp.setAttribute("module-file-size", repr(os.stat("build/default/benchmarks/testapi_boost.so").st_size)) |
| 138 | bench(testapi_boost, dom, bp) |
| 139 | |
| 140 | print "swig results:" |
| 141 | sw = res.appendChild(dom.createElement('swig')) |
| 142 | sw.setAttribute("module-file-size", repr(os.stat("build/default/benchmarks/_testapi_swig.so").st_size)) |
| 143 | sw.setAttribute("module-python-file-size", repr(os.stat("build/default/benchmarks/testapi_swig.py").st_size)) |
| 144 | bench(testapi_swig, dom, sw) |
| 145 | |
| 146 | print "sip results:" |
| 147 | sip = res.appendChild(dom.createElement('sip')) |
| 148 | sip.setAttribute("module-file-size", repr(os.stat("build/default/benchmarks/testapi_sip.so").st_size)) |
| 149 | bench(testapi_sip, dom, sip) |
| 150 | |
| 151 | if len(sys.argv) == 3: |
| 152 | f = open(sys.argv[1], "wb") |
| 153 | dom.writexml(f, "", " ", "\n") |
| 154 | f.close() |
| 155 | |
| 156 | |
| 157 | |
| 158 | if __name__ == '__main__': |
| 159 | main() |