Python httpstat是一个基于Python的命令行工具,用于测量HTTP请求的性能和状态信息。它能够向目标服务器发送HTTP请求,并显示详细的统计信息,包括DNS解析时间、建立连接时间、TLS/SSL握手时间、首字节时间、总时间等。这些信息对于排查网络问题、优化Web应用程序以及监控HTTP请求的性能非常有帮助。
使用python的concurrent.futures
模块来并发执行HTTP请求,并收集性能数据进行分析。
import subprocess
from concurrent.futures import ThreadPoolExecutor# 要测试的URL
# url = "https://modao.cc/proto/design/pb2g5k"
# get请求带参数时,需要加引号
url = '"http://routeplan.dev.hdmpcloud.dpi.net.cn/direction/v1/driving?origin=125.2825605,43.8485565&destination=125.2690028,43.8701480"'# 并发请求的数量
concurrent_requests = 1def run_httpstat(url):# 运行httpstat命令并获取性能数据command = f"httpstat {url}"result = subprocess.run(command, shell=True, capture_output=True, text=True)# 将性能数据记录到日志文件# with open("performance.log", "a") as log_file:# log_file.write(result.stdout)# 打印性能数据print(result.stdout)# 创建线程池并并发执行请求
with ThreadPoolExecutor(max_workers=concurrent_requests) as executor:for _ in range(concurrent_requests):executor.submit(run_httpstat, url)