time_hashlib.py 2.82 KB
Newer Older
1 2
# It's intended that this script be run by hand.  It runs speed tests on
# hashlib functions; it does not test for correctness.
3 4 5 6 7 8

import sys, time
import hashlib


def creatorFunc():
9
    raise RuntimeError("eek, creatorFunc not overridden")
10 11 12 13 14 15 16

def test_scaled_msg(scale, name):
    iterations = 106201/scale * 20
    longStr = 'Z'*scale

    localCF = creatorFunc
    start = time.time()
17
    for f in range(iterations):
18 19 20
        x = localCF(longStr).digest()
    end = time.time()

21
    print(('%2.2f' % (end-start)), "seconds", iterations, "x", len(longStr), "bytes", name)
22 23 24

def test_create():
    start = time.time()
25
    for f in range(20000):
26 27 28
        d = creatorFunc()
    end = time.time()

29
    print(('%2.2f' % (end-start)), "seconds", '[20000 creations]')
30 31 32

def test_zero():
    start = time.time()
33
    for f in range(20000):
34 35 36
        x = creatorFunc().digest()
    end = time.time()

37
    print(('%2.2f' % (end-start)), "seconds", '[20000 "" digests]')
38 39 40 41 42 43 44 45 46



hName = sys.argv[1]

#
# setup our creatorFunc to test the requested hash
#
if hName in ('_md5', '_sha'):
47 48
    exec('import '+hName)
    exec('creatorFunc = '+hName+'.new')
49
    print("testing speed of old", hName, "legacy interface")
50 51
elif hName == '_hashlib' and len(sys.argv) > 3:
    import _hashlib
52
    exec('creatorFunc = _hashlib.%s' % sys.argv[2])
53
    print("testing speed of _hashlib.%s" % sys.argv[2], getattr(_hashlib, sys.argv[2]))
54 55
elif hName == '_hashlib' and len(sys.argv) == 3:
    import _hashlib
56
    exec('creatorFunc = lambda x=_hashlib.new : x(%r)' % sys.argv[2])
57
    print("testing speed of _hashlib.new(%r)" % sys.argv[2])
58
elif hasattr(hashlib, hName) and hasattr(getattr(hashlib, hName), '__call__'):
59
    creatorFunc = getattr(hashlib, hName)
60
    print("testing speed of hashlib."+hName, getattr(hashlib, hName))
61
else:
62
    exec("creatorFunc = lambda x=hashlib.new : x(%r)" % hName)
63
    print("testing speed of hashlib.new(%r)" % hName)
64 65 66 67

try:
    test_create()
except ValueError:
68 69 70 71 72 73 74 75
    print()
    print("pass argument(s) naming the hash to run a speed test on:")
    print(" '_md5' and '_sha' test the legacy builtin md5 and sha")
    print(" '_hashlib' 'openssl_hName' 'fast' tests the builtin _hashlib")
    print(" '_hashlib' 'hName' tests builtin _hashlib.new(shaFOO)")
    print(" 'hName' tests the hashlib.hName() implementation if it exists")
    print("         otherwise it uses hashlib.new(hName).")
    print()
76 77 78 79 80 81 82 83 84 85 86 87
    raise

test_zero()
test_scaled_msg(scale=106201, name='[huge data]')
test_scaled_msg(scale=10620, name='[large data]')
test_scaled_msg(scale=1062, name='[medium data]')
test_scaled_msg(scale=424, name='[4*small data]')
test_scaled_msg(scale=336, name='[3*small data]')
test_scaled_msg(scale=212, name='[2*small data]')
test_scaled_msg(scale=106, name='[small data]')
test_scaled_msg(scale=creatorFunc().digest_size, name='[digest_size data]')
test_scaled_msg(scale=10, name='[tiny data]')