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