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

相关推荐
2601_96798011几秒前
Flask Web 应用
python·flask·web开发·环境变量·虚拟环境
aifannihao17 分钟前
ß 大写变 SS:用 Python 算清 Ruby2Shell 的缓冲区账本
python·安全·标准库·缓冲区溢出·字符串编码
liwulin050637 分钟前
【OLLAMA】调用SKILL的通用ReAct
python
严肃小白兔1 小时前
PHP 8.6 正式发布:部分函数应用、原生 I/O 轮询与新特性实战解析
开发语言·php
郝学胜-神的一滴1 小时前
Effective Python 条款36:优雅拼装迭代器与生成器
开发语言·数据结构·人工智能·python·numpy·scikit-learn
Norna3 小时前
Git 给路径加了引号,这个文件就从 review 里消失了
python
wuyk5553 小时前
Python办公自动化-第04讲-批量新建Excel
开发语言·python·excel
wuyk5553 小时前
Spider-第20讲-综合实战可上线爬虫
大数据·爬虫·python·elasticsearch
code2cat4 小时前
【随笔】Java客户端选了HTTP/3,怎样知道请求实际走了哪个版本
java·开发语言·http
JonaHu4 小时前
关键词排名突然消失?用 Python、SQLite 和 SERP API 搭一套可解释的 SEO 监控
python·sqlite·seo·serp api