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