首页 > 解决方案 > 在 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)

标签: http2pycurlhttpx

解决方案


https://github.com/dalf/pyhttp-benchmark可能会有所帮助。

看:

TLDR:使用 httpx,使用 http2 时

  • 避免大内容(> 64kb)。
  • 避免顺序请求
  • 更喜欢并行请求

推荐阅读