]> git.rkrishnan.org Git - tahoe-lafs/zfec.git/blob - zfec/bench/bench_zfec.py
bench: take --k= and --m= options on the cmdline and print out more explanation of...
[tahoe-lafs/zfec.git] / zfec / bench / bench_zfec.py
1 from zfec import easyfec, Encoder, filefec
2 from pyutil import mathutil
3
4 import os, sys
5
6 from pyutil import benchutil
7
8 FNAME="benchrandom.data"
9
10 def _make_new_rand_file(size):
11     open(FNAME, "wb").write(os.urandom(size))
12
13 def donothing(results, reslenthing):
14     pass
15
16 K=3
17 M=10
18
19 d = ""
20 ds = []
21 easyfecenc = None
22 fecenc = None
23 def _make_new_rand_data(size, k, m):
24     global d, easyfecenc, fecenc
25     d = os.urandom(size)
26     del ds[:]
27     ds.extend([None]*k)
28     blocksize = mathutil.div_ceil(size, k)
29     for i in range(k):
30         ds[i] = d[i*blocksize:(i+1)*blocksize]
31     ds[-1] = ds[-1] + "\x00" * (len(ds[-2]) - len(ds[-1]))
32     easyfecenc = easyfec.Encoder(k, m)
33     fecenc = Encoder(k, m)
34
35 import sha
36 hashers = [ sha.new() for i in range(M) ]
37 def hashem(results, reslenthing):
38     for i, result in enumerate(results):
39         hashers[i].update(result)
40
41 def _encode_file(N):
42     filefec.encode_file(open(FNAME, "rb"), donothing, K, M)
43
44 def _encode_file_stringy(N):
45     filefec.encode_file_stringy(open(FNAME, "rb"), donothing, K, M)
46
47 def _encode_file_stringy_easyfec(N):
48     filefec.encode_file_stringy_easyfec(open(FNAME, "rb"), donothing, K, M)
49
50 def _encode_file_not_really(N):
51     filefec.encode_file_not_really(open(FNAME, "rb"), donothing, K, M)
52
53 def _encode_file_not_really_and_hash(N):
54     filefec.encode_file_not_really_and_hash(open(FNAME, "rb"), donothing, K, M)
55
56 def _encode_file_and_hash(N):
57     filefec.encode_file(open(FNAME, "rb"), hashem, K, M)
58
59 def _encode_data_not_really(N):
60     i = 0
61     for c in d:
62         i += 1
63     assert len(d) == N == i
64     pass
65
66 def _encode_data_easyfec(N):
67     easyfecenc.encode(d)
68
69 def _encode_data_fec(N):
70     fecenc.encode(ds)
71
72 def bench(k, m):
73     # for f in [_encode_file_stringy_easyfec, _encode_file_stringy, _encode_file, _encode_file_not_really,]:
74     # for f in [_encode_file,]:
75     # for f in [_encode_file_not_really, _encode_file_not_really_and_hash, _encode_file, _encode_file_and_hash,]:
76     # for f in [_encode_data_not_really, _encode_data_easyfec, _encode_data_fec,]:
77     print "measuring encoding of data with K=%d, M=%d, reporting results in nanoseconds per byte after encoding 4 MB..." % (k, m)
78     for f in [_encode_data_fec,]:
79         def _init_func(size):
80             return _make_new_rand_data(size, k, m)
81         for BSIZE in [2**22]:
82             benchutil.rep_bench(f, n=BSIZE, initfunc=_init_func, MAXREPS=64, MAXTIME=None, UNITS_PER_SECOND=1000000000)
83             benchutil.print_bench_footer(UNITS_PER_SECOND=1000000000)
84
85 k = K
86 m = M
87 for arg in sys.argv:
88     if arg.startswith('--k='):
89         k = int(arg[len('--k='):])
90     if arg.startswith('--m='):
91         m = int(arg[len('--m='):])
92
93 bench(k, m)