tahoe-lafs/src/allmydata/test/bench_dirnode.py

119 lines
4.1 KiB
Python
Raw Normal View History

import hotshot.stats, os, random, sys
from pyutil import benchutil, randutil # http://allmydata.org/trac/pyutil
from allmydata import dirnode, uri
from allmydata.mutable import filenode as mut_filenode
from allmydata.immutable import filenode as immut_filenode
children = [] # tuples of (k, v) (suitable for passing to dict())
packstr = None
class ContainerNode:
# dirnodes sit on top of a "container" filenode, from which it extracts a
# writekey
def __init__(self):
self._writekey = randutil.insecurerandstr(16)
self._fingerprint = randutil.insecurerandstr(32)
self._cap = uri.WriteableSSKFileURI(self._writekey, self._fingerprint)
def get_writekey(self):
return self._writekey
def get_uri(self):
return self._cap.to_string()
def is_readonly(self):
return False
class FakeNodeMaker:
def create_from_cap(self, writecap, readcap=None):
Overhaul IFilesystemNode handling, to simplify tests and use POLA internally. * stop using IURI as an adapter * pass cap strings around instead of URI instances * move filenode/dirnode creation duties from Client to new NodeMaker class * move other Client duties to KeyGenerator, SecretHolder, History classes * stop passing Client reference to dirnode/filenode constructors - pass less-powerful references instead, like StorageBroker or Uploader * always create DirectoryNodes by wrapping a filenode (mutable for now) * remove some specialized mock classes from unit tests Detailed list of changes (done one at a time, then merged together) always pass a string to create_node_from_uri(), not an IURI instance always pass a string to IFilesystemNode constructors, not an IURI instance stop using IURI() as an adapter, switch on cap prefix in create_node_from_uri() client.py: move SecretHolder code out to a separate class test_web.py: hush pyflakes client.py: move NodeMaker functionality out into a separate object LiteralFileNode: stop storing a Client reference immutable Checker: remove Client reference, it only needs a SecretHolder immutable Upload: remove Client reference, leave SecretHolder and StorageBroker immutable Repairer: replace Client reference with StorageBroker and SecretHolder immutable FileNode: remove Client reference mutable.Publish: stop passing Client mutable.ServermapUpdater: get StorageBroker in constructor, not by peeking into Client reference MutableChecker: reference StorageBroker and History directly, not through Client mutable.FileNode: removed unused indirection to checker classes mutable.FileNode: remove Client reference client.py: move RSA key generation into a separate class, so it can be passed to the nodemaker move create_mutable_file() into NodeMaker test_dirnode.py: stop using FakeClient mockups, use NoNetworkGrid instead. This simplifies the code, but takes longer to run (17s instead of 6s). This should come down later when other cleanups make it possible to use simpler (non-RSA) fake mutable files for dirnode tests. test_mutable.py: clean up basedir names client.py: move create_empty_dirnode() into NodeMaker dirnode.py: get rid of DirectoryNode.create remove DirectoryNode.init_from_uri, refactor NodeMaker for customization, simplify test_web's mock Client to match stop passing Client to DirectoryNode, make DirectoryNode.create_with_mutablefile the normal DirectoryNode constructor, start removing client from NodeMaker remove Client from NodeMaker move helper status into History, pass History to web.Status instead of Client test_mutable.py: fix minor typo
2009-08-15 11:02:56 +00:00
return None
nodemaker = FakeNodeMaker()
testdirnode = dirnode.DirectoryNode(ContainerNode(), nodemaker, uploader=None)
def random_unicode(l):
while True:
try:
return os.urandom(l).decode('utf-8')
except UnicodeDecodeError:
pass
encoding_parameters = {"k": 3, "n": 10}
def random_fsnode():
coin = random.randrange(0, 3)
if coin == 0:
cap = uri.CHKFileURI(randutil.insecurerandstr(16),
randutil.insecurerandstr(32),
random.randrange(1, 5),
random.randrange(6, 15),
random.randrange(99, 1000000000000))
return immut_filenode.FileNode(cap, None, None, None, None, None)
elif coin == 1:
cap = uri.WriteableSSKFileURI(randutil.insecurerandstr(16),
randutil.insecurerandstr(32))
n = mut_filenode.MutableFileNode(None, None, encoding_parameters, None)
return n.init_from_cap(cap)
else:
assert coin == 2
cap = uri.WriteableSSKFileURI(randutil.insecurerandstr(16),
randutil.insecurerandstr(32))
n = mut_filenode.MutableFileNode(None, None, encoding_parameters, None)
n.init_from_cap(cap)
return dirnode.DirectoryNode(n, nodemaker, uploader=None)
def random_metadata():
d = {}
d['ctime'] = random.random()
d['mtime'] = random.random()
d['tahoe'] = {}
d['tahoe']['linkcrtime'] = random.random()
d['tahoe']['linkmotime'] = random.random()
return d
def random_child():
return random_fsnode(), random_metadata()
def init_for_pack(N):
for i in xrange(len(children), N):
name = random_unicode(random.randrange(1, 9))
children.append( (name, random_child()) )
def init_for_unpack(N):
global packstr
init_for_pack(N)
packstr = pack(N)
def pack(N):
return testdirnode._pack_contents(dict(children[:N]))
def unpack(N):
return testdirnode._unpack_contents(packstr)
def unpack_and_repack(N):
return testdirnode._pack_contents(testdirnode._unpack_contents(packstr))
PROF_FILE_NAME="bench_dirnode.prof"
def run_benchmarks(profile=False):
for (initfunc, func) in [(init_for_unpack, unpack),
(init_for_pack, pack),
(init_for_unpack, unpack_and_repack)]:
print "benchmarking %s" % (func,)
benchutil.bench(unpack_and_repack, initfunc=init_for_unpack,
TOPXP=12)#, profile=profile, profresults=PROF_FILE_NAME)
def print_stats():
s = hotshot.stats.load(PROF_FILE_NAME)
s.strip_dirs().sort_stats("time").print_stats(32)
def prof_benchmarks():
# This requires pyutil >= v1.3.34.
run_benchmarks(profile=True)
if __name__ == "__main__":
if '--profile' in sys.argv:
if os.path.exists(PROF_FILE_NAME):
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,)
prof_benchmarks()
print_stats()
else:
run_benchmarks()