1
2
3
4
5
6
7
8
9 """Helper module to enable profiling of the testcase
10
11 If environment variable PROFILELEVEL is set it uses hotshot profiler
12 for unittest.main() call. Value of PROFILELEVEL defines number of top
13 busy functions to report.
14
15 Environment variable PROFILELINES=1 makes hotshot store information
16 per each line, so it could be easily inspected later on.
17
18 Output:
19 Profiler stores its Stats into a file named after original script
20 (sys.argv[0]) with suffix".prof" appended
21
22 Usage:
23 Replace unittest.main() with import runner
24
25 Visualization:
26 kcachegrind provides nice interactive GUI to inspect profiler
27 results. If PROFILELINES was set to 1, it provides information per
28 each line.
29
30 To convert .prof file into a file suitable for kcachegrind, use
31 utility hotshot2calltree which comes in package
32 kcachegrind-converters.
33
34 Example:
35
36 # profile and output 3 most expensive function calls
37 PROFILELEVEL=3 PROFILELINES=1 PYTHONPATH=../ python test_searchlight.py
38 # convert to kcachegrind format
39 hotshot2calltree -o test_searchlight.py.kcache test_searchlight.py.prof
40 # inspect
41 kcachegrind test_searchlight.py.kcache
42
43 """
44
45 import unittest
46 import sys
47
48 from os import environ
49
50 from mvpa import _random_seed
51 profilelevel = None
52
53 if environ.has_key('PROFILELEVEL'):
54 profilelevel = int(environ['PROFILELEVEL'])
55
56
63
64 if profilelevel is None:
65 TestProgramPyMVPA()
66 else:
67 profilelines = environ.has_key('PROFILELINES')
68
69 import hotshot, hotshot.stats
70 pname = "%s.prof" % sys.argv[0]
71 prof = hotshot.Profile(pname, lineevents=profilelines)
72 try:
73
74
75 benchtime, stones = prof.runcall( unittest.main )
76 except SystemExit:
77 pass
78 print "Saving profile data into %s" % pname
79 prof.close()
80 if profilelevel > 0:
81
82
83 print "Loading profile data from %s" % pname
84 stats = hotshot.stats.load(pname)
85 stats.strip_dirs()
86 stats.sort_stats('time', 'calls')
87 stats.print_stats(profilelevel)
88