Coverage for fundamentals/nose2_plugins/cprof.py : 0%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1'''
2Profile test execution using cProfile.
3Config option ``sort`` can be used to change how profiling information
4is presented.
5'''
6__all__ = ('Profiler',)
8import logging
9import cProfile
10import pstats
11from pyprof2calltree import convert, visualize
12import nose2
13import os
15os.environ['PATH'] += ':/usr/local/bin'
17log = logging.getLogger('.'.join(('nose2', 'plugins', __package__)))
20class Profiler(nose2.events.Plugin):
21 '''Profile the test run using cProfile'''
23 configSection = 'profiler'
24 commandLineSwitch = ('P', 'profile', 'Run test under cprofile')
26 def __init__(self):
27 self.sort = self.config.as_str('sort', 'cumulative')
28 self.prof = None
29 self.pfile = self.config.as_str('filename', '')
30 self.cachegrind = self.config.as_str('cachegrind', '')
32 def startTestRun(self, event):
33 '''Setup profiler'''
34 self.prof = cProfile.Profile()
35 event.executeTests = self.prof.runcall
37 def beforeSummaryReport(self, event):
38 '''Output profiling results'''
39 self.prof.disable()
40 stats = pstats.Stats(self.prof, stream=event.stream).sort_stats(
41 self.sort)
42 event.stream.writeln(nose2.util.ln('Profiling results'))
43 stats.print_stats()
44 if self.pfile:
45 stats.dump_stats(self.pfile)
46 if self.cachegrind:
47 visualize(self.prof.getstats())