Python使用FastApi测试;Node.JS使用Fastify;Rust则使用Actix。
选择的Python和Node框架,是在搜索 "最快的<某语言>api "时得到的最高结果;Rust的Actix是一直高度维护的。
测试的基础很简单;在我的MacBook Pro M1上,每个框架处理来自网络服务器的5000个基本 "Hello, World "响应需要多长时间?
我用来运行测试的代码非常简单,显然,我们只关心速度:
Python
<span style="color:#333333"><span style="background-color:#f5f5f5">客户端代码:
<strong>import</strong> requests
from requests.adapters <strong>import</strong> HTTPAdapter
from requests.packages.urllib3.util.retry <strong>import</strong> Retry
MAX_RETIES = 3
def create_retriable_session():
s = requests.Session()
retries = Retry(
total=MAX_RETIES,
)
s.mount('http:<span style="color:#0000aa"><em>//', HTTPAdapter(max_retries=retries))</em></span><span style="color:black">
s.mount('https:</span><span style="color:#0000aa"><em>//', HTTPAdapter(max_retries=retries))</em></span><span style="color:black">
<strong>return</strong> s
def main():
s = create_retriable_session()
<strong>for</strong> _ in range(0, 5000):
s.get(</span><span style="color:#00bb00">"http://127.0.0.1:8000/"</span><span style="color:black">)
</span></span></span>