该基准是否可靠 - aiohttp vs请求

时间:2018-04-25 20:31:21

标签: python-3.x python-requests benchmarking aiohttp

我们正努力在我的工作中选择技术。我以为我会使用两个库(aiohttp和请求)运行基准测试。

我希望它尽可能公平/公正,并希望从社区看到这一点。

所以这是我目前的代码:

import asyncio as aio
import aiohttp
import requests
import time

TEST_URL = "https://a-domain-i-can-use.tld"

def requests_fetch_url(url):
    with requests.Session() as session:
        with session.get(url) as resp:
            html = resp.text

async def aio_fetch_url(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            html = await resp.text()

t_start_1 = time.time()
for i in range(10):
    [requests_fetch_url(TEST_URL) for i in range(16)]
t_end_1 = time.time()
print("using requests : %.2fs" % (t_end_1-t_start_1))

t_start_2 = time.time()
for i in range(10):
    aio.get_event_loop().run_until_complete(aio.gather(
        *[aio_fetch_url(TEST_URL) for i in range(16)]
    ))
t_end_2 = time.time()
print("using aiohttp : %.2fs" % (t_end_2-t_start_2))

ratio = (t_end_1-t_start_1)/(t_end_2-t_start_2)
print("ratio : %.2f" % ratio)

那有偏见吗?有没有办法改善它更可靠? 我还应该监控CPU和/或RAM的使用情况吗?还有什么我想念的? 有没有办法改善这个?

0 个答案:

没有答案