Ejemplo n.º 1
0
    def test_sort_columns(self):
        from repoze.profile.decorator import profile

        @profile('test sort columns', sort_columns=('time', ))
        def do_nothing():
            pass

        out = StringIO()
        old_out = sys.stdout
        sys.stdout = out
        do_nothing()
        assert 'test sort columns' in out.getvalue()
        assert 'Ordered by: internal time' in out.getvalue()
        sys.stdout = old_out
Ejemplo n.º 2
0
    def test_limit_lines(self):
        from repoze.profile.decorator import profile

        @profile('test limit lines', lines=1)
        def do_nothing():
            pass

        out = StringIO()
        old_out = sys.stdout
        sys.stdout = out
        do_nothing()
        assert 'test limit lines' in out.getvalue()
        assert '1 due to restriction' in out.getvalue()
        sys.stdout = old_out
Ejemplo n.º 3
0
    def test_it(self):
        from repoze.profile.decorator import profile

        @profile('test function')
        def do_nothing():
            pass

        out = StringIO()
        old_out = sys.stdout
        sys.stdout = out
        do_nothing()
        assert 'test function' in out.getvalue()
        assert 'Ordered by: cumulative time' in out.getvalue()
        sys.stdout = old_out
Ejemplo n.º 4
0
    def index(self, request, output=None):  # output=None D/I for testing
        querydata = request.get_params()
        fulldirs = int(querydata.get('fulldirs', 0))
        sort = querydata.get('sort', 'time')
        clear = querydata.get('clear', None)
        filename = querydata.get('filename', '').strip()
        limit = int(querydata.get('limit', 100))
        mode = querydata.get('mode', 'stats')
        if output is None:
            output = StringIO()
        url = request.get_url()
        log_exists = os.path.exists(self.log_filename)

        if clear and log_exists:
            os.remove(self.log_filename)
            self.profiler = profile.Profile()
            log_exists = False

        if log_exists:
            stats = self.Stats(self.log_filename)  # D/I
            if not fulldirs:
                stats.strip_dirs()
            stats.sort_stats(sort)
            if hasattr(stats, 'stream'):
                # python 2.5
                stats.stream = output
            try:
                orig_stdout = sys.stdout  # python 2.4
                sys.stdout = output
                print_fn = getattr(stats, 'print_%s' % mode)
                if filename:
                    print_fn(filename, limit)
                else:
                    print_fn(limit)
            finally:
                sys.stdout = orig_stdout

        profiledata = output.getvalue()
        description = empty_description
        action = url
        formelements = ''
        filename = filename or ''
        if profiledata:
            description = """
            Profiling information is generated using the standard Python 
            profiler. To learn how to interpret the profiler statistics, 
            see the <a
            href="http://www.python.org/doc/current/lib/module-profile.html">
            Python profiler documentation</a>."""
            sort_repl = '<option value="%s">' % sort
            sort_selected = '<option value="%s" selected>' % sort
            sort = sort_tmpl.replace(sort_repl, sort_selected)
            limit_repl = '<option value="%s">' % limit
            limit_selected = '<option value="%s" selected>' % limit
            limit = limit_tmpl.replace(limit_repl, limit_selected)
            mode_repl = '<option value="%s">' % mode
            mode_selected = '<option value="%s" selected>' % mode
            mode = mode_tmpl.replace(mode_repl, mode_selected)
            fulldirs_checked = '/>'
            fulldirs_repl = '/>'
            if fulldirs:
                fulldirs_checked = 'checked/>'
            fulldirs = fulldirs_tmpl.replace(fulldirs_repl, fulldirs_checked)
            filename_repl = 'value=""'
            filename_selected = 'value="%s"' % filename
            filename = filename_tmpl.replace(filename_repl, filename_selected)
            fulldirs_repl
            formelements = string.Template(formelements_tmpl)
            formelements = formelements.substitute({
                'description': description,
                'action': action,
                'sort': sort,
                'limit': limit,
                'fulldirs': fulldirs,
                'mode': mode,
                'filename': filename,
            })
        index = string.Template(index_tmpl)
        index = index.substitute({
            'formelements': formelements,
            'action': action,
            'description': description,
            'profiledata': profiledata,
        })
        return index