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