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