blob: 55ebac62912fe1dd744fe58bdfca0617c8e1fdac [file] [log] [blame]
Thomas Woutersa9773292006-04-21 09:43:23 +00001# 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. Smithf21a5f72005-08-21 18:45:59 +00003
Gregory P. Smith5719ef12015-01-04 00:36:04 -08004import sys
5import time
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00006import hashlib
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00007
8
9def creatorFunc():
Collin Winter3add4d72007-08-29 23:37:32 +000010 raise RuntimeError("eek, creatorFunc not overridden")
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000011
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000012def test_scaled_msg(scale, name):
Gregory P. Smith5719ef12015-01-04 00:36:04 -080013 iterations = 106201//scale * 20
14 longStr = b'Z'*scale
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000015
16 localCF = creatorFunc
Victor Stinner8db5b542018-12-17 11:30:34 +010017 start = time.perf_counter()
Guido van Rossum805365e2007-05-07 22:24:25 +000018 for f in range(iterations):
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000019 x = localCF(longStr).digest()
Victor Stinner8db5b542018-12-17 11:30:34 +010020 end = time.perf_counter()
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000021
Guido van Rossumbe19ed72007-02-09 05:37:30 +000022 print(('%2.2f' % (end-start)), "seconds", iterations, "x", len(longStr), "bytes", name)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000023
24def test_create():
Victor Stinner8db5b542018-12-17 11:30:34 +010025 start = time.perf_counter()
Guido van Rossum805365e2007-05-07 22:24:25 +000026 for f in range(20000):
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000027 d = creatorFunc()
Victor Stinner8db5b542018-12-17 11:30:34 +010028 end = time.perf_counter()
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000029
Guido van Rossumbe19ed72007-02-09 05:37:30 +000030 print(('%2.2f' % (end-start)), "seconds", '[20000 creations]')
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000031
32def test_zero():
Victor Stinner8db5b542018-12-17 11:30:34 +010033 start = time.perf_counter()
Guido van Rossum805365e2007-05-07 22:24:25 +000034 for f in range(20000):
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000035 x = creatorFunc().digest()
Victor Stinner8db5b542018-12-17 11:30:34 +010036 end = time.perf_counter()
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000037
Guido van Rossumbe19ed72007-02-09 05:37:30 +000038 print(('%2.2f' % (end-start)), "seconds", '[20000 "" digests]')
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000039
40
41
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000042hName = sys.argv[1]
43
44#
45# setup our creatorFunc to test the requested hash
46#
47if hName in ('_md5', '_sha'):
Georg Brandl7cae87c2006-09-06 06:51:57 +000048 exec('import '+hName)
49 exec('creatorFunc = '+hName+'.new')
Guido van Rossumbe19ed72007-02-09 05:37:30 +000050 print("testing speed of old", hName, "legacy interface")
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000051elif hName == '_hashlib' and len(sys.argv) > 3:
52 import _hashlib
Georg Brandl7cae87c2006-09-06 06:51:57 +000053 exec('creatorFunc = _hashlib.%s' % sys.argv[2])
Guido van Rossumbe19ed72007-02-09 05:37:30 +000054 print("testing speed of _hashlib.%s" % sys.argv[2], getattr(_hashlib, sys.argv[2]))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000055elif hName == '_hashlib' and len(sys.argv) == 3:
56 import _hashlib
Georg Brandl7cae87c2006-09-06 06:51:57 +000057 exec('creatorFunc = lambda x=_hashlib.new : x(%r)' % sys.argv[2])
Guido van Rossumbe19ed72007-02-09 05:37:30 +000058 print("testing speed of _hashlib.new(%r)" % sys.argv[2])
Guido van Rossumd59da4b2007-05-22 18:11:13 +000059elif hasattr(hashlib, hName) and hasattr(getattr(hashlib, hName), '__call__'):
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000060 creatorFunc = getattr(hashlib, hName)
Guido van Rossumbe19ed72007-02-09 05:37:30 +000061 print("testing speed of hashlib."+hName, getattr(hashlib, hName))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000062else:
Georg Brandl7cae87c2006-09-06 06:51:57 +000063 exec("creatorFunc = lambda x=hashlib.new : x(%r)" % hName)
Guido van Rossumbe19ed72007-02-09 05:37:30 +000064 print("testing speed of hashlib.new(%r)" % hName)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000065
66try:
67 test_create()
68except ValueError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000069 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. Smithf21a5f72005-08-21 18:45:59 +000077 raise
78
79test_zero()
80test_scaled_msg(scale=106201, name='[huge data]')
81test_scaled_msg(scale=10620, name='[large data]')
82test_scaled_msg(scale=1062, name='[medium data]')
83test_scaled_msg(scale=424, name='[4*small data]')
84test_scaled_msg(scale=336, name='[3*small data]')
85test_scaled_msg(scale=212, name='[2*small data]')
86test_scaled_msg(scale=106, name='[small data]')
87test_scaled_msg(scale=creatorFunc().digest_size, name='[digest_size data]')
88test_scaled_msg(scale=10, name='[tiny data]')