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