| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import sys |
| 5 |
import time |
| 6 |
import hashlib |
| 7 |
|
| 8 |
|
| 9 |
def creatorFunc(): |
| 10 |
raise RuntimeError("eek, creatorFunc not overridden") |
| 11 |
|
| 12 |
def test_scaled_msg(scale, name): |
| 13 |
iterations = 106201//scale * 20 |
| 14 |
longStr = b'Z'*scale |
| 15 |
|
| 16 |
localCF = creatorFunc |
| 17 |
start = time.perf_counter() |
| 18 |
for f in range(iterations): |
| 19 |
x = localCF(longStr).digest() |
| 20 |
end = time.perf_counter() |
| 21 |
|
| 22 |
print(('%2.2f' % (end-start)), "seconds", iterations, "x", len(longStr), "bytes", name) |
| 23 |
|
| 24 |
def test_create(): |
| 25 |
start = time.perf_counter() |
| 26 |
for f in range(20000): |
| 27 |
d = creatorFunc() |
| 28 |
end = time.perf_counter() |
| 29 |
|
| 30 |
print(('%2.2f' % (end-start)), "seconds", '[20000 creations]') |
| 31 |
|
| 32 |
def test_zero(): |
| 33 |
start = time.perf_counter() |
| 34 |
for f in range(20000): |
| 35 |
x = creatorFunc().digest() |
| 36 |
end = time.perf_counter() |
| 37 |
|
| 38 |
print(('%2.2f' % (end-start)), "seconds", '[20000 "" digests]') |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
hName = sys.argv[1] |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
if hName in ('_md5', '_sha'): |
| 48 |
exec('import '+hName) |
| 49 |
exec('creatorFunc = '+hName+'.new') |
| 50 |
print("testing speed of old", hName, "legacy interface") |
| 51 |
elif hName == '_hashlib' and len(sys.argv) > 3: |
| 52 |
import _hashlib |
| 53 |
exec('creatorFunc = _hashlib.%s' % sys.argv[2]) |
| 54 |
print("testing speed of _hashlib.%s" % sys.argv[2], getattr(_hashlib, sys.argv[2])) |
| 55 |
elif hName == '_hashlib' and len(sys.argv) == 3: |
| 56 |
import _hashlib |
| 57 |
exec('creatorFunc = lambda x=_hashlib.new : x(%r)' % sys.argv[2]) |
| 58 |
print("testing speed of _hashlib.new(%r)" % sys.argv[2]) |
| 59 |
elif hasattr(hashlib, hName) and hasattr(getattr(hashlib, hName), '__call__'): |
| 60 |
creatorFunc = getattr(hashlib, hName) |
| 61 |
print("testing speed of hashlib."+hName, getattr(hashlib, hName)) |
| 62 |
else: |
| 63 |
exec("creatorFunc = lambda x=hashlib.new : x(%r)" % hName) |
| 64 |
print("testing speed of hashlib.new(%r)" % hName) |
| 65 |
|
| 66 |
try: |
| 67 |
test_create() |
| 68 |
except ValueError: |
| 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() |
| 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]') |