http2 - 在 python 中对 HTTP1.1/HTTP2 进行基准测试
问题描述
我用 python 对 http1.1/http2 做了一些基准测试,代码很简单,就像多次重复谷歌搜索请求一样。结果很有趣:http2 版本的速度要慢得多。(我尝试了两个 pycurl/httpx 库)有人可以解释为什么会发生这种情况吗?
更新:这是httpx版本代码:(第一pip install httpx[http2]
)
import time
import httpx
client = httpx.Client(http2=True)
start = time.time()
for _ in range(100):
response = client.get("https://www.google.com/search?q=good")
response.raise_for_status()
print(time.time() - start)
解决方案
https://github.com/dalf/pyhttp-benchmark可能会有所帮助。
看:
- httpx 0.18.1 的结果
- httpx 0.16.1 的结果
- 标签:
- httpx_uvloop_True = httpx,使用 uvloop 和 http2
- httpx_uvloop_False = httpx,仅使用 uvloop 和 http1
TLDR:使用 httpx,使用 http2 时
- 避免大内容(> 64kb)。
- 避免顺序请求
- 更喜欢并行请求
推荐阅读
- fingerprint - 如何在设备断开连接时触发事件。像网络故障/使用 Zkemkeeper 关闭机器电源
- google-cloud-platform - 在 GCP 中,授予所有权限的最小 IAM 角色集是什么?
- node.js - sockjs-node net::ERR_CONNECTION_REFUSED
- arrays - 使用 powershell 将文件复制到多个目标路径
- python - 在 Selenium 中选择动态下拉项
- python - 为什么我得到弱引用对象不再存在-使用-python-and-mysql
- appimage - 如何将 shell 程序打包到 AppImage 中?
- javascript - jsPDF - 如何在网格中添加图像
- javascript - 如何在 Forge Viewer 中卸载默认扩展?
- parsing - 一元减法搞砸了解析