]> git.rkrishnan.org Git - tahoe-lafs/zfec.git/blob - zfec/bench/bench_zfec.py
93cb6254e3116dcb69dbaeaa3358aee7c25c3a6b
[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     SIZE = 10**6
74     MAXREPS = 64
75     # for f in [_encode_file_stringy_easyfec, _encode_file_stringy, _encode_file, _encode_file_not_really,]:
76     # for f in [_encode_file,]:
77     # for f in [_encode_file_not_really, _encode_file_not_really_and_hash, _encode_file, _encode_file_and_hash,]:
78     # for f in [_encode_data_not_really, _encode_data_easyfec, _encode_data_fec,]:
79     print "measuring encoding of data with K=%d, M=%d, reporting results in nanoseconds per byte after encoding %d bytes %d times in a row..." % (k, m, SIZE, MAXREPS)
80     for f in [_encode_data_fec,]:
81         def _init_func(size):
82             return _make_new_rand_data(size, k, m)
83         for BSIZE in [SIZE]:
84             results = benchutil.rep_bench(f, n=BSIZE, initfunc=_init_func, MAXREPS=MAXREPS, MAXTIME=None, UNITS_PER_SECOND=1000000000)
85             print "and now represented in MB/s..."
86             print
87             best = results['best']
88             mean = results['mean']
89             worst = results['worst']
90             print "best:  % 4.3f MB/sec" % (10**3 / best)
91             print "mean:  % 4.3f MB/sec" % (10**3 / mean)
92             print "worst: % 4.3f MB/sec" % (10**3 / worst)
93
94 k = K
95 m = M
96 for arg in sys.argv:
97     if arg.startswith('--k='):
98         k = int(arg[len('--k='):])
99     if arg.startswith('--m='):
100         m = int(arg[len('--m='):])
101
102 bench(k, m)