10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# It's intended that this script be run by hand. It runs speed tests on 20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# hashlib functions; it does not test for correctness. 30a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 40a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys, time 50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport hashlib 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 80a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef creatorFunc(): 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise RuntimeError, "eek, creatorFunc not overridden" 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_scaled_msg(scale, name): 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao iterations = 106201/scale * 20 130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao longStr = 'Z'*scale 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao localCF = creatorFunc 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao start = time.time() 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for f in xrange(iterations): 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = localCF(longStr).digest() 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao end = time.time() 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print ('%2.2f' % (end-start)), "seconds", iterations, "x", len(longStr), "bytes", name 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_create(): 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao start = time.time() 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for f in xrange(20000): 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = creatorFunc() 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao end = time.time() 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print ('%2.2f' % (end-start)), "seconds", '[20000 creations]' 300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_zero(): 320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao start = time.time() 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for f in xrange(20000): 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = creatorFunc().digest() 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao end = time.time() 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print ('%2.2f' % (end-start)), "seconds", '[20000 "" digests]' 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 410a8c90248264a8b26970b4473770bcc3df8515fJosh GaohName = sys.argv[1] 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# setup our creatorFunc to test the requested hash 450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif hName in ('_md5', '_sha'): 470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exec 'import '+hName 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exec 'creatorFunc = '+hName+'.new' 490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print "testing speed of old", hName, "legacy interface" 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoelif hName == '_hashlib' and len(sys.argv) > 3: 510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import _hashlib 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exec 'creatorFunc = _hashlib.%s' % sys.argv[2] 530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print "testing speed of _hashlib.%s" % sys.argv[2], getattr(_hashlib, sys.argv[2]) 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoelif hName == '_hashlib' and len(sys.argv) == 3: 550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import _hashlib 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exec 'creatorFunc = lambda x=_hashlib.new : x(%r)' % sys.argv[2] 570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print "testing speed of _hashlib.new(%r)" % sys.argv[2] 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoelif hasattr(hashlib, hName) and callable(getattr(hashlib, hName)): 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao creatorFunc = getattr(hashlib, hName) 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print "testing speed of hashlib."+hName, getattr(hashlib, hName) 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoelse: 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exec "creatorFunc = lambda x=hashlib.new : x(%r)" % hName 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print "testing speed of hashlib.new(%r)" % hName 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotry: 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_create() 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoexcept ValueError: 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print "pass argument(s) naming the hash to run a speed test on:" 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print " '_md5' and '_sha' test the legacy builtin md5 and sha" 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print " '_hashlib' 'openssl_hName' 'fast' tests the builtin _hashlib" 720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print " '_hashlib' 'hName' tests builtin _hashlib.new(shaFOO)" 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print " 'hName' tests the hashlib.hName() implementation if it exists" 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print " otherwise it uses hashlib.new(hName)." 750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print 760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise 770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 780a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotest_zero() 790a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotest_scaled_msg(scale=106201, name='[huge data]') 800a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotest_scaled_msg(scale=10620, name='[large data]') 810a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotest_scaled_msg(scale=1062, name='[medium data]') 820a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotest_scaled_msg(scale=424, name='[4*small data]') 830a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotest_scaled_msg(scale=336, name='[3*small data]') 840a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotest_scaled_msg(scale=212, name='[2*small data]') 850a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotest_scaled_msg(scale=106, name='[small data]') 860a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotest_scaled_msg(scale=creatorFunc().digest_size, name='[digest_size data]') 870a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotest_scaled_msg(scale=10, name='[tiny data]') 88