2
我正在嘗試使用aiohttp
創建一個網站流量模擬器。以下代碼示例異步發出10k個請求。我想知道他們中有多少人同時發生,所以我可以說這是10k用戶同時請求網站的模型。如何確定每秒使用aiohttp的請求數?
如何確定併發網絡請求的數量,或者如何確定aiohttp每秒請求多少個請求?有沒有辦法實時調試/分析併發請求的數量?
有沒有更好的方法來使用任何其他編程語言來建模網絡流量模擬器?
import asyncio
import aiohttp
async def fetch(session, url):
with aiohttp.Timeout(10, loop=session.loop):
async with session.get(url) as response:
return await response.text()
async def run(r):
url = "http://localhost:3000/"
tasks = []
# Create client session that will ensure we dont open new connection
# per each request.
async with aiohttp.ClientSession() as session:
for i in range(r):
html = await fetch(session, url)
print(html)
# make 10k requests per second ?? (not confident this is true)
number = 10000
loop = asyncio.get_event_loop()
loop.run_until_complete(run(number))