Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1 | |
| 2 | import sys, time |
| 3 | import hashlib |
| 4 | from test import test_support |
| 5 | |
| 6 | |
| 7 | def creatorFunc(): |
| 8 | raise RuntimeError, "eek, creatorFunc not overridden" |
| 9 | |
| 10 | |
| 11 | def test_scaled_msg(scale, name): |
| 12 | |
| 13 | iterations = 106201/scale * 20 |
| 14 | longStr = 'Z'*scale |
| 15 | |
| 16 | localCF = creatorFunc |
| 17 | start = time.time() |
| 18 | for f in xrange(iterations): |
| 19 | x = localCF(longStr).digest() |
| 20 | end = time.time() |
| 21 | |
| 22 | print ('%2.2f' % (end-start)), "seconds", iterations, "x", len(longStr), "bytes", name |
| 23 | |
| 24 | def test_create(): |
| 25 | start = time.time() |
| 26 | for f in xrange(20000): |
| 27 | d = creatorFunc() |
| 28 | end = time.time() |
| 29 | |
| 30 | print ('%2.2f' % (end-start)), "seconds", '[20000 creations]' |
| 31 | |
| 32 | def test_zero(): |
| 33 | start = time.time() |
| 34 | for f in xrange(20000): |
| 35 | x = creatorFunc().digest() |
| 36 | end = time.time() |
| 37 | |
| 38 | print ('%2.2f' % (end-start)), "seconds", '[20000 "" digests]' |
| 39 | |
| 40 | |
| 41 | |
| 42 | ### this 'test' is not normally run. skip it if the test runner finds it |
| 43 | if __name__ != '__main__': |
| 44 | raise test_support.TestSkipped, "not a unit test (stand alone benchmark)" |
| 45 | |
| 46 | hName = sys.argv[1] |
| 47 | |
| 48 | # |
| 49 | # setup our creatorFunc to test the requested hash |
| 50 | # |
| 51 | if hName in ('_md5', '_sha'): |
| 52 | exec 'import '+hName |
| 53 | exec 'creatorFunc = '+hName+'.new' |
| 54 | print "testing speed of old", hName, "legacy interface" |
| 55 | elif hName == '_hashlib' and len(sys.argv) > 3: |
| 56 | import _hashlib |
| 57 | exec 'creatorFunc = _hashlib.%s' % sys.argv[2] |
| 58 | print "testing speed of _hashlib.%s" % sys.argv[2], getattr(_hashlib, sys.argv[2]) |
| 59 | elif hName == '_hashlib' and len(sys.argv) == 3: |
| 60 | import _hashlib |
| 61 | exec 'creatorFunc = lambda x=_hashlib.new : x(%r)' % sys.argv[2] |
| 62 | print "testing speed of _hashlib.new(%r)" % sys.argv[2] |
| 63 | elif hasattr(hashlib, hName) and callable(getattr(hashlib, hName)): |
| 64 | creatorFunc = getattr(hashlib, hName) |
| 65 | print "testing speed of hashlib."+hName, getattr(hashlib, hName) |
| 66 | else: |
| 67 | exec "creatorFunc = lambda x=hashlib.new : x(%r)" % hName |
| 68 | print "testing speed of hashlib.new(%r)" % hName |
| 69 | |
| 70 | try: |
| 71 | test_create() |
| 72 | except ValueError: |
| 73 | print |
| 74 | print "pass argument(s) naming the hash to run a speed test on:" |
| 75 | print " '_md5' and '_sha' test the legacy builtin md5 and sha" |
| 76 | print " '_hashlib' 'openssl_hName' 'fast' tests the builtin _hashlib" |
| 77 | print " '_hashlib' 'hName' tests builtin _hashlib.new(shaFOO)" |
| 78 | print " 'hName' tests the hashlib.hName() implementation if it exists" |
| 79 | print " otherwise it uses hashlib.new(hName)." |
| 80 | print |
| 81 | raise |
| 82 | |
| 83 | test_zero() |
| 84 | test_scaled_msg(scale=106201, name='[huge data]') |
| 85 | test_scaled_msg(scale=10620, name='[large data]') |
| 86 | test_scaled_msg(scale=1062, name='[medium data]') |
| 87 | test_scaled_msg(scale=424, name='[4*small data]') |
| 88 | test_scaled_msg(scale=336, name='[3*small data]') |
| 89 | test_scaled_msg(scale=212, name='[2*small data]') |
| 90 | test_scaled_msg(scale=106, name='[small data]') |
| 91 | test_scaled_msg(scale=creatorFunc().digest_size, name='[digest_size data]') |
| 92 | test_scaled_msg(scale=10, name='[tiny data]') |