Exemplo n.º 1
0
    def __call__(self, environ, start_response):
        request = MiniRequest(environ)

        if request.path_info == self.path:
            # we're being asked to render the profile view
            self.lock.acquire()
            try:
                text = self.index(request)
            finally:
                self.lock.release()
            start_response('200 OK',
                           [('content-type', 'text/html; charset="UTF-8"'),
                            ('content-length', str(len(text)))])
            return [bytes_(text)]

        self.lock.acquire()
        try:
            _locals = locals()
            code = self.unwind and PROFILE_EXEC_EAGER or PROFILE_EXEC_LAZY
            self.profiler.runctx(code, globals(), _locals)

            if self.first_request:  # discard to avoid timing warm-up
                self.profiler = profile.Profile()
                self.first_request = False
            else:
                self.profiler.dump_stats(self.log_filename)
                if HAS_PP2CT and self.cachegrind_filename is not None:
                    stats = pstats.Stats(self.profiler)
                    conv = pyprof2calltree.CalltreeConverter(stats)
                    with open(self.cachegrind_filename, 'w') as grind:
                        conv.output(grind)
            app_iter = _locals['app_iter_']
            return app_iter
        finally:
            self.lock.release()
Exemplo n.º 2
0
    def save_data(self):
        try:
            import gprof2dot
            import pyprof2calltree
        except ImportError:
            msg = ('Unable to start profiling.\n Please either '
                   'disable performance profiling in settings.yaml or '
                   'install all modules listed in test-requirements.txt.')
            raise error.ProfilingError(msg)

        self.profiler.disable()
        elapsed = time.time() - self.start
        pref_filename = os.path.join(
            self.paths['last_performance_test'],
            '{method:s}.{handler_name:s}.{elapsed_time:.0f}ms.{t_time}.'.
            format(method=self.method,
                   handler_name=self.handler_name or 'root',
                   elapsed_time=elapsed * 1000.0,
                   t_time=time.time()))
        tree_file = pref_filename + 'prof'
        stats_file = pref_filename + 'txt'
        callgraph_file = pref_filename + 'dot'

        # write pstats
        with file(stats_file, 'w') as file_o:
            stats = Stats(self.profiler, stream=file_o)
            stats.sort_stats('time', 'cumulative').print_stats()

        # write callgraph in dot format
        parser = gprof2dot.PstatsParser(self.profiler)

        def get_function_name((filename, line, name)):
            module = os.path.splitext(filename)[0]
            module_pieces = module.split(os.path.sep)
            return "{module:s}:{line:d}:{name:s}".format(module="/".join(
                module_pieces[-4:]),
                                                         line=line,
                                                         name=name)

        parser.get_function_name = get_function_name
        gprof = parser.parse()

        with open(callgraph_file, 'w') as file_o:
            dot = gprof2dot.DotWriter(file_o)
            theme = gprof2dot.TEMPERATURE_COLORMAP
            dot.graph(gprof, theme)

        # write calltree
        call_tree = pyprof2calltree.CalltreeConverter(stats)
        with file(tree_file, 'wb') as file_o:
            call_tree.output(file_o)
Exemplo n.º 3
0
        def decorator(*args, **kwargs):
            result = None
            current_thread = threading.current_thread()
            if cumulative:
                if profile_per_thread:
                    tls = threading.local()
                    if not hasattr(tls, 'profiler'):
                        tls.profiler = Profile()
                    profiler = tls.profiler
                else:
                    global profiler
            else:
                profiler = Profile()
            try:
                result = profiler.runcall(func, *args, **kwargs)
            finally:
                if dump_stats:
                    if generate_profile:
                        if profile_per_thread:
                            profile_path = os.path.join(
                                dump_dir, '%s.%s' %
                                (current_thread.name, profile_filename))
                        else:
                            profile_path = os.path.join(
                                dump_dir, profile_filename)
                        profiler.dump_stats(profile_path)

                stats = pstats.Stats(profiler)
                conv = pyprof2calltree.CalltreeConverter(stats)
                if generate_callgrind:
                    if profile_per_thread:
                        callgrind_path = os.path.join(
                            dump_dir, '%s.%s' %
                            (current_thread.name, callgrind_filename))
                    else:
                        callgrind_path = os.path.join(dump_dir,
                                                      callgrind_filename)

                    with open(callgrind_path, 'w') as fd:
                        conv.output(fd)
                if print_stats:
                    stats.strip_dirs().sort_stats(sort_stats).print_stats(
                        print_stats)
            return result
 def decorator(*args, **kwargs):
     result = None
     if cumulative:
         global profiler
     else:
         profiler = Profile()
     try:
         result = profiler.runcall(func, *args, **kwargs)
     finally:
         if dump_stats:
             profiler.dump_stats(profile_filename)
         stats = pstats.Stats(profiler)
         conv = pyprof2calltree.CalltreeConverter(stats)
         with open(callgrind_filename, 'w') as fd:
             conv.output(fd)
         if print_stats:
             stats.strip_dirs().sort_stats(sort_stats).print_stats(
                 print_stats)
     return result
Exemplo n.º 5
0
    def __call__(self, environ, start_response):
        path_info = environ.get('PATH_INFO')

        if path_info == self.path:
            # we're being asked to render the profile view
            self.lock.acquire()
            try:
                text = self.index(environ)
            finally:
                self.lock.release()
            start_response('200 OK', [('content-type', 'text/html'),
                                      ('content-length', str(len(text)))])
            return [text]

        self.lock.acquire()
        try:
            _locals = locals()
            self.profiler.runctx(
                'app_iter = self.app(environ, start_response); '
                'app_iter = (type(app_iter) is GeneratorType) '
                                 'and list(app_iter) or app_iter',
                globals(), _locals)

            if self.first_request: # discard to avoid timing warm-up
                self.profiler = profile.Profile()
                self.first_request = False
            else:
                self.profiler.dump_stats(self.log_filename)
                if HAS_PP2CT and self.cachegrind_filename is not None:
                    stats = pstats.Stats(self.profiler)
                    conv = pyprof2calltree.CalltreeConverter(stats)
                    grind = None
                    try:
                        grind = file(self.cachegrind_filename, 'wb')
                        conv.output(grind)
                    finally:
                        if grind is not None:
                            grind.close()

            app_iter = _locals['app_iter']
            return app_iter
        finally:
            self.lock.release()
Exemplo n.º 6
0
    def save_data(self):
        elapsed = time.time() - self.start
        pref_filename = os.path.join(
            settings.LOAD_TESTS_PATHS['last_performance_test'],
            '{method:s}.{handler_name:s}.{elapsed_time:.0f}ms.{t_time}.'.
            format(
                method=self.method,
                handler_name=self.handler_name or 'root',
                elapsed_time=elapsed * 1000.0,
                t_time=time.time()))
        tree_file = pref_filename + 'prof'
        stats_file = pref_filename + 'txt'
        callgraph_file = pref_filename + 'dot'

        # write pstats
        with file(stats_file, 'w') as file_o:
            stats = Stats(self.profiler, stream=file_o)
            stats.sort_stats('time', 'cumulative').print_stats()

        # write callgraph in dot format
        parser = gprof2dot.PstatsParser(self.profiler)

        def get_function_name((filename, line, name)):
            module = os.path.splitext(filename)[0]
            module_pieces = module.split(os.path.sep)
            return "{module:s}:{line:d}:{name:s}".format(
                module="/".join(module_pieces[-4:]),
                line=line,
                name=name)

        parser.get_function_name = get_function_name
        gprof = parser.parse()

        with open(callgraph_file, 'w') as file_o:
            dot = gprof2dot.DotWriter(file_o)
            theme = gprof2dot.TEMPERATURE_COLORMAP
            dot.graph(gprof, theme)

        # write calltree
        call_tree = pyprof2calltree.CalltreeConverter(stats)
        with file(tree_file, 'wb') as file_o:
            call_tree.output(file_o)
Exemplo n.º 7
0
 def do_kcachegrind(self, line):
     if not self.stats:
         print >> self.stream, "Need to load profile at first."
         return
     import pyprof2calltree
     conv = pyprof2calltree.CalltreeConverter(self.stats)
     grind = None
     ts = time.time()
     tmp_cachegrind_file = tempfile.mkstemp('.profile', 'cachegrind-')
     print >> self.stream, tmp_cachegrind_file
     try:
         #grind = open(tmp_cachegrind_file, 'wb')
         conv.output(tmp_cachegrind_file)
         subprocess.call(['kcachegrind', tmp_cachegrind_file])
         os.remove(tmp_cachegrind_file)
     except TypeError:
         print >> self.stream, "Can not convert into grind format."
     except OSError:
         print >> self.stream, "Can not launch kcachegrind. please verify it has been installed."
     except Exception as ex:
         print >> self.stream, "Error:", type(ex)
     finally:
         if grind is not None:
             grind.close()