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


Python tracemalloc.take_snapshot方法代码示例

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


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

示例1: print_memory_snapshot

# 需要导入模块: import tracemalloc [as 别名]
# 或者: from tracemalloc import take_snapshot [as 别名]
def print_memory_snapshot(extra_str=None):
    import os
    import tracemalloc
    log_file = os.getenv("SparseSC_log_file") #None if non-existant
    snapshot = tracemalloc.take_snapshot()
    top_stats = snapshot.statistics('lineno')
    if log_file is not None:
        log_file = open(log_file, "a")
    if extra_str is not None:
        print(extra_str, file=log_file)
    limit=10
    print("[ Top 10 ] ", file=log_file)
    for stat in top_stats[:limit]:
        print(stat, file=log_file)
    other = top_stats[limit:]
    if other:
        size = sum(stat.size for stat in other)
        print("%s other: %.1f KiB" % (len(other), size / 1024), file=log_file)
    total = sum(stat.size for stat in top_stats)
    print("Total allocated size: %.1f KiB" % (total / 1024), file=log_file)
    #if old_snapshot is not None:
    #    diff_stats = snapshot.compare_to(old_snapshot, 'lineno')
    if log_file is not None:
        log_file.close() 
开发者ID:microsoft,项目名称:SparseSC,代码行数:26,代码来源:print_progress.py

示例2: profile_main

# 需要导入模块: import tracemalloc [as 别名]
# 或者: from tracemalloc import take_snapshot [as 别名]
def profile_main():
    """

    :return:
    """
    log.info("Profiling: ENABLED")
    # Enable memory usage profiling at the line level
    tracemalloc.start()
    # Enable CPU usage/function call timing/rate at the function level
    # Automatigically dumps profile to `filename` for further analysis
    cProfile.run("main()", filename=(CWD + "/chimay-red.cprof"))
    # Take snapshot of traced malloc profile
    snapshot = tracemalloc.take_snapshot()
    # Print snapshot statistics filtering for only `tracefiles`
    display_top(snapshot, limit=20, modpaths=TRACEFILES)

    return 0 
开发者ID:seekintoo,项目名称:Chimay-Red,代码行数:19,代码来源:chimay_red.py

示例3: tracemalloc_dump

# 需要导入模块: import tracemalloc [as 别名]
# 或者: from tracemalloc import take_snapshot [as 别名]
def tracemalloc_dump() -> None:
    if not tracemalloc.is_tracing():
        logger.warning("pid %s: tracemalloc off, nothing to dump",
                       os.getpid())
        return
    # Despite our name for it, `timezone_now` always deals in UTC.
    basename = "snap.{}.{}".format(os.getpid(),
                                   timezone_now().strftime("%F-%T"))
    path = os.path.join(settings.TRACEMALLOC_DUMP_DIR, basename)
    os.makedirs(settings.TRACEMALLOC_DUMP_DIR, exist_ok=True)

    gc.collect()
    tracemalloc.take_snapshot().dump(path)

    with open(f'/proc/{os.getpid()}/stat', 'rb') as f:
        procstat = f.read().split()
    rss_pages = int(procstat[23])
    logger.info("tracemalloc dump: tracing %s MiB (%s MiB peak), using %s MiB; rss %s MiB; dumped %s",
                tracemalloc.get_traced_memory()[0] // 1048576,
                tracemalloc.get_traced_memory()[1] // 1048576,
                tracemalloc.get_tracemalloc_memory() // 1048576,
                rss_pages // 256,
                basename) 
开发者ID:zulip,项目名称:zulip,代码行数:25,代码来源:debug.py

示例4: test_trace

# 需要导入模块: import tracemalloc [as 别名]
# 或者: from tracemalloc import take_snapshot [as 别名]
def test_trace():
    import tracemalloc

    tracemalloc.start(10)
    time1 = tracemalloc.take_snapshot()

    import pycorrector

    c = pycorrector.correct('少先队员因该为老人让坐')
    print(c)

    time2 = tracemalloc.take_snapshot()
    stats = time2.compare_to(time1, 'lineno')
    print('*' * 32)
    for stat in stats[:3]:
        print(stat)

    stats = time2.compare_to(time1, 'traceback')
    print('*' * 32)
    for stat in stats[:3]:
        print(stat.traceback.format()) 
开发者ID:shibing624,项目名称:pycorrector,代码行数:23,代码来源:memory_test.py

示例5: test_snapshot

# 需要导入模块: import tracemalloc [as 别名]
# 或者: from tracemalloc import take_snapshot [as 别名]
def test_snapshot(self):
        obj, source = allocate_bytes(123)

        # take a snapshot
        snapshot = tracemalloc.take_snapshot()

        # write on disk
        snapshot.dump(support.TESTFN)
        self.addCleanup(support.unlink, support.TESTFN)

        # load from disk
        snapshot2 = tracemalloc.Snapshot.load(support.TESTFN)
        self.assertEqual(snapshot2.traces, snapshot.traces)

        # tracemalloc must be tracing memory allocations to take a snapshot
        tracemalloc.stop()
        with self.assertRaises(RuntimeError) as cm:
            tracemalloc.take_snapshot()
        self.assertEqual(str(cm.exception),
                         "the tracemalloc module must be tracing memory "
                         "allocations to take a snapshot") 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:23,代码来源:test_tracemalloc.py

示例6: test_create_snapshot

# 需要导入模块: import tracemalloc [as 别名]
# 或者: from tracemalloc import take_snapshot [as 别名]
def test_create_snapshot(self):
        raw_traces = [(5, (('a.py', 2),))]

        with contextlib.ExitStack() as stack:
            stack.enter_context(patch.object(tracemalloc, 'is_tracing',
                                             return_value=True))
            stack.enter_context(patch.object(tracemalloc, 'get_traceback_limit',
                                             return_value=5))
            stack.enter_context(patch.object(tracemalloc, '_get_traces',
                                             return_value=raw_traces))

            snapshot = tracemalloc.take_snapshot()
            self.assertEqual(snapshot.traceback_limit, 5)
            self.assertEqual(len(snapshot.traces), 1)
            trace = snapshot.traces[0]
            self.assertEqual(trace.size, 5)
            self.assertEqual(len(trace.traceback), 1)
            self.assertEqual(trace.traceback[0].filename, 'a.py')
            self.assertEqual(trace.traceback[0].lineno, 2) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:21,代码来源:test_tracemalloc.py

示例7: main

# 需要导入模块: import tracemalloc [as 别名]
# 或者: from tracemalloc import take_snapshot [as 别名]
def main():
    print("Warming up (to discount regex cache etc.)")
    run()

    tracemalloc.start(25)
    gc.collect()
    snapshot1 = tracemalloc.take_snapshot()
    run()
    gc.collect()
    snapshot2 = tracemalloc.take_snapshot()

    top_stats = snapshot2.compare_to(snapshot1, 'traceback')

    print("Objects not released")
    print("====================")
    for stat in top_stats:
        if "tracemalloc.py" in str(stat) or stat.size_diff == 0:
            continue
        print(stat)
        for line in stat.traceback.format():
            print("    ", line)

    print("\nquickjs should not show up above.") 
开发者ID:PetterS,项目名称:quickjs,代码行数:25,代码来源:check_memory.py

示例8: test

# 需要导入模块: import tracemalloc [as 别名]
# 或者: from tracemalloc import take_snapshot [as 别名]
def test(self):
        timings = []
        memory_usage = []
        tracemalloc.start()

        for i in range(self.repeat):
            before_memory = tracemalloc.take_snapshot()
            start_time = time.time()

            self.bench()

            end_time = time.time()
            after_memory = tracemalloc.take_snapshot()
            timings.append(end_time - start_time)
            memory_usage.append(sum([t.size for t in after_memory.compare_to(before_memory, 'filename')]))

        print("time min:", min(timings), "max:", max(timings), "avg:", sum(timings) / len(timings))  # NOQA
        print("memory min:", min(memory_usage), "max:", max(memory_usage), "avg:", sum(memory_usage) / len(memory_usage))  # NOQA 
开发者ID:wagtail,项目名称:wagtail,代码行数:20,代码来源:benchmark.py

示例9: snapshot

# 需要导入模块: import tracemalloc [as 别名]
# 或者: from tracemalloc import take_snapshot [as 别名]
def snapshot(self):
        snapshot = tracemalloc.take_snapshot()
        top_stats = snapshot.statistics('lineno')
        total = sum(stat.size for stat in top_stats)
        if abs(self.last_total - total) < self.min_change:
            return

        self.last_total = total

        self.log_stream.write("Top %s lines at %s\n" % (self.limit, time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())))
        for index, stat in enumerate(top_stats[:self.limit], 1):
            frame = stat.traceback[0]
            # replace "/path/to/module/file.py" with "module/file.py"
            filename = os.sep.join(frame.filename.split(os.sep)[-2:])
            self.log_stream.write("#%s: %s:%s: %.1f KiB\n" % (index, filename, frame.lineno, stat.size / 1024))
            line = linecache.getline(frame.filename, frame.lineno).strip()
            if line:
                self.log_stream.write('    %s\n' % line)

        other = top_stats[self.limit:]
        if other:
            size = sum(stat.size for stat in other)
            self.log_stream.write("%s other: %.1f KiB\n" % (len(other), size / 1024))
        self.log_stream.write("Total allocated size: %.1f KiB\n\n" % (total / 1024))
        self.log_stream.flush() 
开发者ID:rwth-i6,项目名称:sisyphus,代码行数:27,代码来源:tools.py

示例10: test_leak_in_transport

# 需要导入模块: import tracemalloc [as 别名]
# 或者: from tracemalloc import take_snapshot [as 别名]
def test_leak_in_transport(zipkin_url, client, loop):

    tracemalloc.start()

    endpoint = az.create_endpoint('simple_service')
    tracer = await az.create(zipkin_url, endpoint, sample_rate=1,
                             send_interval=0.0001, loop=loop)

    await asyncio.sleep(5)
    gc.collect()
    snapshot1 = tracemalloc.take_snapshot()

    await asyncio.sleep(10)
    gc.collect()
    snapshot2 = tracemalloc.take_snapshot()

    top_stats = snapshot2.compare_to(snapshot1, 'lineno')
    count = sum(s.count for s in top_stats)
    await tracer.close()
    assert count < 400  # in case of leak this number is around 901452 
开发者ID:aio-libs,项目名称:aiozipkin,代码行数:22,代码来源:test_zipkin.py

示例11: test_create_snapshot

# 需要导入模块: import tracemalloc [as 别名]
# 或者: from tracemalloc import take_snapshot [as 别名]
def test_create_snapshot(self):
        raw_traces = [(0, 5, (('a.py', 2),))]

        with contextlib.ExitStack() as stack:
            stack.enter_context(patch.object(tracemalloc, 'is_tracing',
                                             return_value=True))
            stack.enter_context(patch.object(tracemalloc, 'get_traceback_limit',
                                             return_value=5))
            stack.enter_context(patch.object(tracemalloc, '_get_traces',
                                             return_value=raw_traces))

            snapshot = tracemalloc.take_snapshot()
            self.assertEqual(snapshot.traceback_limit, 5)
            self.assertEqual(len(snapshot.traces), 1)
            trace = snapshot.traces[0]
            self.assertEqual(trace.size, 5)
            self.assertEqual(len(trace.traceback), 1)
            self.assertEqual(trace.traceback[0].filename, 'a.py')
            self.assertEqual(trace.traceback[0].lineno, 2) 
开发者ID:bkerler,项目名称:android_universal,代码行数:21,代码来源:test_tracemalloc.py

示例12: memory_monitor

# 需要导入模块: import tracemalloc [as 别名]
# 或者: from tracemalloc import take_snapshot [as 别名]
def memory_monitor(command_queue: Queue, poll_interval=1):
    tracemalloc.start()
    old_max = 0
    snapshot = None
    while True:
        try:
            command_queue.get(timeout=poll_interval)
            if snapshot is not None:
                print(datetime.now())
                display_top(snapshot)

            return
        except Empty:
            max_rss = getrusage(RUSAGE_SELF).ru_maxrss
            if max_rss > old_max:
                old_max = max_rss
            snapshot = tracemalloc.take_snapshot()
            display_top(snapshot, limit=1)
            print(datetime.now(), 'max RSS', old_max) 
开发者ID:automl,项目名称:Auto-PyTorch,代码行数:21,代码来源:mem_test_thread.py

示例13: profile_memory

# 需要导入模块: import tracemalloc [as 别名]
# 或者: from tracemalloc import take_snapshot [as 别名]
def profile_memory(func: Callable[[], Any]) -> MemoryFrame:
    """Profile memory and return a tree of statistics.

    Feed those to `render_memory_svg()` to write an SVG.
    """
    import tracemalloc

    tracemalloc.start(1024)
    try:
        func()
    finally:
        snap = tracemalloc.take_snapshot()
        tracemalloc.stop()
    stats = snap.statistics("traceback")
    root = MemoryFrame(blocks=0, size=0)
    for stat in stats:
        blocks = stat.count
        size = stat.size
        callee = root
        callee.blocks += blocks
        callee.size += size
        for frame in stat.traceback:
            lineid = (frame.filename, frame.lineno)
            callee = callee.callers.setdefault(
                lineid, MemoryFrame(blocks=0, size=0)
            )
            callee.blocks += blocks
            callee.size += size
    while len(root.callers) == 1:
        root = next(iter(root.callers.values()))
    return root 
开发者ID:edgedb,项目名称:edgedb,代码行数:33,代码来源:profiler.py

示例14: _memory_of_version

# 需要导入模块: import tracemalloc [as 别名]
# 或者: from tracemalloc import take_snapshot [as 别名]
def _memory_of_version(self, version, store_packets=500) -> tracemalloc.Snapshot:
        """
        Create memory snapshot of collector run with packets of version :version:
        :param version:
        :return:
        """
        if not tracemalloc.is_tracing():
            raise RuntimeError
        pkts, t1, t2 = send_recv_packets(generate_packets(NUM_PACKETS_PERFORMANCE, version),
                                         store_packets=store_packets)
        self.assertEqual(len(pkts), NUM_PACKETS_PERFORMANCE)
        snapshot = tracemalloc.take_snapshot()
        del pkts
        return snapshot 
开发者ID:bitkeks,项目名称:python-netflow-v9-softflowd,代码行数:16,代码来源:test_performance.py

示例15: test_compare_memory

# 需要导入模块: import tracemalloc [as 别名]
# 或者: from tracemalloc import take_snapshot [as 别名]
def test_compare_memory(self):
        """
        Test memory usage of two collector runs with IPFIX and NetFlow v9 packets respectively.
        Then compare the two memory snapshots to make sure the libraries do not cross each other.
        TODO: more features could be tested, e.g. too big of a difference if one version is optimized better
        :return:
        """
        pkts, t1, t2 = send_recv_packets(generate_packets(NUM_PACKETS_PERFORMANCE, 10))
        self.assertEqual(len(pkts), NUM_PACKETS_PERFORMANCE)
        snapshot_ipfix = tracemalloc.take_snapshot()
        del pkts
        tracemalloc.clear_traces()

        pkts, t1, t2 = send_recv_packets(generate_packets(NUM_PACKETS_PERFORMANCE, 9))
        self.assertEqual(len(pkts), NUM_PACKETS_PERFORMANCE)
        snapshot_v9 = tracemalloc.take_snapshot()
        del pkts

        stats = snapshot_v9.compare_to(snapshot_ipfix, "lineno")
        for stat in stats:
            if stat.traceback[0].filename.endswith("netflow/ipfix.py"):
                self.assertEqual(stat.count, 0)
                self.assertEqual(stat.size, 0)

        stats = snapshot_ipfix.compare_to(snapshot_v9, "lineno")
        for stat in stats:
            if stat.traceback[0].filename.endswith("netflow/v9.py"):
                self.assertEqual(stat.count, 0)
                self.assertEqual(stat.size, 0) 
开发者ID:bitkeks,项目名称:python-netflow-v9-softflowd,代码行数:31,代码来源:test_performance.py


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