OSDN Git Service

perftests: finish profiling code
authorMartin Renold <martinxyz@gmx.ch>
Sun, 7 Feb 2010 13:43:21 +0000 (14:43 +0100)
committerMartin Renold <martinxyz@gmx.ch>
Sun, 7 Feb 2010 13:58:55 +0000 (14:58 +0100)
also add python profiling instructions

profile_py.sh [deleted file]
tests/README.profiling [new file with mode: 0644]
tests/test_performance.py

diff --git a/profile_py.sh b/profile_py.sh
deleted file mode 100755 (executable)
index 53eacfd..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/sh
-(
-
-python -m cProfile -s time mypaint $@ #-p #dab_and_render_performance.myp
-#python -m cProfile -o tmp.prof mypaint #-p #dab_and_render_performance.myp
-
-) > profile.out 2>&1
-head -n 30 profile.out
-
diff --git a/tests/README.profiling b/tests/README.profiling
new file mode 100644 (file)
index 0000000..ce3cb38
--- /dev/null
@@ -0,0 +1,12 @@
+Quick Profiling HOWTO:
+
+# get gprof2dot (you will also need graphviz installed)
+wget http://gprof2dot.jrfonseca.googlecode.com/hg/gprof2dot.py
+chmod +x gprof2dot.py
+
+# run some test with profiling
+./test_performance.py startup -p base
+
+# generate a PNG
+./gprof2dot.py -f base_startup_2.pstats output.pstats | dot -Tpng -o output.png
+
index f5e070c..3114d2d 100755 (executable)
@@ -19,7 +19,7 @@ stop_measurement = -4
 
 all_tests = {}
 
-def run_test(testfunction):
+def run_test(testfunction, profile=None):
     """Run a single test
     testfunction must be a generator (using yield)
     """
@@ -32,7 +32,10 @@ def run_test(testfunction):
             res = tst.next()
             assert res == stop_measurement
         t0 = time()
-        run_function_under_test() # <--- profile this!
+        if profile:
+            profile.runcall(run_function_under_test)
+        else:
+            run_function_under_test()
         time_total += time() - t0
 
     if time_total:
@@ -43,8 +46,9 @@ def run_test(testfunction):
 def with_gui_setup(testfunction):
     """Add GUI setup code around a testfunction
     testfunction must be a generator (using yield)
+    returns a new generator function
     """
-    def modified_testfunction():
+    def testfunction_with_gui_setup():
         global app
         app = None
         tst = testfunction()
@@ -93,7 +97,7 @@ def with_gui_setup(testfunction):
         finally:
             os.system('rm -rf ' + tempdir)
 
-    return modified_testfunction
+    return testfunction_with_gui_setup
 
 def gui_test(f):
     "decorator to declare GUI test functions"
@@ -102,13 +106,10 @@ def gui_test(f):
 
 def nogui_test(f):
     "decorator for test functions that require no gui"
-    def run_f():
-        run_test(f)
-    all_tests[f.__name__] = run_f
+    all_tests[f.__name__] = f
     return f
 
 
-
 @gui_test
 def startup():
     yield start_measurement
@@ -187,20 +188,30 @@ def paint_rotated():
         yield res
 
 @nogui_test
-def saveload():
+def load_ora():
+    from lib import document
+    d = document.Document()
+    yield start_measurement
+    d.load('bigimage.ora')
+    yield stop_measurement
+
+@nogui_test
+def save_ora():
     from lib import document
     d = document.Document()
-    t0 = t1 = time()
     d.load('bigimage.ora')
-    print 'ora load time %.3f' % (time() - t1)
-    t1 = time()
+    yield start_measurement
     d.save('test_save.ora')
-    print 'ora save time %.3f' % (time() - t1)
-    t1 = time()
-    d.save('test_save.png')
-    print 'png save time %.3f' % (time() - t1)
+    yield stop_measurement
 
-    print 'result = %.3f' % (time() - t0)
+@nogui_test
+def save_png():
+    from lib import document
+    d = document.Document()
+    d.load('bigimage.ora')
+    yield start_measurement
+    d.save('test_save.png')
+    yield stop_measurement
 
 def scroll(zoom_func):
     dw = app.drawWindow
@@ -260,7 +271,9 @@ if __name__ == '__main__':
             # The UI startup time dominates over the actual test execution time
             # Perhaps the tests should return a function that we can execute 
             # after setting everything up?
-            cProfile.run('func()', sys.argv[3])
+            profile = cProfile.Profile()
+            run_test(func, profile)
+            profile.dump_stats(sys.argv[3])
         sys.exit(0)
 
     from optparse import OptionParser