time_hashlib.py 2.8 KB

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