gpt4 book ai didi

python - 如何在使用 aiohttp 时获取 response_time 和 response_size

转载 作者:行者123 更新时间:2023-12-03 23:28:13 25 4
gpt4 key购买 nike

是否可以获取使用 aiohttp 发出的每个请求的响应时间和响应大小?

文档似乎在任何地方都没有这些属性。

谢谢

最佳答案

len(response.text())将返回解压缩响应的大小。
如果您想要原始压缩响应的大小,您需要设置 auto_decompress=False在创建 aiohttp.ClientSession 期间.之后你可以通过 len(await response.read()) 获得它.
但它会让 response.text()不可用,因为它需要未压缩的响应。要使其再次可用,您必须手动解压缩它:

import time
import zlib
import brotli

async with aiohttp.ClientSession(auto_decompress=False) as session:
start = time.monotonic()
response = await session.get(url='www.test.com')
response_time = time.monotonic() - start
response_size = len(await response.read())

encoding = response.headers['Content-Encoding']
if encoding == 'gzip':
response._body = zlib.decompress(response._body, 16 + zlib.MAX_WBITS)
elif encoding == 'deflate':
response._body = zlib.decompress(response._body, -zlib.MAX_WBITS)
elif encoding == 'br':
response._body == brotli.decompress(response._body)

response_text = await response.text()
关于 time.time()来自 pymotw.com :

Because time.time() looks at the system clock, and the system clock can be changed by the user or system services for synchronizing clocks across multiple computers, calling time.time() repeatedly may produce values that go forwards and backwards. This can result in unexpected behavior when trying to measure durations or otherwise use those times for computation. Avoid those situations by using time.monotonic(), which always returns values that go forward.


aiohttp docs建议使用 loop.time() (这也是单调的):
async def on_request_start(session, trace_config_ctx, params):
trace_config_ctx.start = asyncio.get_event_loop().time()

async def on_request_end(session, trace_config_ctx, params):
elapsed = asyncio.get_event_loop().time() - trace_config_ctx.start
print("Request took {}".format(elapsed))

trace_config = aiohttp.TraceConfig()
trace_config.on_request_start.append(on_request_start)
trace_config.on_request_end.append(on_request_end)
async with aiohttp.ClientSession(trace_configs=[trace_config]) as client:
client.get('http://example.com/some/redirect/')

关于python - 如何在使用 aiohttp 时获取 response_time 和 response_size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56990958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com