2009-07-07 03:30:35 +00:00
import hotshot . stats , os , random , sys
2009-07-04 22:43:00 +00:00
2010-05-21 12:20:52 +00:00
from pyutil import benchutil , randutil # http://tahoe-lafs.org/trac/pyutil
2009-07-04 22:43:00 +00:00
2017-02-27 17:56:49 +00:00
from zope . interface import implementer
2009-11-11 22:25:42 +00:00
from allmydata import dirnode , uri
2009-11-20 07:22:39 +00:00
from allmydata . interfaces import IFileNode
from allmydata . mutable . filenode import MutableFileNode
from allmydata . immutable . filenode import ImmutableFileNode
2009-07-04 22:43:00 +00:00
2017-02-27 17:56:49 +00:00
@implementer ( IFileNode )
class ContainerNode ( object ) :
2009-11-11 22:25:42 +00:00
# dirnodes sit on top of a "container" filenode, from which it extracts a
# writekey
2009-07-04 22:43:00 +00:00
def __init__ ( self ) :
2009-11-11 22:25:42 +00:00
self . _writekey = randutil . insecurerandstr ( 16 )
self . _fingerprint = randutil . insecurerandstr ( 32 )
self . _cap = uri . WriteableSSKFileURI ( self . _writekey , self . _fingerprint )
2009-07-04 22:43:00 +00:00
def get_writekey ( self ) :
2009-11-11 22:25:42 +00:00
return self . _writekey
2009-11-20 07:22:39 +00:00
def get_cap ( self ) :
return self . _cap
2009-11-11 22:25:42 +00:00
def get_uri ( self ) :
return self . _cap . to_string ( )
def is_readonly ( self ) :
return False
2010-07-18 03:17:10 +00:00
def is_mutable ( self ) :
return True
class FakeNode :
def raise_error ( self ) :
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
2009-07-04 22:43:00 +00:00
2010-07-18 03:17:10 +00:00
class FakeNodeMaker :
def create_from_cap ( self , writecap , readcap = None , deep_immutable = False , name = ' ' ) :
return FakeNode ( )
2010-07-19 04:49:48 +00:00
def random_unicode ( n = 140 , b = 3 , codec = ' utf-8 ' ) :
l = [ ]
while len ( l ) < n :
2009-07-04 22:43:00 +00:00
try :
2010-07-19 04:49:48 +00:00
u = os . urandom ( b ) . decode ( codec ) [ 0 ]
2009-07-04 22:43:00 +00:00
except UnicodeDecodeError :
pass
2010-07-19 04:49:48 +00:00
else :
l . append ( u )
return u ' ' . join ( l )
2009-07-04 22:43:00 +00:00
2009-11-11 22:25:42 +00:00
encoding_parameters = { " k " : 3 , " n " : 10 }
2009-07-04 22:43:00 +00:00
def random_metadata ( ) :
d = { }
d [ ' tahoe ' ] = { }
d [ ' tahoe ' ] [ ' linkcrtime ' ] = random . random ( )
d [ ' tahoe ' ] [ ' linkmotime ' ] = random . random ( )
return d
2010-05-21 20:25:11 +00:00
PROF_FILE_NAME = " bench_dirnode.prof "
2009-07-04 22:43:00 +00:00
2010-05-21 12:20:52 +00:00
class B ( object ) :
def __init__ ( self ) :
self . children = [ ] # tuples of (k, v) (suitable for passing to dict())
self . packstr = None
self . nodemaker = FakeNodeMaker ( )
self . testdirnode = dirnode . DirectoryNode ( ContainerNode ( ) , self . nodemaker , uploader = None )
2010-05-21 20:25:11 +00:00
def random_fsnode ( self ) :
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 ImmutableFileNode ( cap , None , None , None , None , None )
elif coin == 1 :
cap = uri . WriteableSSKFileURI ( randutil . insecurerandstr ( 16 ) ,
randutil . insecurerandstr ( 32 ) )
n = 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 = MutableFileNode ( None , None , encoding_parameters , None )
n . init_from_cap ( cap )
return dirnode . DirectoryNode ( n , self . nodemaker , uploader = None )
def random_child ( self ) :
return self . random_fsnode ( ) , random_metadata ( )
2010-05-21 12:20:52 +00:00
def init_for_pack ( self , N ) :
for i in xrange ( len ( self . children ) , N ) :
2010-07-19 04:49:48 +00:00
name = random_unicode ( random . randrange ( 0 , 10 ) )
2010-05-21 20:25:11 +00:00
self . children . append ( ( name , self . random_child ( ) ) )
2009-07-04 22:43:00 +00:00
2010-05-21 12:20:52 +00:00
def init_for_unpack ( self , N ) :
self . init_for_pack ( N )
2010-05-21 20:25:11 +00:00
self . packstr = self . pack ( N )
2009-07-04 22:43:00 +00:00
2010-05-21 12:20:52 +00:00
def pack ( self , N ) :
return self . testdirnode . _pack_contents ( dict ( self . children [ : N ] ) )
2009-07-04 22:43:00 +00:00
2010-05-21 12:20:52 +00:00
def unpack ( self , N ) :
return self . testdirnode . _unpack_contents ( self . packstr )
2009-07-04 22:43:00 +00:00
2010-05-21 12:20:52 +00:00
def unpack_and_repack ( self , N ) :
2010-05-21 20:25:11 +00:00
return self . testdirnode . _pack_contents ( self . testdirnode . _unpack_contents ( self . packstr ) )
2009-07-07 03:30:35 +00:00
2010-05-21 20:25:11 +00:00
def run_benchmarks ( self , profile = False ) :
for ( initfunc , func ) in [ ( self . init_for_unpack , self . unpack ) ,
( self . init_for_pack , self . pack ) ,
( self . init_for_unpack , self . unpack_and_repack ) ] :
print " benchmarking %s " % ( func , )
2010-07-19 04:49:48 +00:00
for N in 16 , 512 , 2048 , 16384 :
print " %5d " % N ,
benchutil . rep_bench ( func , N , initfunc = initfunc , MAXREPS = 20 , UNITS_PER_SECOND = 1000 )
benchutil . print_bench_footer ( UNITS_PER_SECOND = 1000 )
print " (milliseconds) "
2009-07-05 16:29:53 +00:00
2010-05-21 20:25:11 +00:00
def prof_benchmarks ( self ) :
# This requires pyutil >= v1.3.34.
self . run_benchmarks ( profile = True )
2009-07-04 22:43:00 +00:00
2010-05-21 20:25:11 +00:00
def print_stats ( self ) :
s = hotshot . stats . load ( PROF_FILE_NAME )
s . strip_dirs ( ) . sort_stats ( " time " ) . print_stats ( 32 )
2009-07-04 22:43:00 +00:00
if __name__ == " __main__ " :
2009-07-07 03:30:35 +00:00
if ' --profile ' in sys . argv :
if os . path . exists ( PROF_FILE_NAME ) :
2009-07-20 00:21:14 +00:00
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 , )
2010-05-21 20:25:11 +00:00
b = B ( )
b . prof_benchmarks ( )
b . print_stats ( )
2009-07-07 03:30:35 +00:00
else :
2010-05-21 20:25:11 +00:00
b = B ( )
b . run_benchmarks ( )