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

相关推荐
随遇而安zx2 分钟前
【地基篇】---JVM 内存结构知识点(Java 8 运行时数据区深度解析)
java·开发语言·jvm
FfHUCisI17 分钟前
Golang 切片扩容策略
开发语言·数据库·golang
en.en..18 分钟前
Ubuntu嵌入式开发 export环境变量
java·开发语言·数据库
gf132111119 分钟前
【python_回复邮件】
android·java·python
天空属于哈夫克327 分钟前
企业微信API:企业微信机器人如何开发?
python·机器人·企业微信
Patrick在香港28 分钟前
把 Claude 塞进 pandas 管道:7 条脏地址实测,5 条自动清洗、2 条被闸门拦下
python·pandas·etl·claude·数据清洗
二进制漫游记29 分钟前
PostgreSQL超详细入门教程:Windows安装+Python异步连接+完整增删改查实战
python·postgresql
ynchyong30 分钟前
JavaScript Promise 实战:.then() 与 .catch() 的区别及回调函数封装指南
开发语言·javascript·ecmascript·promise·then
2601_9620776031 分钟前
从零搭建Python接口自动化测试框架:pytest+requests实战指南
python·pytest·接口自动化·requests·框架搭建
名字还没想好☜34 分钟前
Go 1.21 context.WithoutCancel 实战:父 context 取消了,收尾任务还要继续跑
开发语言·后端·golang·go