]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/test/bench_dirnode.py
70b5ee4eb77a8f88f0f82d974ea8a2b34a997726
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / test / bench_dirnode.py
1 import hotshot.stats, os, random, sys
2
3 from pyutil import benchutil, randutil # http://allmydata.org/trac/pyutil
4
5 from allmydata import client, dirnode, uri
6 from allmydata.mutable import filenode as mut_filenode
7 from allmydata.immutable import filenode as immut_filenode
8 from allmydata.util import cachedir, fileutil
9
10 class FakeClient(client.Client):
11     # just enough
12     def __init__(self):
13         self._node_cache = {}
14         download_cachedir = fileutil.NamedTemporaryDirectory()
15         self.download_cache_dirman = cachedir.CacheDirectoryManager(download_cachedir.name)
16     def getServiceNamed(self, name):
17         return None
18     def get_encoding_parameters(self):
19         return {"k": 3, "n": 10}
20     def get_writekey(self):
21         return os.urandom(16)
22
23 class FakeMutableFileNode(mut_filenode.MutableFileNode):
24     def __init__(self, client):
25         mut_filenode.MutableFileNode.__init__(self, client)
26         self._uri = uri.WriteableSSKFileURI(randutil.insecurerandstr(16), randutil.insecurerandstr(32))
27
28 class FakeDirectoryNode(dirnode.DirectoryNode):
29     def __init__(self, client):
30         dirnode.DirectoryNode.__init__(self, client)
31         mutfileuri = uri.WriteableSSKFileURI(randutil.insecurerandstr(16), randutil.insecurerandstr(32))
32         myuri = uri.DirectoryURI(mutfileuri)
33         self.init_from_uri(myuri)
34
35
36 children = [] # tuples of (k, v) (suitable for passing to dict())
37 packstr = None
38 fakeclient = FakeClient()
39 testdirnode = dirnode.DirectoryNode(fakeclient)
40 testdirnode.init_from_uri(uri.DirectoryURI(uri.WriteableSSKFileURI(randutil.insecurerandstr(16), randutil.insecurerandstr(32))))
41
42 def random_unicode(l):
43     while True:
44         try:
45             return os.urandom(l).decode('utf-8')
46         except UnicodeDecodeError:
47             pass
48
49 def random_fsnode():
50     coin = random.randrange(0, 3)
51     if coin == 0:
52         return immut_filenode.FileNode(uri.CHKFileURI(randutil.insecurerandstr(16), randutil.insecurerandstr(32), random.randrange(1, 5), random.randrange(6, 15), random.randrange(99, 1000000000000)), fakeclient, None)
53     elif coin == 1:
54         return FakeMutableFileNode(fakeclient)
55     else:
56         assert coin == 2
57         return FakeDirectoryNode(fakeclient)
58
59 def random_metadata():
60     d = {}
61     d['ctime'] = random.random()
62     d['mtime'] = random.random()
63     d['tahoe'] = {}
64     d['tahoe']['linkcrtime'] = random.random()
65     d['tahoe']['linkmotime'] = random.random()
66     return d
67
68 def random_child():
69     return random_fsnode(), random_metadata()
70
71 def init_for_pack(N):
72     for i in xrange(len(children), N):
73         children.append((random_unicode(random.randrange(1, 9)), random_child()))
74
75 def init_for_unpack(N):
76     global packstr
77     init_for_pack(N)
78     packstr = pack(N)
79
80 def pack(N):
81     return testdirnode._pack_contents(dirnode.CachingDict(children[:N]))
82
83 def unpack(N):
84     return testdirnode._unpack_contents(packstr)
85
86 def unpack_and_repack(N):
87     return testdirnode._pack_contents(testdirnode._unpack_contents(packstr))
88
89 PROF_FILE_NAME="bench_dirnode.prof"
90
91 def run_benchmarks(profile=False):
92     for (func, initfunc) in [(unpack, init_for_unpack), (pack, init_for_pack), (unpack_and_repack, init_for_unpack)]:
93         print "benchmarking %s" % (func,)
94         benchutil.bench(unpack_and_repack, initfunc=init_for_unpack, TOPXP=12, profile=profile, profresults=PROF_FILE_NAME)
95
96 def print_stats():
97     s = hotshot.stats.load(PROF_FILE_NAME)
98     s.strip_dirs().sort_stats("time").print_stats(32)
99
100 def prof_benchmarks():
101     # This requires pyutil >= v1.3.34.
102     run_benchmarks(profile=True)
103
104 if __name__ == "__main__":
105     if '--profile' in sys.argv:
106         if os.path.exists(PROF_FILE_NAME):
107             print "WARNING: profiling results file '%s' already exists -- the profiling results from this run will be added into the profiling results stored in that file and then the sum of them will be printed out after this run." % (PROF_FILE_NAME,)
108         prof_benchmarks()
109         print_stats()
110     else:
111         run_benchmarks()