Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 78 lines (61 sloc) 2.068 kb
c73bf7c Andrey Petrov Initial import of source.
authored
1 #!/usr/bin/env python
fb9c8a7 PEP8 compliance
Piotr Dobrogost authored
2
3 """
4 Really simple rudimentary benchmark to compare ConnectionPool versus standard
5 urllib to demonstrate the usefulness of connection re-using.
6 """
f22bbc2 Thomas Kluyver Fixes for importing tests.
takluyver authored
7 from __future__ import print_function
fb9c8a7 PEP8 compliance
Piotr Dobrogost authored
8
9 import sys
10 import time
11 import urllib
12
13 sys.path.append('../')
14 import urllib3
15
c73bf7c Andrey Petrov Initial import of source.
authored
16
17 # URLs to download. Doesn't matter as long as they're from the same host, so we
18 # can take advantage of connection re-using.
19 TO_DOWNLOAD = [
20 'http://code.google.com/apis/apps/',
21 'http://code.google.com/apis/base/',
22 'http://code.google.com/apis/blogger/',
23 'http://code.google.com/apis/calendar/',
24 'http://code.google.com/apis/codesearch/',
25 'http://code.google.com/apis/contact/',
26 'http://code.google.com/apis/books/',
27 'http://code.google.com/apis/documents/',
28 'http://code.google.com/apis/finance/',
29 'http://code.google.com/apis/health/',
30 'http://code.google.com/apis/notebook/',
31 'http://code.google.com/apis/picasaweb/',
32 'http://code.google.com/apis/spreadsheets/',
33 'http://code.google.com/apis/webmastertools/',
34 'http://code.google.com/apis/youtube/',
35 ]
36
37
38 def urllib_get(url_list):
39 assert url_list
40 for url in url_list:
41 now = time.time()
42 r = urllib.urlopen(url)
43 elapsed = time.time() - now
f22bbc2 Thomas Kluyver Fixes for importing tests.
takluyver authored
44 print("Got in %0.3f: %s" % (elapsed, url))
c73bf7c Andrey Petrov Initial import of source.
authored
45
fb9c8a7 PEP8 compliance
Piotr Dobrogost authored
46
c73bf7c Andrey Petrov Initial import of source.
authored
47 def pool_get(url_list):
48 assert url_list
ed8fb53 fixed benchmark.py - HTTPConnectionPool no longer has from_url() method
Piotr Dobrogost authored
49 pool = urllib3.connection_from_url(url_list[0])
c73bf7c Andrey Petrov Initial import of source.
authored
50 for url in url_list:
51 now = time.time()
52 r = pool.get_url(url)
53 elapsed = time.time() - now
f22bbc2 Thomas Kluyver Fixes for importing tests.
takluyver authored
54 print("Got in %0.3fs: %s" % (elapsed, url))
c73bf7c Andrey Petrov Initial import of source.
authored
55
fb9c8a7 PEP8 compliance
Piotr Dobrogost authored
56
c73bf7c Andrey Petrov Initial import of source.
authored
57 if __name__ == '__main__':
f22bbc2 Thomas Kluyver Fixes for importing tests.
takluyver authored
58 print("Running pool_get ...")
c73bf7c Andrey Petrov Initial import of source.
authored
59 now = time.time()
60 pool_get(TO_DOWNLOAD)
61 pool_elapsed = time.time() - now
62
f22bbc2 Thomas Kluyver Fixes for importing tests.
takluyver authored
63 print("Running urllib_get ...")
c73bf7c Andrey Petrov Initial import of source.
authored
64 now = time.time()
65 urllib_get(TO_DOWNLOAD)
66 urllib_elapsed = time.time() - now
67
f22bbc2 Thomas Kluyver Fixes for importing tests.
takluyver authored
68 print("Completed pool_get in %0.3fs" % pool_elapsed)
69 print("Completed urllib_get in %0.3fs" % urllib_elapsed)
c73bf7c Andrey Petrov Initial import of source.
authored
70
71
72 """
73 Example results:
74
75 Completed pool_get in 1.163s
76 Completed urllib_get in 2.318s
77 """
Something went wrong with that request. Please try again.