12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import os , sys , time
15
+ import os , sys , time , pickle , boto3 , botocore
16
16
from os .path import join , getsize , exists
17
- import boto3 , botocore
18
17
from botocore .client import ClientError
19
- from exercizer import Exercizer
20
18
from traceback import format_exc
21
19
from pprint import pprint
22
- import pickle
20
+ from exercizer import Exercizer
21
+
23
22
class AWSExercizer (Exercizer ):
24
- def __init__ (self , env_credentials = {}, region_name = 'us-east-1' ):
25
- Exercizer .__init__ (self )
23
+ def __init__ (
24
+ self ,
25
+ env_credentials = {},
26
+ region_name = 'us-east-1' ,
27
+ container_name = 'blobtester' ,
28
+ fileSizeskb = [],
29
+ localDir = '/tmp/localDir' ,
30
+ numIters = 1 ):
31
+ Exercizer .__init__ (
32
+ self ,
33
+ fileSizeskb ,
34
+ localDir ,
35
+ numIters )
26
36
if region_name == 'us-east-1' : # aws quirk - you can't specify us-east-1 explicitly
27
37
self .storage_client = boto3 .client ('s3' ,
28
38
aws_access_key_id = os .environ .get (env_credentials ['account' ]),
@@ -33,66 +43,90 @@ def __init__(self, env_credentials= {}, region_name ='us-east-1'):
33
43
aws_secret_access_key = os .environ .get (env_credentials ['secret' ]),
34
44
region_name = region_name )
35
45
self .region_name = region_name
46
+ self .container_name = container_name
36
47
37
48
38
- def UploadObjectsToContainer (self , container_name = 'blobtester' , localDir = '/tmp/smalldir' ):
49
+ def UploadObjectsToContainer (self ):
39
50
# create bucket if it does not exist
40
51
try :
41
- self .storage_client .head_bucket (Bucket = container_name )
52
+ self .storage_client .head_bucket (Bucket = self . container_name )
42
53
except ClientError :
43
54
if self .region_name != 'us-east-1' :
44
55
container = self .storage_client .create_bucket (
45
- Bucket = container_name ,
56
+ Bucket = self . container_name ,
46
57
CreateBucketConfiguration = {
47
58
'LocationConstraint' : self .region_name
48
59
})
49
60
else :
50
- container = self .storage_client .create_bucket (Bucket = container_name )
61
+ container = self .storage_client .create_bucket (
62
+ Bucket = self .container_name )
51
63
52
- dic_uploadData = {}
53
- for root , dirs , files in os .walk (localDir ):
54
- for name in files :
55
- filePath = join (root ,name )
56
- self .startTimer ()
57
- try :
58
- self .storage_client .upload_file (filePath , container_name , filePath )
59
- dic_uploadData [filePath ] = (self .endTimer (), getsize (filePath ))
60
- except :
61
- print ('Failure uploading {}' .format (filePath ))
62
- print (format_exc ())
63
- self .endTimer ()
64
- dic_uploadData [filePath ] = - 1
65
- return dic_uploadData
64
+ list_uploadData = []
65
+ for eachFile in self .manifest :
66
+ filePath , intfilesize = eachFile
67
+ print "U" ,
68
+ print eachFile
69
+ self .makeOneRandomBinFile (filePath , intfilesize )
70
+ self .startTimer ()
71
+ try :
72
+ self .storage_client .upload_file (
73
+ filePath , self .container_name , filePath )
74
+ list_uploadData .append (
75
+ (self .endTimer (), getsize (filePath ), 'aws_upload' ))
76
+ except :
77
+ print ('Failure uploading {}' .format (filePath ))
78
+ print (format_exc ())
79
+ self .endTimer ()
80
+ os .remove (filePath )
81
+ return list_uploadData
66
82
67
- def ListObjectsInContainer (self , container_name = 'blobtester' ):
83
+ def ListObjectsInContainer (self ):
68
84
'''
69
85
Return generator with the list of blobs
70
86
'''
71
- return self .storage_client .list_objects_v2 (Bucket = container_name )['Contents' ]
87
+ objList = self .storage_client .list_objects_v2 (
88
+ Bucket = self .container_name )
89
+ if 'Contents' in objList :
90
+ return objList ['Contents' ]
91
+ else :
92
+ return []
72
93
73
- def DownloadObjectsFromContainer (self , container_name = 'blobtester' , localDir = '/tmp/smalldir' ):
74
- if not exists (localDir ):
75
- os .makedirs (localDir )
76
- dic_downloadData = {}
77
- self .startTimer ()
78
- blobList = self .ListObjectsInContainer (container_name )
79
- dic_downloadData ['_listObjectsInContainer' ] = (self .endTimer (), "Container objects listing time" )
94
+ def DownloadObjectsFromContainer (self ):
95
+ if not exists (self .localDir ):
96
+ os .makedirs (self .localDir )
97
+ list_downloadData = []
98
+ blobList = self .ListObjectsInContainer ()
80
99
for aBlob in blobList :
81
100
self .startTimer ()
82
- localPath = join (localDir ,aBlob ['Key' ].split ('/' )[- 1 ])
83
- self .storage_client .download_file (container_name , aBlob ['Key' ], localPath )
84
- dic_downloadData [localPath ] = (self .endTimer (), getsize (localPath ))
85
- return dic_downloadData
101
+ localPath = join (self .localDir ,aBlob ['Key' ].split ('/' )[- 1 ])
102
+ self .storage_client .download_file (
103
+ self .container_name ,
104
+ aBlob ['Key' ], localPath )
105
+ blobsize = getsize (localPath )
106
+ list_downloadData .append (
107
+ (self .endTimer (), blobsize , 'aws_download' ))
108
+ print "D" ,
109
+ print (localPath , blobsize )
110
+ os .remove (localPath )
111
+ self .startTimer ()
112
+ self .storage_client .delete_object (
113
+ Bucket = self .container_name ,
114
+ Key = aBlob ['Key' ])
115
+ list_downloadData .append ((self .endTimer (), blobsize , 'aws_delete' ))
116
+ return list_downloadData
86
117
87
118
def DeleteContainer (self , container_name = 'blobtester' ):
88
119
self .startTimer ()
89
- blobList = self .ListObjectsInContainer (container_name )
120
+ blobList = self .ListObjectsInContainer ()
90
121
deleteList = []
91
122
for aBlob in blobList :
92
123
deleteList .append ({'Key' : aBlob ['Key' ]})
93
- self .storage_client .delete_objects (Bucket = container_name , Delete = { 'Objects' : deleteList })
94
- self .storage_client .delete_bucket (Bucket = container_name )
95
- return {container_name : self .endTimer (), 'operation' :'Deleted' }
124
+ if len (deleteList ) > 0 :
125
+ self .storage_client .delete_objects (
126
+ Bucket = self .container_name ,
127
+ Delete = { 'Objects' : deleteList })
128
+ self .storage_client .delete_bucket (Bucket = self .container_name )
129
+ return {self .container_name : self .endTimer (), 'operation' :'Deleted' }
96
130
97
131
98
132
if __name__ == "__main__" :
@@ -101,17 +135,18 @@ def DeleteContainer(self, container_name='blobtester'):
101
135
'account' : 'S3KEY' ,
102
136
'secret' :'S3SECRET'
103
137
}
104
- awsex = AWSExercizer (env_credentials = env_credentials )
105
- # Upload
106
- # print "Upload objects"
107
- # pprint(awsex.UploadObjectsToContainer())
108
- # Download
109
- # print "Download objects"
110
- # pprint(awsex.DownloadObjectsFromContainer())
111
- # Delete
138
+ awsex = AWSExercizer (
139
+ env_credentials = env_credentials ,
140
+ localDir = sys .argv [1 ],
141
+ numIters = sys .argv [2 ],
142
+ fileSizeskb = sys .argv [3 :])
112
143
113
- pickle .dump (awsex .UploadObjectsToContainer (localDir = sys .argv [1 ]), open ('/tmp/outputdata/objbench/aws_upload.pkl' ,'wb' ))
144
+ pickle .dump (
145
+ awsex .UploadObjectsToContainer (),
146
+ open ('/tmp/outputdata/objbench/aws_upload.pkl' ,'wb' ))
114
147
# Download
115
- pickle .dump (awsex .DownloadObjectsFromContainer (localDir = sys .argv [1 ]), open ('/tmp/outputdata/objbench/aws_download.pkl' ,'wb' ))
116
- print "Delete buckets"
148
+ pickle .dump (
149
+ awsex .DownloadObjectsFromContainer (),
150
+ open ('/tmp/outputdata/objbench/aws_download.pkl' ,'wb' ))
151
+ print "Delete bucket"
117
152
pprint (awsex .DeleteContainer ())
0 commit comments