<s> import tempfile <EOL> from rally . common import broker <EOL> from rally . common import utils as rutils <EOL> from rally . plugins . openstack . scenarios . swift import utils as swift_utils <EOL> class SwiftObjectMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def _create_containers ( self , context , containers_per_tenant , threads ) : <EOL> """<STR_LIT>""" <EOL> containers = [ ] <EOL> def publish ( queue ) : <EOL> for user , tenant_id in ( rutils . iterate_per_tenants ( <EOL> context . get ( "<STR_LIT>" , [ ] ) ) ) : <EOL> context [ "<STR_LIT>" ] [ tenant_id ] [ "<STR_LIT>" ] = [ ] <EOL> for i in range ( containers_per_tenant ) : <EOL> args = ( user , context [ "<STR_LIT>" ] [ tenant_id ] [ "<STR_LIT>" ] ) <EOL> queue . append ( args ) <EOL> def consume ( cache , args ) : <EOL> user , tenant_containers = args <EOL> if user [ "<STR_LIT:id>" ] not in cache : <EOL> cache [ user [ "<STR_LIT:id>" ] ] = swift_utils . SwiftScenario ( <EOL> { "<STR_LIT:user>" : user , "<STR_LIT>" : context . get ( "<STR_LIT>" , { } ) } ) <EOL> container_name = cache [ user [ "<STR_LIT:id>" ] ] . _create_container ( ) <EOL> tenant_containers . append ( { "<STR_LIT:user>" : user , <EOL> "<STR_LIT>" : container_name , <EOL> "<STR_LIT>" : [ ] } ) <EOL> containers . append ( ( user [ "<STR_LIT>" ] , container_name ) ) <EOL> broker . run ( publish , consume , threads ) <EOL> return containers <EOL> def _create_objects ( self , context , objects_per_container , object_size , <EOL> threads ) : <EOL> """<STR_LIT>""" <EOL> objects = [ ] <EOL> with tempfile . TemporaryFile ( ) as dummy_file : <EOL> dummy_file . truncate ( object_size ) <EOL> def publish ( queue ) : <EOL> for tenant_id in context [ "<STR_LIT>" ] : <EOL> containers = context [ "<STR_LIT>" ] [ tenant_id ] [ "<STR_LIT>" ] <EOL> for container in containers : <EOL> for i in range ( objects_per_container ) : <EOL> queue . append ( container ) <EOL> def consume ( cache , container ) : <EOL> user = container [ "<STR_LIT:user>" ] <EOL> if user [ "<STR_LIT:id>" ] not in cache : <EOL> cache [ user [ "<STR_LIT:id>" ] ] = swift_utils . SwiftScenario ( <EOL> { "<STR_LIT:user>" : user , "<STR_LIT>" : context . get ( "<STR_LIT>" , { } ) } ) <EOL> dummy_file . seek ( <NUM_LIT:0> ) <EOL> object_name = cache [ user [ "<STR_LIT:id>" ] ] . _upload_object ( <EOL> container [ "<STR_LIT>" ] , <EOL> dummy_file ) [ <NUM_LIT:1> ] <EOL> container [ "<STR_LIT>" ] . append ( object_name ) <EOL> objects . append ( ( user [ "<STR_LIT>" ] , container [ "<STR_LIT>" ] , <EOL> object_name ) ) <EOL> broker . run ( publish , consume , threads ) <EOL> return objects <EOL> def _delete_containers ( self , context , threads ) : <EOL> """<STR_LIT>""" <EOL> def publish ( queue ) : <EOL> for tenant_id in context [ "<STR_LIT>" ] : <EOL> containers = context [ "<STR_LIT>" ] [ tenant_id ] [ "<STR_LIT>" ] <EOL> for container in containers [ : ] : <EOL> args = container , containers <EOL> queue . append ( args ) <EOL> def consume ( cache , args ) : <EOL> container , tenant_containers = args <EOL> user = container [ "<STR_LIT:user>" ] <EOL> if user [ "<STR_LIT:id>" ] not in cache : <EOL> cache [ user [ "<STR_LIT:id>" ] ] = swift_utils . SwiftScenario ( <EOL> { "<STR_LIT:user>" : user , "<STR_LIT>" : context . get ( "<STR_LIT>" , { } ) } ) <EOL> cache [ user [ "<STR_LIT:id>" ] ] . _delete_container ( container [ "<STR_LIT>" ] ) <EOL> tenant_containers . remove ( container ) <EOL> broker . run ( publish , consume , threads ) <EOL> def _delete_objects ( self , context , threads ) : <EOL> """<STR_LIT>""" <EOL> def publish ( queue ) : <EOL> for tenant_id in context [ "<STR_LIT>" ] : <EOL> containers = context [ "<STR_LIT>" ] [ tenant_id ] [ "<STR_LIT>" ] <EOL> for container in containers : <EOL> for object_name in container [ "<STR_LIT>" ] [ : ] : <EOL> args = object_name , container <EOL> queue . append ( args ) <EOL> def consume ( cache , args ) : <EOL> object_name , container = args <EOL> user = container [ "<STR_LIT:user>" ] <EOL> if user [ "<STR_LIT:id>" ] not in cache : <EOL> cache [ user [ "<STR_LIT:id>" ] ] = swift_utils . SwiftScenario ( <EOL> { "<STR_LIT:user>" : user , "<STR_LIT>" : context . get ( "<STR_LIT>" , { } ) } ) <EOL> cache [ user [ "<STR_LIT:id>" ] ] . _delete_object ( container [ "<STR_LIT>" ] , <EOL> object_name ) <EOL> container [ "<STR_LIT>" ] . remove ( object_name ) <EOL> broker . run ( publish , consume , threads ) </s>
