请求与请求期货 - 响应时间不准确?

requests vs. request futures - response times inaccurate?

我有 Python 运行 10 个 GET 请求并测量响应时间的代码:

from datetime import datetime
from requests_futures.sessions import FuturesSession
import requests

class CustomSession(FuturesSession):

    def __init__(self, *args, **kwargs):
        super(CustomSession, self).__init__(*args, **kwargs)
        self.timing = {}
        self.timing = {}

    def request(self, method, url, *args, **kwargs):
        background_callback = kwargs.pop('background_callback', None)
        test_id = kwargs.pop('test_id', None)

        # start counting
        self.timing[test_id] = {}
        self.timing[test_id]['cS'] = datetime.now()

        def time_it(sess, resp):
            # here if you want to time the server stuff only
            self.timing[test_id]['cE'] = datetime.now()
            if background_callback:
                background_callback(sess, resp)
            # here if you want to include any time in the callback

        return super(CustomSession, self).request(method, url, *args,
                                                  background_callback=time_it,
                                                  **kwargs)

# using requests-futures

print('requests-futures:')

session = CustomSession()

futures = []
for i in range(10):

    futures.append(session.get('http://google.com/', test_id=i))
for future in futures:
    try:
        r = future.result()
        #print((session.timing[i]['cE'] - session.timing[i]['cS']))
    except Exception as e:
        print(e)
for i in range(10):
    print((session.timing[i]['cE'] - session.timing[i]['cS']).total_seconds() * 1000)


# using requests

print('requests:')

for i in range(10):

    check_start_timestamp = datetime.utcnow()
    r = requests.get('http://google.com')
    check_end_timestamp = datetime.utcnow()
    cE = int((check_end_timestamp - check_start_timestamp).total_seconds() * 1000)
    print(cE)

请求期货:

112.959
118.627
160.139
174.32
214.399
224.295
267.557
276.582
316.824
327.00800000000004

请求:

99
104
92
110
100
126
140
112
102
107

看来:

  1. requests-futures的响应时间出现累加(时间越来越大)
  2. 使用普通 requests 运行速度大大加快。

这正常吗?我是否遗漏了会导致差异的内容?

问题 1


requests-futures 的响应时间似乎相加(时间越来越大)

原因是requests_futures在后台使用了线程池。您可以看到这一点,因为时间以块的形式出现(为清楚起见添加了分隔符,线程数可以通过 max_workers 参数更改):

  • 默认池大小 2:

    161.226
    172.41600000000003
    ---
    250.141
    253.18600000000004
    ---
    329.32800000000003
    342.71000000000004
    ---
    408.21200000000005
    420.614
    ---
    487.356
    499.311
    
  • 池大小为 4:

    149.781
    154.761
    151.971
    155.385
    ---
    225.458
    230.596
    239.784
    240.386
    ---
    313.801
    314.056
    
  • 图表(蓝色为2,红色为4):

    可以看到,分组出现的间隔大致相同,应该是一个请求的响应时间。

理论上,将池大小设置为 10 可为您的测试提供最佳结果,给出如下结果:

252.977
168.379
161.689
165.44
169.238
157.929
171.77
154.089
168.283
159.23999999999998

然而,下面的效果更有效。

问题 2


使用普通请求运行速度大大加快。

我不能确定,但​​看看第一个请求批次的时间,它只有 ~15 个单位(微秒?)。这可能是由于:

  • 线程切换。由于正常请求请求发生在与请求者相同的线程中,因此作业会立即开始。对于线程池,只有当 OS 切换到正确的线程时才会启动请求。这会产生时间开销。
  • 投票中。期货可能会使用某种轮询来检查结果,因此那里也可能会有延迟。

futures 的优点是 10 个请求的总时间更少,而不是单个时间,所以这种微小的差异并不是真正的问题。