1 import hotshot.stats, os, random, sys
3 from pyutil import benchutil, randutil # http://allmydata.org/trac/pyutil
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
10 class FakeClient(client.Client):
14 download_cachedir = fileutil.NamedTemporaryDirectory()
15 self.download_cache_dirman = cachedir.CacheDirectoryManager(download_cachedir.name)
16 def getServiceNamed(self, name):
18 def get_encoding_parameters(self):
19 return {"k": 3, "n": 10}
20 def get_writekey(self):
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))
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)
36 children = [] # tuples of (k, v) (suitable for passing to dict())
38 fakeclient = FakeClient()
39 testdirnode = dirnode.DirectoryNode(fakeclient)
40 testdirnode.init_from_uri(uri.DirectoryURI(uri.WriteableSSKFileURI(randutil.insecurerandstr(16), randutil.insecurerandstr(32))))
42 def random_unicode(l):
45 return os.urandom(l).decode('utf-8')
46 except UnicodeDecodeError:
50 coin = random.randrange(0, 3)
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)
54 return FakeMutableFileNode(fakeclient)
57 return FakeDirectoryNode(fakeclient)
59 def random_metadata():
61 d['ctime'] = random.random()
62 d['mtime'] = random.random()
64 d['tahoe']['linkcrtime'] = random.random()
65 d['tahoe']['linkmotime'] = random.random()
69 return random_fsnode(), random_metadata()
72 for i in xrange(len(children), N):
73 children.append((random_unicode(random.randrange(1, 9)), random_child()))
75 def init_for_unpack(N):
81 return testdirnode._pack_contents(dirnode.CachingDict(children[:N]))
84 return testdirnode._unpack_contents(packstr)
86 def unpack_and_repack(N):
87 return testdirnode._pack_contents(testdirnode._unpack_contents(packstr))
89 PROF_FILE_NAME="bench_dirnode.prof"
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)
97 s = hotshot.stats.load(PROF_FILE_NAME)
98 s.strip_dirs().sort_stats("time").print_stats(32)
100 def prof_benchmarks():
101 # This requires pyutil >= v1.3.34.
102 run_benchmarks(profile=True)
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."