benchmark.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python
  2. """
  3. Really simple rudimentary benchmark to compare ConnectionPool versus standard
  4. urllib to demonstrate the usefulness of connection re-using.
  5. """
  6. from __future__ import print_function
  7. import sys
  8. import time
  9. import urllib
  10. sys.path.append("../")
  11. import urllib3 # noqa: E402
  12. # URLs to download. Doesn't matter as long as they're from the same host, so we
  13. # can take advantage of connection re-using.
  14. TO_DOWNLOAD = [
  15. "http://code.google.com/apis/apps/",
  16. "http://code.google.com/apis/base/",
  17. "http://code.google.com/apis/blogger/",
  18. "http://code.google.com/apis/calendar/",
  19. "http://code.google.com/apis/codesearch/",
  20. "http://code.google.com/apis/contact/",
  21. "http://code.google.com/apis/books/",
  22. "http://code.google.com/apis/documents/",
  23. "http://code.google.com/apis/finance/",
  24. "http://code.google.com/apis/health/",
  25. "http://code.google.com/apis/notebook/",
  26. "http://code.google.com/apis/picasaweb/",
  27. "http://code.google.com/apis/spreadsheets/",
  28. "http://code.google.com/apis/webmastertools/",
  29. "http://code.google.com/apis/youtube/",
  30. ]
  31. def urllib_get(url_list):
  32. assert url_list
  33. for url in url_list:
  34. now = time.time()
  35. urllib.urlopen(url)
  36. elapsed = time.time() - now
  37. print("Got in %0.3f: %s" % (elapsed, url))
  38. def pool_get(url_list):
  39. assert url_list
  40. pool = urllib3.PoolManager()
  41. for url in url_list:
  42. now = time.time()
  43. pool.request("GET", url, assert_same_host=False)
  44. elapsed = time.time() - now
  45. print("Got in %0.3fs: %s" % (elapsed, url))
  46. if __name__ == "__main__":
  47. print("Running pool_get ...")
  48. now = time.time()
  49. pool_get(TO_DOWNLOAD)
  50. pool_elapsed = time.time() - now
  51. print("Running urllib_get ...")
  52. now = time.time()
  53. urllib_get(TO_DOWNLOAD)
  54. urllib_elapsed = time.time() - now
  55. print("Completed pool_get in %0.3fs" % pool_elapsed)
  56. print("Completed urllib_get in %0.3fs" % urllib_elapsed)
  57. """
  58. Example results:
  59. Completed pool_get in 1.163s
  60. Completed urllib_get in 2.318s
  61. """