diff options
| author | Eli Collins <elic@assurancetechnologies.com> | 2016-06-10 13:56:58 -0400 |
|---|---|---|
| committer | Eli Collins <elic@assurancetechnologies.com> | 2016-06-10 13:56:58 -0400 |
| commit | b03ee9e4e41c6d0f982a0b04f0cd4fb8a13c04d2 (patch) | |
| tree | b4b3f00cade0b0370d81868d83d95b501d00aefc /passlib | |
| parent | dbf60ebae71568df71476910f5d387b3109f03b3 (diff) | |
| download | passlib-b03ee9e4e41c6d0f982a0b04f0cd4fb8a13c04d2.tar.gz | |
passlib.tests.test_totp: use proper timing tool for runtime measurement,
prevents some spurious test failures when under erratic system load
Diffstat (limited to 'passlib')
| -rw-r--r-- | passlib/tests/test_totp.py | 15 | ||||
| -rw-r--r-- | passlib/tests/utils.py | 18 |
2 files changed, 24 insertions, 9 deletions
diff --git a/passlib/tests/test_totp.py b/passlib/tests/test_totp.py index d3668e1..e054131 100644 --- a/passlib/tests/test_totp.py +++ b/passlib/tests/test_totp.py @@ -7,16 +7,16 @@ from __future__ import unicode_literals from passlib.utils.compat import PY3 import base64 import datetime +from functools import partial import logging; log = logging.getLogger(__name__) import random import sys -import time as _time # site # pkg from passlib import exc from passlib.utils import to_bytes, to_unicode from passlib.utils.compat import unicode, u -from passlib.tests.utils import TestCase +from passlib.tests.utils import TestCase, time_call # local __all__ = [ "EngineTest", @@ -190,15 +190,12 @@ class UtilsTest(TestCase): self.addCleanup(setattr, totp, "ENCRYPT_COST", totp.ENCRYPT_COST) # time default cost - start = _time.clock() - _ = encrypt_key(KEY1_RAW, PASS1) - delta = _time.clock() - start + totp.ENCRYPT_COST -= 2 + delta, _ = time_call(partial(encrypt_key, KEY1_RAW, PASS1), maxtime=0) - # this should take 8x as long + # this should take (2**3=8) times as long totp.ENCRYPT_COST += 3 - start = _time.clock() - _ = encrypt_key(KEY1_RAW, PASS1) - delta2 = _time.clock() - start + delta2, _ = time_call(partial(encrypt_key, KEY1_RAW, PASS1), maxtime=0) self.assertAlmostEqual(delta2, delta*8, delta=(delta*8)*0.5) diff --git a/passlib/tests/utils.py b/passlib/tests/utils.py index 1467a5b..c1f9a10 100644 --- a/passlib/tests/utils.py +++ b/passlib/tests/utils.py @@ -240,6 +240,24 @@ def quicksleep(delay): while tick()-start < delay: pass +def time_call(func, setup=None, maxtime=1, bestof=3): + """ + timeit() wrapper which tries to get as accurate a measurement as possible w/in maxtime seconds. + + :returns: + ``(avg_seconds_per_call, log10_number_of_repetitions)`` + """ + from timeit import Timer + from math import log + timer = Timer(func, setup=setup or '') + number = 1 + while True: + delta = min(timer.repeat(bestof, number)) + maxtime -= delta*bestof + if maxtime < 0: + return delta/number, int(log(number, 10)) + number *= 10 + #============================================================================= # custom test harness #============================================================================= |
