Skip to content

Instantly share code, notes, and snippets.

@yhchan
Created February 8, 2014 09:31
Show Gist options
  • Save yhchan/8880183 to your computer and use it in GitHub Desktop.
Save yhchan/8880183 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Modifiy from urllib3 benchmark
https://raw.github.com/shazow/urllib3/master/test/benchmark.py
Really simple rudimentary benchmark to compare ConnectionPool versus standard
urllib to demonstrate the usefulness of connection re-using.
"""
from __future__ import print_function
import time
import requests
# URLs to download. Doesn't matter as long as they're from the same host, so we
# can take advantage of connection re-using.
TO_DOWNLOAD = [
'https://developers.google.com/maps/',
'https://developers.google.com/youtube/',
'https://developers.google.com/games/',
'https://developers.google.com/chrome/',
'https://developers.google.com/wallet/',
'https://developers.google.com/tv/',
'https://developers.google.com/cloud/',
'https://developers.google.com/android/',
'https://developers.google.com/google-apps/',
'https://developers.google.com/+/',
'https://developers.google.com/academy/',
'https://developers.google.com/products/',
'https://developers.google.com/mobile/',
'https://developers.google.com/webmasters/',
'https://developers.google.com/monetize/',
'https://developers.google.com/advertise/',
'https://developers.google.com/events/io/',
'https://developers.google.com/appengine/',
'https://developers.google.com/live/',
]
def requests_get(url_list):
assert url_list
for url in url_list:
now = time.time()
requests.get(url)
elapsed = time.time() - now
print("Got in %0.3f: %s" % (elapsed, url))
def requests_session_get(url_list):
assert url_list
session = requests.Session()
for url in url_list:
now = time.time()
session.get(url)
elapsed = time.time() - now
print("Got in %0.3fs: %s" % (elapsed, url))
if __name__ == '__main__':
print("Running requests_session_get ...")
now = time.time()
requests_session_get(TO_DOWNLOAD)
requests_session_elapsed = time.time() - now
print("Running requests_get ...")
now = time.time()
requests_get(TO_DOWNLOAD)
requests_elapsed = time.time() - now
print("Completed requests_session_get in %0.3fs" %
requests_session_elapsed)
print("Completed requests_get in %0.3fs" % requests_elapsed)
"""
Example results:
Completed requests_session_get in 8.576s
Completed requests_get in 9.981s
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment