time_hashlib.py 2.9 KB

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