Skip to content

Commit

Permalink
add 'setup.py speed' to run benchmarks
Browse files Browse the repository at this point in the history
refs #30
  • Loading branch information
warner committed Dec 16, 2014
1 parent a595fd7 commit 23dfe82
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -9,3 +9,4 @@ python:
install: true
script:
- python setup.py test
- python setup.py speed
39 changes: 39 additions & 0 deletions setup.py
Expand Up @@ -5,6 +5,7 @@
except ImportError:
# but most users really don't require it
from distutils.core import setup, Command
import timeit

import versioneer
versioneer.versionfile_source = "ecdsa/_version.py"
Expand Down Expand Up @@ -35,6 +36,44 @@ def run(self):
# all tests os.exit(1) upon failure
commands["test"] = Test

class Speed(Test):
description = "run benchmark suite"
def run(self):
def do(setup_statements, statement):
# extracted from timeit.py
t = timeit.Timer(stmt=statement,
setup="\n".join(setup_statements))
# determine number so that 0.2 <= total time < 2.0
for i in range(1, 10):
number = 10**i
x = t.timeit(number)
if x >= 0.2:
break
return x / number

for curve in ["NIST192p", "NIST224p", "NIST256p", "SECP256k1",
"NIST384p", "NIST521p"]:
S1 = "import ecdsa"
S2 = "sk = ecdsa.SigningKey.generate(ecdsa.%s)" % curve
S3 = "msg = 'msg'"
S4 = "sig = sk.sign(msg)"
S5 = "vk = sk.get_verifying_key()"
S6 = "vk.verify(sig, msg)"
# We happen to know that .generate() also calculates the
# verifying key, which is the time-consuming part. If the code
# were changed to lazily calculate vk, we'd need to change this
# benchmark to loop over S5 instead of S2
keygen = do([S1], S2)
sign = do([S1,S2,S3], S4)
verf = do([S1,S2,S3,S4,S5], S6)
import ecdsa
sig = ecdsa.SigningKey.generate(getattr(ecdsa, curve)).sign("msg")
print "%9s: siglen=%3d, keygen=%.3fs, sign=%.3fs, verify=%.3fs" \
% (curve, len(sig), keygen, sign, verf)

commands["speed"] = Speed


setup(name="ecdsa",
version=versioneer.get_version(),
description="ECDSA cryptographic signature library (pure python)",
Expand Down

0 comments on commit 23dfe82

Please sign in to comment.