Ejemplo n.º 1
0
 def __init__(self, monitor_pid, interval, pipe, *args, include_children=False, **kw):
     super().__init__(*args, **kw)
     self.monitor_pid = monitor_pid
     self.interval = interval
     self.pipe = pipe
     self.include_children = include_children
     try:
         self.backend = choose_backend('psutil_uss')
     except KeyError:
         self.backend = choose_backend()
Ejemplo n.º 2
0
def memory_profiler(_function=None,
                    name='',
                    print_res=True,
                    stream=None,
                    precision=1,
                    backend='psutil'):
    """
    内存分析器装饰器

    :param _function: 被封装的对象,由解释器自动传入,不需关心
    :type _function: types.FunctionType or types.MethodType
    :param str name: 关键字参数,被装饰方法所使用的内存分析器名称,默认为使用被装饰方法的方法名
    :param bool print_res: 是否在被装饰对象退出后立刻打印分析结果,默认为 True 。
        当需要将多次调用结果聚集后输出时,可设为 False ,并通过 Mixin 中的 memory_profiler 进行结果输出
    :param object stream: 输出方式,默认为 stdout ,可指定为文件
    :param int precision: 精度,默认为 1
    :param str backend: 内存监控的 backend ,默认为 'psutil'
    :return: 装饰后的函数或方法
    :rtype: types.FunctionType or types.MethodType
    """
    invoked = bool(_function and callable(_function))
    if invoked:
        func = _function  # type: types.FunctionType or types.MethodType

    backend = choose_backend(backend)
    if backend == 'tracemalloc' and has_tracemalloc:  # pragma: no cover
        if not tracemalloc.is_tracing():  # pragma: no cover
            tracemalloc.start()  # pragma: no cover

    def wrapper(func):
        """
        装饰器封装函数
        """
        @wraps(func)
        def inner(*args, **kwargs):
            """
            将被封装方法使用 LineProfiler 进行封装
            """
            if not (args and base.is_instance_or_subclass(
                    args[0], MemoryProfilerMixin)):
                # 若当前被装饰的方法未继承 MemoryProfilerMixin ,则将其作为普通函数装饰
                lp = MemoryProfiler(backend=backend)
            else:
                self_or_cls = args[0]  # type: MemoryProfilerMixin
                _name = name or func
                lp = self_or_cls.memory_profiler(_name, raise_except=False)

            profiler_wrapper = lp(func)
            res = profiler_wrapper(*args, **kwargs)
            if print_res:
                lp.print_stats(stream=stream, precision=precision)
            return res

        return inner

    return wrapper if not invoked else wrapper(func)
Ejemplo n.º 3
0
def my_profiler(func=None, stream=None, precision=1, backend='psutil'):
    backend = memory_profiler.choose_backend(backend)

    @wraps(func)
    def wrapper(*args, **kwargs):
        prof = memory_profiler.LineProfiler(backend=backend)
        prof(func)(*args, **kwargs)
        return prof

    return wrapper
Ejemplo n.º 4
0
    def __enter__(self):
        backend = choose_backend()

        self.child_conn, self.parent_conn = Pipe()  # this will store MemTimer's results
        self.p = MemTimer(os.getpid(), self.interval, self.child_conn, self.filename,
                          backend=backend, timestamps=self.timestamps, max_usage=self.max_usage,
                          include_children=self.include_children)
        self.p.start()
        self.parent_conn.recv()  # wait until memory logging in subprocess is ready

        return self
Ejemplo n.º 5
0
def _process_backend(backend='psutil'):
    """
    处理内存分析器的后端

    :param str backend: 内存监控的 backend ,默认为 'psutil'
    :return: 处理后的后端名称
    :rtype: str
    """
    backend = choose_backend(backend)
    if backend == 'tracemalloc' and has_tracemalloc and \
       not tracemalloc.is_tracing():  # pragma: no cover
        tracemalloc.start()
    return backend