python3堆栈_Python3中内存配置文件分配的类名和调用堆栈

我想在python3的单元测试中计算应用程序的内存消耗。

我试图在一个简单的单元测试中使用tracemaloc模块来获取所需的信息。

但是我不知道如何得到类名。

memory_profile.test_memory_profile.TstClass

给定对象的。

有没有别的办法?

class TstClass(object):

def __init__(self):

self.value = 100

class TestMemUsage(unittest.TestCase):

def test_tracemalloc_single_object(self):

tracemalloc.start(5)

snapshot1 = tracemalloc.take_snapshot()

obj = TstClass()

snapshot2 = tracemalloc.take_snapshot()

stats = snapshot2.compare_to(snapshot1, 'lineno')

stat = stats[2] # Allocation of ExampleClass()

print(stat)

for line in stat.traceback.format(): # Traceback

print(line)

file_path, line_nr = stat.traceback._frames[0]

print('obj={}'.format(obj))

print('file_path={}'.format(file_path))

print('line_nr={}'.format(line_nr))

print('size_diff={}'.format(stat.size_diff))

print('count_diff={}'.format(stat.count_diff))

# How to get the class name of the object here ?

# e.g. memory_profile.test_memory_profile.TstClass

测试输出:

D:\Projekte\python\python_examples\memory_profile\test_stack_overflow.py:18: size=356 B (+356 B), count=2 (+2), average=178 B

File "D:\Projekte\python\python_examples\memory_profile\test_stack_overflow.py", line 18

obj = TstClass()

obj=

file_path=D:\Projekte\python\python_examples\memory_profile\test_stack_overflow.py

line_nr=18

size_diff=356

count_diff=2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值