httplib Performance Tests
25 March 2008
15:24
as NumPy
You'd think that the second test function (test_shared) would run faster because it reuses the connection object across requests. However, when combined with chunked Transfer-Encoding, it is actually significantly slower on Linux (see next paste for results). On OS X it is faster, as expected.
| 1 | #!/usr/bin/env python |
|---|---|
| 2 | |
| 3 | from __future__ import division |
| 4 | import httplib |
| 5 | import gc |
| 6 | import sys |
| 7 | from time import time |
| 8 | |
| 9 | TEST_HOST = '127.0.0.1:5984' |
| 10 | TEST_PATH = '/' |
| 11 | |
| 12 | testfuncs = {} |
| 13 | |
| 14 | def test_simple(host, path): |
| 15 | def run(): |
| 16 | conn = httplib.HTTPConnection(host) |
| 17 | conn.request('GET', path) |
| 18 | resp = conn.getresponse() |
| 19 | assert resp.status < 400 |
| 20 | return resp.read() |
| 21 | return run |
| 22 | testfuncs['simple'] = test_simple |
| 23 | |
| 24 | def test_shared(host, path): |
| 25 | conn = httplib.HTTPConnection(host) |
| 26 | def run(): |
| 27 | conn.request('GET', path) |
| 28 | resp = conn.getresponse() |
| 29 | assert resp.status < 400 |
| 30 | return resp.read() |
| 31 | return run |
| 32 | testfuncs['shared'] = test_shared |
| 33 | |
| 34 | def measure(func, *args, **kwargs): |
| 35 | start = time() |
| 36 | func(*args, **kwargs) |
| 37 | return time() - start |
| 38 | |
| 39 | def test(tests, host, path, number=25): |
| 40 | gc.disable() |
| 41 | for testname in tests: |
| 42 | testfunc = testfuncs[testname](host, path) |
| 43 | results = [] |
| 44 | for idx in range(number): |
| 45 | secs = measure(testfunc) |
| 46 | print '%10s (%2d): GET %6.2fms' %\ |
| 47 | (testname, idx, secs * 1000) |
| 48 | results.append(secs) |
| 49 | |
| 50 | if __name__ == '__main__': |
| 51 | from optparse import OptionParser |
| 52 | parser = OptionParser(usage='%prog [options] test1 ...') |
| 53 | parser.add_option('-n', '--number', dest='number', type='int') |
| 54 | parser.add_option('-p', '--profile', action='store_true', dest='profile') |
| 55 | parser.add_option('-H', '--host', dest='host') |
| 56 | parser.add_option('-P', '--path', dest='path') |
| 57 | parser.set_defaults(number=100, profile=False, host=TEST_HOST, path=TEST_PATH) |
| 58 | options, tests = parser.parse_args() |
| 59 | |
| 60 | if options.profile: |
| 61 | import hotshot, hotshot.stats |
| 62 | prof = hotshot.Profile("httplib_test.prof") |
| 63 | benchtime = prof.runcall(test, tests, options.host, options.path, |
| 64 | number=options.number) |
| 65 | stats = hotshot.stats.load("httplib_test.prof") |
| 66 | stats.strip_dirs() |
| 67 | stats.sort_stats('time', 'calls') |
| 68 | stats.print_stats(.05) |
| 69 | else: |
| 70 | test(tests, options.host, options.path, number=options.number) |
Comments
No comments so far.
