Source

nose2 / nose2 / plugins / prof.py

try:
    import hotshot
    from hotshot import stats
except ImportError:
    hotshot, stats = None, None
import os
import tempfile

from unittest2 import Plugin


class Profiler(Plugin):
    """TODO: document"""

    configSection = 'profiler'
    commandLineSwitch = ('P', 'profile', 'Run tests under profiler')

    def __init__(self):
        self.pfile = self.config.as_str('filename', '')
        self.sort = self.config.as_str('sort', 'cumulative')
        self.restrict = self.config.as_list('restrict', [])
        self.clean = False
        self.fileno = None

    def startTestRun(self, event):
        if not hotshot:
            return
        self.createPfile()
        self.prof = hotshot.Profile(self.pfile)
        event.executeTests = self.prof.runcall

    def runProf(self, result):
        self.prof.runcall(self.suite, result)

    def beforeSummaryReport(self, event):
        if not hotshot:
            return
        # write prof output to stream
        class Stream:
            def write(self, *msg):
                for m in msg:
                    event.message(unicode(m), (0, 1, 2))
                    event.message(u' ', (0, 1, 2))
        stream = Stream()
        self.prof.close()
        prof_stats = stats.load(self.pfile)
        prof_stats.sort_stats(self.sort)
        result = event.result
        if hasattr(result, 'separator1'):
            event.message(result.separator1, (0, 1, 2))
        event.message("\nProfiling results\n", (0, 1, 2))
        if hasattr(result, 'separator2'):
            event.message(result.separator2, (0, 1, 2))
        compat_25 = hasattr(prof_stats, 'stream')
        if compat_25:
            tmp = prof_stats.stream
            prof_stats.stream = stream
        else:
            tmp = sys.stdout
            sys.stdout = stream
        try:
            if self.restrict:
                prof_stats.print_stats(*self.restrict)
            else:
                prof_stats.print_stats()
        finally:
            if compat_25:
                prof_stats.stream = tmp
            else:
                sys.stdout = tmp
        self.prof.close()
        event.message("\n", (0, 1, 2))

        if self.clean:
            if self.fileno:
                try:
                    os.close(self.fileno)
                except OSError:
                    pass
            try:
                os.unlink(self.pfile)
            except OSError:
                pass

    def createPfile(self):
        if not self.pfile:
            self.fileno, self.pfile = tempfile.mkstemp()
            self.clean = True
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.