当前位置: 首页>>代码示例>>Python>>正文


Python LineProfiler.get_stats方法代码示例

本文整理汇总了Python中line_profiler.LineProfiler.get_stats方法的典型用法代码示例。如果您正苦于以下问题:Python LineProfiler.get_stats方法的具体用法?Python LineProfiler.get_stats怎么用?Python LineProfiler.get_stats使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在line_profiler.LineProfiler的用法示例。


在下文中一共展示了LineProfiler.get_stats方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ProfilingPanel

# 需要导入模块: from line_profiler import LineProfiler [as 别名]
# 或者: from line_profiler.LineProfiler import get_stats [as 别名]
class ProfilingPanel(Panel):
    """
    Panel that displays profiling information.
    """
    title = _('Profiling')

    template = 'debug_toolbar_line_profiler/panels/profiling.html'

    def _unwrap_closure_and_profile(self, func):
        if not hasattr(func, '__code__'):
            return
        self.line_profiler.add_function(func)
        for subfunc in getattr(func, 'profile_additional', []):
            self._unwrap_closure_and_profile(subfunc)
        if func.__closure__:
            for cell in func.__closure__:
                target = cell.cell_contents
                if inspect.isclass(target) and View in inspect.getmro(target):
                    for name, value in inspect.getmembers(target):
                        if name[0] != '_' and inspect.ismethod(value):
                            self._unwrap_closure_and_profile(value)
                else:
                    self._unwrap_closure_and_profile(target)

    def process_view(self, request, view_func, view_args, view_kwargs):
        self.profiler = cProfile.Profile()
        args = (request,) + view_args
        self.line_profiler = LineProfiler()
        self._unwrap_closure_and_profile(view_func)
        self.line_profiler.enable_by_count()
        out = self.profiler.runcall(view_func, *args, **view_kwargs)
        self.line_profiler.disable_by_count()
        return out

    def add_node(self, func_list, func, max_depth, cum_time=0.1):
        func_list.append(func)
        func.has_subfuncs = False
        if func.depth < max_depth:
            for subfunc in func.subfuncs():
                if (subfunc.stats[3] >= cum_time or
                        (hasattr(self.stats, 'line_stats') and
                            (subfunc.func in self.stats.line_stats.timings))):
                    func.has_subfuncs = True
                    self.add_node(func_list, subfunc, max_depth, cum_time=cum_time)

    def process_response(self, request, response):
        if not hasattr(self, 'profiler'):
            return None
        # Could be delayed until the panel content is requested (perf. optim.)
        self.profiler.create_stats()
        self.stats = DjangoDebugToolbarStats(self.profiler)
        self.stats.line_stats = self.line_profiler.get_stats()
        self.stats.calc_callees()

        root = FunctionCall(self.stats, self.stats.get_root_func(), depth=0)

        func_list = []
        self.add_node(func_list, root, 10, root.stats[3] / 8)

        self.record_stats({'func_list': func_list})
开发者ID:ariesfath,项目名称:django-debug-toolbar-line-profiler,代码行数:62,代码来源:panel.py

示例2: ProfilingDebugPanel

# 需要导入模块: from line_profiler import LineProfiler [as 别名]
# 或者: from line_profiler.LineProfiler import get_stats [as 别名]
class ProfilingDebugPanel(DebugPanel):
    """
    Panel that displays the Django version.
    """
    name = 'Profiling'
    template = 'debug_toolbar/panels/profiling.html'
    has_content = True
    
    def nav_title(self):
        return _('Profiling')
    
    def url(self):
        return ''
    
    def title(self):
        return _('Profiling')
    
    def _unwrap_closure_and_profile(self, func):
        if not hasattr(func, 'func_code'):
            return
        self.line_profiler.add_function(func)
        if func.func_closure:
            for cell in func.func_closure:
                if hasattr(cell.cell_contents, 'func_code'):
                    self._unwrap_closure_and_profile(cell.cell_contents)
    
    def process_view(self, request, view_func, view_args, view_kwargs):
        __traceback_hide__ = True
        self.profiler = cProfile.Profile()
        args = (request,) + view_args
        if DJ_PROFILE_USE_LINE_PROFILER:
            self.line_profiler = LineProfiler()
            self._unwrap_closure_and_profile(view_func)
            self.line_profiler.enable_by_count()
            out = self.profiler.runcall(view_func, *args, **view_kwargs)
            self.line_profiler.disable_by_count()
        else:
            self.line_profiler = None
            out = self.profiler.runcall(view_func, *args, **view_kwargs)
        return out
    
    def add_node(self, func_list, func, max_depth, cum_time=0.1):
        func_list.append(func)
        func.has_subfuncs = False
        if func.depth < max_depth:
            for subfunc in func.subfuncs():
                if (subfunc.stats[3] >= cum_time or
                   (hasattr(self.stats, 'line_stats') and
                   (subfunc.func in self.stats.line_stats.timings))):
                    func.has_subfuncs = True
                    self.add_node(func_list, subfunc, max_depth, cum_time=cum_time)
    
    def process_response(self, request, response):
        self.profiler.create_stats()
        self.stats = DjangoDebugToolbarStats(self.profiler)
        if DJ_PROFILE_USE_LINE_PROFILER:
            self.stats.line_stats = self.line_profiler.get_stats()
        self.stats.calc_callees()
        
        root = FunctionCall(self.stats, self.stats.get_root_func(), depth=0)
        
        func_list = []
        self.add_node(func_list, root, 10, root.stats[3]/8)
        
        self.stats_record({'func_list': func_list})
开发者ID:colinhowe,项目名称:django-debug-toolbar,代码行数:67,代码来源:profiling.py

示例3: ProfilingPanel

# 需要导入模块: from line_profiler import LineProfiler [as 别名]
# 或者: from line_profiler.LineProfiler import get_stats [as 别名]
class ProfilingPanel(Panel):
    """
    Panel that displays profiling information.
    """
    title = _('Profiling')

    template = 'debug_toolbar_line_profiler/panels/profiling.html'

    def _unwrap_closure_and_profile(self, func):
        if not hasattr(func, '__code__'):
            return
        self.line_profiler.add_function(func)
        for subfunc in getattr(func, 'profile_additional', []):
            self._unwrap_closure_and_profile(subfunc)
        if PY2:
            func_closure = func.func_closure
        else:
            func_closure = func.__closure__

        if func_closure:
            for cell in func_closure:
                target = cell.cell_contents
                if hasattr(target, '__code__'):
                    self._unwrap_closure_and_profile(cell.cell_contents)
                if inspect.isclass(target) and View in inspect.getmro(target):
                    for name, value in inspect.getmembers(target):
                        if name[0] != '_' and inspect.ismethod(value):
                            self._unwrap_closure_and_profile(value)

    def process_view(self, request, view_func, view_args, view_kwargs):
        self.view_func = view_func
        self.profiler = cProfile.Profile()
        args = (request,) + view_args
        self.line_profiler = LineProfiler()
        self._unwrap_closure_and_profile(view_func)
        signals.profiler_setup.send(sender=self,
                                    profiler=self.line_profiler,
                                    view_func=view_func,
                                    view_args=view_args,
                                    view_kwargs=view_kwargs)
        self.line_profiler.enable_by_count()
        out = self.profiler.runcall(view_func, *args, **view_kwargs)
        self.line_profiler.disable_by_count()
        return out

    def add_node(self, func_list, func, max_depth, cum_time=0.1):
        """
        add_node does a depth first traversal of the call graph, appending a
        FunctionCall object to func_list, so that the Django template only
        has to do a single for loop over func_list that can render a tree
        structure

        Parameters:
            func_list is an array that will have a FunctionCall for each call
                added to it
            func is a FunctionCall object that will have all its callees added
            max_depth is the maximum depth we should recurse
            cum_time is the minimum cum_time a function should have to be
                included in the output
        """
        func_list.append(func)
        func.has_subfuncs = False
        # this function somewhat dangerously relies on FunctionCall to set its
        # subfuncs' depth argument correctly
        if func.depth >= max_depth:
            return

        # func.subfuncs returns FunctionCall objects
        subs = sorted(func.subfuncs(), key=FunctionCall.cumtime, reverse=True)
        for subfunc in subs:
            # a sub function is important if it takes a long time or it has
            # line_stats
            if (subfunc.cumtime() >= cum_time or
                    (hasattr(self.stats, 'line_stats') and
                     subfunc.func in self.stats.line_stats.timings)):
                func.has_subfuncs = True
                self.add_node(
                    func_list=func_list,
                    func=subfunc,
                    max_depth=max_depth,
                    cum_time=subfunc.cumtime()/16)

    def process_response(self, request, response):
        if not hasattr(self, 'profiler'):
            return None
        # Could be delayed until the panel content is requested (perf. optim.)
        self.profiler.create_stats()
        self.stats = DjangoDebugToolbarStats(self.profiler)
        self.stats.line_stats = self.line_profiler.get_stats()
        self.stats.calc_callees()

        func_list = []
        root_func = self.stats.get_root_func(self.view_func)

        if root_func is not None:
            root_node = FunctionCall(statobj=self.stats,
                                     func=root_func,
                                     depth=0)
            self.add_node(
                func_list=func_list,
#.........这里部分代码省略.........
开发者ID:gizmag,项目名称:django-debug-toolbar-line-profiler,代码行数:103,代码来源:panel.py


注:本文中的line_profiler.LineProfiler.get_stats方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。