pycurl返回比命令行卷曲高得多的APPCONNECT(SSL握手)时间

时间:2017-12-04 00:41:05

标签: python python-2.7 curl libcurl pycurl

我正在使用pycurl来衡量网站的响应时间。根据卷曲documentation,卷曲提供以下六次:

An overview of the six time values available from curl_easy_getinfo()

curl_easy_perform()
    |
    |--NAMELOOKUP
    |--|--CONNECT
    |--|--|--APPCONNECT
    |--|--|--|--PRETRANSFER
    |--|--|--|--|--STARTTRANSFER
    |--|--|--|--|--|--TOTAL
    |--|--|--|--|--|--REDIRECT

当我运行以下python脚本时,APPCONNECT时间 - 对应于SSL握手 - 与命令行相比一直高得多:

import pycurl
import certifi
import logging

def retrieve_url(url):
    """
    Retrieves the requested url and records the response time.

    :param url: the url to retrieve
    :type url: string
    :return: A tuple of response times per exchange type if the request was successful, False otherwise
    :rtype: tuple/boolean
    """
    rvale = False
    try:
        curl = pycurl.Curl()
        curl.setopt(pycurl.CAINFO, certifi.where())
        # curl.setopt(pycurl.CAINFO, "C:/curl/curl-ca-bundle.crt") <-- same performance
        curl.setopt(pycurl.URL, url)  # set url
        curl.setopt(pycurl.WRITEFUNCTION, lambda x: None)
        curl.setopt(pycurl.SSL_VERIFYPEER, 1)
        curl.setopt(pycurl.SSL_VERIFYHOST, 2)
        curl.setopt(pycurl.FOLLOWLOCATION, 1)
        curl.perform()
        # Time from start until name resolving completed
        # https://curl.haxx.se/libcurl/c/CURLINFO_NAMELOOKUP_TIME.html
        nlookup_time = curl.getinfo(pycurl.NAMELOOKUP_TIME)
        # Time from start until remote host or proxy completed.
        # https://curl.haxx.se/libcurl/c/CURLINFO_CONNECT_TIME.html
        connect_time = curl.getinfo(pycurl.CONNECT_TIME)
        # Time from start until SSL/SSH handshake completed
        # https://curl.haxx.se/libcurl/c/CURLINFO_APPCONNECT_TIME.html
        appconnect_time = curl.getinfo(pycurl.APPCONNECT_TIME)
        # Time from start until just when the first byte is received.
        # https://curl.haxx.se/libcurl/c/CURLINFO_STARTTRANSFER_TIME.html
        pretransfer_time = curl.getinfo(pycurl.PRETRANSFER_TIME)
        ttfb = curl.getinfo(pycurl.STARTTRANSFER_TIME)
        curl.close()
        rvalue = (nlookup_time, connect_time, appconnect_time, ttfb)
    except pycurl, e:
        logging.error(str(e))

    return rvalue

print retrieve_url("https://stackoverflow.com")

对于上面的剧本我得到的时间是:

NAMELOOKUP: 0.047  
CONNECT: 0.063  
APPCONNECT: 0.344  
STARTTRANSFER: 0.406  

从命令行我得到:

NAMELOOKUP:  0.004  
CONNECT:  0.017  
APPCONNECT:  0.049  
STARTTRANSFER:  0.161

上述时间在运行之间变化很小。对于pycurl,APPCONNECT时间的差异总是高一个数量级。

所以我试着找出pycurl中这种开销的原因是什么?我在Windows 7上运行Python 2.7,curl版本是7.52.1。我也在Debian GNU / Linux 8.8(jessie)中尝试了curl 7.38.0,结果非常相似,所以它似乎不依赖于操作系统/版本。

0 个答案:

没有答案