#NEWLINE# def run_all_benchmarks( url = '' , cycles = 10 , delay = None , output_file = None , **kwargs ) : #NEWLINE# #INDENT# results = list ( ) #NEWLINE# headers = ( 'Library' , 'Reuse Connections?' , 'Options' , 'Time' ) #NEWLINE# tests = list ( ) #NEWLINE# timer_type = kwargs . get ( 'timer' ) #NEWLINE# tests . append ( ( 'requests' , False , 'Default' , 'import requests' , "r = requests.get('$url', verify=False)" ) ) #NEWLINE# tests . append ( ( 'requests' , True , 'Default' , "import requests;             session = requests.Session();             r = requests.Request('GET', '$url').prepare()" , 'v = session.send(r, verify=False)' ) ) #NEWLINE# tests . append ( ( 'pycurl' , True , "Reuse handle, don't save body" , "from pycurl import Curl;             mycurl=Curl();             mycurl.setopt(mycurl.SSL_VERIFYPEER, 0);             mycurl.setopt(mycurl.SSL_VERIFYHOST, 0);             mycurl.setopt(mycurl.URL, '$url');             mycurl.setopt(mycurl.WRITEFUNCTION, lambda x: None)" , 'mycurl.perform()' ) ) #NEWLINE# tests . append ( ( 'pycurl' , True , 'Reuse handle, save response to new cStringIO buffer' , "from pycurl import Curl; from cStringIO import StringIO;             mycurl=Curl();             mycurl.setopt(mycurl.SSL_VERIFYPEER, 0);             mycurl.setopt(mycurl.SSL_VERIFYHOST, 0);             mycurl.setopt(mycurl.URL, '$url')" , 'body = StringIO();             mycurl.setopt(mycurl.WRITEFUNCTION, body.write);             mycurl.perform();             val = body.getvalue();             body.close()' ) ) #NEWLINE# tests . append ( ( 'pycurl' , False , 'Reuse handle, save response to new cStringIO buffer' , "from pycurl import Curl; from cStringIO import StringIO;             mycurl=Curl();             mycurl.setopt(mycurl.URL, '$url');             mycurl.setopt(mycurl.SSL_VERIFYPEER, 0);             mycurl.setopt(mycurl.SSL_VERIFYHOST, 0);             body = StringIO();             mycurl.setopt(mycurl.FORBID_REUSE, 1)" , 'body = StringIO();             mycurl.setopt(mycurl.WRITEFUNCTION, body.write);             mycurl.perform();             val = body.getvalue();             body.close()' ) ) #NEWLINE# tests . append ( ( 'pycurl' , False , 'New handle, save response to new cStringIO buffer' , 'from pycurl import Curl; from cStringIO import StringIO' , "body = StringIO();             mycurl=Curl();             body = StringIO();             mycurl.setopt(mycurl.URL, '$url');             mycurl.setopt(mycurl.SSL_VERIFYPEER, 0);             mycurl.setopt(mycurl.SSL_VERIFYHOST, 0);             mycurl.setopt(mycurl.DNS_USE_GLOBAL_CACHE, True);             mycurl.setopt(mycurl.WRITEFUNCTION, body.write);             mycurl.perform();             val = body.getvalue();             body.close()" ) ) #NEWLINE# parsed_url = urlparse . urlparse ( url ) #NEWLINE# scheme = parsed_url . scheme #NEWLINE# hostname = parsed_url . hostname #NEWLINE# port = parsed_url . port #NEWLINE# setup_string = '' #NEWLINE# if ( scheme == 'https' ) : #NEWLINE# #INDENT# setup_string = "import urllib3;             http_pool = urllib3.HTTPSConnectionPool('{0}', port={1}, cert_reqs='CERT_NONE', assert_hostname=False)" . format ( hostname , port ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# setup_string = 'import urllib3; http_pool = urllib3.PoolManager()' #NEWLINE# #UNINDENT# tests . append ( ( 'urllib3' , True , 'Default' , setup_string , "body = http_pool.urlopen('GET', '$url').read()" ) ) #NEWLINE# tests . append ( ( 'urllib' , False , 'Default' , 'import urllib' , "body = urllib.urlopen('$url').read()" ) ) #NEWLINE# for test in tests : #NEWLINE# #INDENT# my_result = run_test ( test [ 0 ] , url , cycles , test [ 1 ] , test [ 2 ] , test [ 3 ] , test [ 4 ] , delay = delay , timer = test ) #NEWLINE# results . append ( ( test [ 0 ] , test [ 1 ] , test [ 2 ] , my_result [ ( - 1 ) ] ) ) #NEWLINE# #UNINDENT# if output_file : #NEWLINE# #INDENT# with open ( output_file , 'wb' ) as csvfile : #NEWLINE# #INDENT# outwriter = csv . writer ( csvfile , dialect = csv . excel ) #NEWLINE# outwriter . writerow ( ( 'url' , 'cycles' , 'delay' ) ) #NEWLINE# outwriter . writerow ( ( url , cycles , delay ) ) #NEWLINE# outwriter . writerow ( headers ) #NEWLINE# for result in results : #NEWLINE# #INDENT# outwriter . writerow ( result )