【发布时间】:2022-04-22 05:28:10
【问题描述】:
如何对异步 Python 脚本(使用 ASYNCIO)进行分析/基准测试?
你通常会这样做
totalMem = tracemalloc.get_traced_memory()[0]
totalTime = time.time()
retValue = myFunction()
totalTime = time.time() - totalTime
totalMem = tracemalloc.get_traced_memory()[0] - totalMem
这样我可以节省函数花费的总时间。 我学会了如何使用装饰器,我就是这么做的 - 并将所有统计数据转储到一个文本文件中以供以后分析。
但是,当您拥有 ASYNCIO 脚本时,情况就大不相同了:该函数将在执行“await aiohttpSession.get()”时阻塞,并且控制将返回到事件循环,该循环将运行其他函数。
这样,经过的时间和总分配内存的变化不会透露任何信息,因为我将测量的不仅仅是那个函数。
唯一可行的方法是
class MyTracer:
def __init__(self):
self.totalTime = 0
self.totalMem = 0
self.startTime = time.time()
self.startMem = tracemalloc.get_traced_memory()[0]
def stop(self):
self.totalTime += time.time() - self.startTime
self.totalMem += tracemalloc.get_traced_memory()[0] - self.startMem
def start(self):
self.startTime = time.time()
self.startMem = tracemalloc.get_traced_memory()[0]
现在,不知何故,将其插入代码中:
def myFunction():
tracer = MyTracer()
session = aiohttp.ClientSession()
# do something
tracer.stop()
# the time elapsed here, and the changes in the memory allocation, are not from the current function
retValue = await(await session.get('https://hoochie-mama.org/cosmo-kramer',
headers={
'User-Agent': 'YoYo Mama! v3.0',
'Cookies': 'those cookies are making me thirsty!',
})).text()
tracer.start()
# do more things
tracer.stop()
# now "tracer" has the info about total time spent in this function, and the memory allocated by it
# (the memory stats could be negative if the function releases more than allocates)
有没有办法实现这一点,我的意思是,无需插入所有这些代码就可以分析我的所有 asyncio 代码? 或者是否有一个模块已经能够做到这一点?
【问题讨论】:
标签: python profiling benchmarking python-asyncio