前置工作
1.建立lambda function
2.建立api gateway 并部署
api gateway的使用文档:https://docs.aws.amazon.com/zh_cn/apigateway/index.html
3.python写工具测量
代码实现
import requests,time
import asyncio
from aiohttp import ClientSession
import aiohttp
import matplotlib.pyplot as plt
url = "xxx"#填入自己函数调用api
tasks = []
run_time=[]
present_time=[]
start_time=[]
count=300
async def run():
async with ClientSession() as session:
async with session.get(url) as response:
response = await response.read()
present_time.append(time.time())
print(response)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
begin_time=time.time()
for i in range(count):
start_time.append(time.time())
tasks.append(run())
end_time = time.time()
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
end_time = time.time()
print("运行时间:",str(end_time-begin_time))
for i in range(count):
run_time.append(present_time[i]-start_time[i])
print(run_time)
x=[i for i in range(count)]
plt.plot(x,run_time)
plt.show()
思考
为什么延迟随并发的数量增加而增加?
高并发如何实现?