summaryrefslogtreecommitdiff
path: root/src/pycryptopp/bench/common.py
blob: 24ad619731248238bcdbe842a83127718681b325 (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
msg = "crypto libraries should come with benchmarks"

try:
    import pyutil.benchutil
    rep_bench = pyutil.benchutil.rep_bench
    print_bench_footer = pyutil.benchutil.print_bench_footer
except ImportError:
    def this_rep_bench(func, N, UNITS_PER_SECOND, MAXTIME, MAXREPS, initfunc=None):
        import time

        if initfunc is not None:
            initfunc(N)

        mean = 0
        MAXREPS = 100

        timeout = time.time() + MAXTIME

        for i in range(MAXREPS):
            start = time.time()

            func(N)

            stop = time.time()

            mean += (stop - start)

            if stop >= timeout:
                break

        num = i+1
        mean *= UNITS_PER_SECOND
        mean /= num

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

    def this_print_bench_footer(UNITS_PER_SECOND=1):
        from decimal import Decimal
        print "time units per second: %s; seconds per time unit: %s" % (UNITS_PER_SECOND, Decimal(1)/UNITS_PER_SECOND)

    rep_bench = this_rep_bench
    print_bench_footer = this_print_bench_footer

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