]> git.rkrishnan.org Git - tahoe-lafs/zfec.git/blob - zfec/bench/bench_zfec.py
whitespace, docstrings, copyright statements
[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
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):
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():
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     for f in [_encode_data_fec,]:
78         for BSIZE in [2**22]:
79             benchutil.rep_bench(f, n=BSIZE, initfunc=_make_new_rand_data, MAXREPS=64, MAXTIME=None)
80
81 bench()