Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 请求与请求未来-响应时间不准确?_Python_Performance_Python 3.x_Python Requests - Fatal编程技术网

Python 请求与请求未来-响应时间不准确?

Python 请求与请求未来-响应时间不准确?,python,performance,python-3.x,python-requests,Python,Performance,Python 3.x,Python Requests,我有运行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

我有运行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
    请求的响应时间似乎是累加的(时间越来越长)

    原因是请求在后台使用线程池。您可以看到这一点,因为计时是分块进行的(为了清晰起见,添加了分隔符,线程的数量可以通过
    max\u 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个单位(微秒?)。这可能是由于:

    • 线程切换。由于正常请求与请求者发生在同一个线程中,因此作业会立即启动。对于线程池,只有当操作系统切换到正确的线程时,才会启动请求。这将导致时间开销
    • 投票。期货可能会使用某种轮询来检查结果,因此也可能会有延迟
    futures的优点是10个请求的总时间较低,而不是单个请求的时间,因此这一微小的差异并不是真正的问题