python写接口性能测试

python 复制代码
import time
import requests

def measure_response_time(api_url):
    try:
        start_time = time.time()
        response = requests.get(api_url, timeout=10)  # 设置超时时间为10秒
        end_time = time.time()
        response_time = end_time - start_time
        print(f"接口 {api_url} 的响应时间为:{response_time} 秒")
    except requests.exceptions.RequestException as e:
        print(f"无法访问接口 {api_url},错误: {e}")

if __name__ == '__main__':
    api_urls = [
        'http://www.baidu.com',
        'https://chat.qinzhi.xyz/chat/15773192312389'
    ]
    for url in api_urls:
        measure_response_time(url)

需要安装requests包 pip install requests

相关推荐
海天一色y7 分钟前
Pycharm(十六)面向对象进阶
ide·python·pycharm
??? Meggie8 分钟前
【Python】保持Selenium稳定爬取的方法(防检测策略)
开发语言·python·selenium
XIE3921 小时前
Browser-use使用教程
python
酷爱码2 小时前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python
画个大饼2 小时前
Go语言实战:快速搭建完整的用户认证系统
开发语言·后端·golang
蹦蹦跳跳真可爱5892 小时前
Python----深度学习(基于深度学习Pytroch簇分类,圆环分类,月牙分类)
人工智能·pytorch·python·深度学习·分类
喵先生!3 小时前
C++中的vector和list的区别与适用场景
开发语言·c++
Thomas_YXQ4 小时前
Unity3D Lua集成技术指南
java·开发语言·驱动开发·junit·全文检索·lua·unity3d
xMathematics4 小时前
计算机图形学实践:结合Qt和OpenGL实现绘制彩色三角形
开发语言·c++·qt·计算机图形学·cmake·opengl
MinggeQingchun5 小时前
Python - 爬虫-网页解析数据-库lxml(支持XPath)
爬虫·python·xpath·lxml