ยปCore Development>Code coverage>Lib/test/time_hashlib.py

Python code coverage for Lib/test/time_hashlib.py

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