summaryrefslogtreecommitdiff
path: root/src/pycryptopp/bench/common.py
blob: 2d8f315c26bf97e8854d3626124492c545ccdde4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
msg = "crypto libraries should come with benchmarks"

try:
    import pyutil.benchutil
    rep_bench = pyutil.benchutil.rep_bench
except (ImportError, AttributeError):
    import platform, time
    if 'windows' in platform.system().lower():
        clock = time.clock
    else:
        clock = time.time

    def this_rep_bench(func, N, UNITS_PER_SECOND, MAXTIME, MAXREPS, initfunc=None):
        tt = time.time

        if initfunc is not None:
            initfunc(N)

        meanc = 0
        MAXREPS = 100

        timeout = tt() + MAXTIME

        for i in range(MAXREPS):
            startc = clock()

            func(N)

            stopc = clock()

            deltac = stopc - startc
            if deltac <= 0:
                print "clock jump backward or wrapped -- ignoring this sample. startc: %s, stopc: %s, deltac: %s" % (startc, stopc, deltac,)
            else:
                meanc += deltac

            if time.time() >= timeout:
                break

        num = i+1
        meanc *= UNITS_PER_SECOND
        meanc /= num
        meanc /= N

        res = {
            'meanc': meanc,
            'num': num
            }
        print "mean: %(meanc)#8.03e (of %(num)6d)" % res
    rep_bench = this_rep_bench

import random as insecurerandom
def insecurerandstr(n):
    return ''.join(map(chr, map(insecurerandom.randrange, [0]*n, [256]*n)))

def calib_clock():
    interval = 1.0
    
    import time
    tc = time.clock
    tt = time.time

    def measure_sleep(x, clock):
        st = clock()
        time.sleep(x)