【问题标题】:requests vs. request futures - response times inaccurate?请求与请求期货 - 响应时间不准确?
【发布时间】:2015-04-23 16:01:29
【问题描述】:

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

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 运行速度大大加快。

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

【问题讨论】:

    标签: python performance python-3.x python-requests


    【解决方案1】:

    问题 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 个单位(微秒?)。这可能是由于:

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

    future 的优点是 10 个请求的总时间较低,而不是单个的时间,所以这种细微的差异并不是真正的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多