forked from Sage-Bionetworks/synapsePythonClient
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuploadTestFiles.py
More file actions
298 lines (254 loc) · 9.65 KB
/
Copy pathuploadTestFiles.py
File metadata and controls
298 lines (254 loc) · 9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""
Create some test files and upload them to Synapse and S3. This is used as the first step
for benchmarking downloads.
"""
import asyncio
import os
import shutil
import subprocess # nosec
import synapseclient
import synapseutils
from synapseclient.models import Folder, Project
PARENT_PROJECT = "syn$FILL_ME_IN"
S3_BUCKET = "s3://$FILL_ME_IN"
S3_PROFILE = "$FILL_ME_IN"
# Create a bunch of folders with known names to be used during the benchmarking
FOLDER_10_FILES_10GIB = "download_benchmarking_10_files_10gib"
FOLDER_1_FILES_10GIB = "download_benchmarking_1_files_10gib"
FOLDER_10_FILES_1GIB = "download_benchmarking_10_files_1gib"
FOLDER_100_FILES_100MIB = "download_benchmarking_100_files_100mib"
FOLDER_10_FILES_100MIB = "download_benchmarking_10_files_100mib"
FOLDER_100_FILES_10MIB = "download_benchmarking_100_files_10mib"
FOLDER_1000_FILES_1MIB = "download_benchmarking_1000_files_1mib"
MiB: int = 2**20
def create_folder_structure(
path: str,
depth_of_directory_tree: int,
num_sub_directories: int,
num_files_per_directory: int,
total_size_of_files_mib: int,
) -> None:
"""Create a tree directory structure starting with `root/subdir`.
Example:
Input:
depth_of_directory_tree = 1
num_sub_directories = 1
num_files_per_directory = 2
Result:
root/subdir1/file1.txt
root/subdir1/file2.txt
Input:
depth_of_directory_tree = 1
num_sub_directories = 2
num_files_per_directory = 2
Result:
root/subdir1/file1.txt
root/subdir1/file2.txt
root/subdir2/file1.txt
root/subdir2/file2.txt
Arguments:
path: The path to the root directory
depth_of_directory_tree: The depth of the directory tree
num_sub_directories: The number of subdirectories to create
num_files_per_directory: The number of files to create in each directory
total_size_of_files_mib: The total size of all files in MiB
Returns:
The total number of directories, total number of files, and the size of each
file in bytes
"""
# Calculate total number of files and size of each file
total_dirs = sum(
[num_sub_directories**i for i in range(1, depth_of_directory_tree + 1)]
)
total_files = total_dirs * num_files_per_directory
total_size_of_files_bytes = total_size_of_files_mib * MiB
size_of_each_file_bytes = total_size_of_files_bytes // total_files
print(f"total_directories: {total_dirs}")
print(f"total_files: {total_files}")
print(f"total_size_of_files_bytes: {total_size_of_files_bytes}")
print(f"size_of_each_file_bytes: {size_of_each_file_bytes}")
def create_files_in_current_dir(path_to_create_files: str) -> None:
for i in range(1, num_files_per_directory + 1):
chunk_size = MiB # size of each chunk in bytes
num_chunks = size_of_each_file_bytes // chunk_size
filename = os.path.join(path_to_create_files, f"file{i}.txt")
if (
os.path.isfile(filename)
and os.path.getsize(filename) == size_of_each_file_bytes
):
with open(filename, "r+b") as f:
f.seek(0)
f.write(os.urandom(chunk_size))
else:
if os.path.isfile(filename):
os.remove(filename)
with open(filename, "wb") as f:
for _ in range(num_chunks):
f.write(os.urandom(chunk_size))
def create_directories_in_current_dir(
path_to_create_dirs: str, current_depth: int
) -> None:
if current_depth < depth_of_directory_tree:
for i in range(1, num_sub_directories + 1):
path = f"{path_to_create_dirs}/subdir{i}"
os.makedirs(path, exist_ok=True)
create_files_in_current_dir(path)
new_depth = current_depth + 1
create_directories_in_current_dir(path, new_depth)
# Start creating directories and files
root_dir = os.path.join(path, "root")
os.makedirs(root_dir, exist_ok=True)
create_directories_in_current_dir(root_dir, 0)
return total_dirs, total_files, size_of_each_file_bytes
def sync_to_synapse(path: str, project_id: str, syn: synapseclient.Synapse) -> None:
"""Execute the test that uses synapseutils to sync all files/folders to synapse.
Arguments:
path: The path to the root directory
project_id: The project ID to sync to
syn: The logged in synapse instance
Returns:
None
"""
manifest_path = f"{path}/benchmarking_manifest.tsv"
with open(manifest_path, "w", encoding="utf-8") as f:
pass
synapseutils.generate_sync_manifest(
syn,
directory_path=path,
parent_id=project_id,
manifest_path=manifest_path,
)
synapseutils.syncToSynapse(
syn,
manifestFile=manifest_path,
sendMessages=False,
)
def execute_sync_to_s3(path: str, key_in_bucket: str) -> None:
subprocess.run(
f"aws s3 sync {path} {S3_BUCKET}/{key_in_bucket} --profile {S3_PROFILE}",
shell=True,
check=False,
) # nosec
async def set_up_folders_one_time(path: str, syn: synapseclient.Synapse) -> None:
project = await Project(id=PARENT_PROJECT).get_async()
depth = 1
sub_directories = 1
files_per_directory = 1000
size_mib = 1024
create_folder_structure(
path=path,
depth_of_directory_tree=depth,
num_sub_directories=sub_directories,
num_files_per_directory=files_per_directory,
total_size_of_files_mib=size_mib,
)
folder = await Folder(
name=FOLDER_1000_FILES_1MIB, parent_id=project.id
).store_async()
sync_to_synapse(path=path, syn=syn, project_id=folder.id)
os.remove(f"{path}/benchmarking_manifest.tsv")
execute_sync_to_s3(path=path, key_in_bucket=FOLDER_1000_FILES_1MIB)
shutil.rmtree(path)
# depth = 1
# sub_directories = 1
# files_per_directory = 100
# size_mib = 1024
# create_folder_structure(
# path=path,
# depth_of_directory_tree=depth,
# num_sub_directories=sub_directories,
# num_files_per_directory=files_per_directory,
# total_size_of_files_mib=size_mib,
# )
# folder = await Folder(
# name=FOLDER_100_FILES_10MIB, parent_id=project.id
# ).store_async()
# sync_to_synapse(path=path, syn=syn, project_id=folder.id)
# os.remove(f"{path}/benchmarking_manifest.tsv")
# shutil.rmtree(path)
# depth = 1
# sub_directories = 1
# files_per_directory = 10
# size_mib = 1024
# create_folder_structure(
# path=path,
# depth_of_directory_tree=depth,
# num_sub_directories=sub_directories,
# num_files_per_directory=files_per_directory,
# total_size_of_files_mib=size_mib,
# )
# folder = await Folder(
# name=FOLDER_10_FILES_100MIB, parent_id=project.id
# ).store_async()
# sync_to_synapse(path=path, syn=syn, project_id=folder.id)
# os.remove(f"{path}/benchmarking_manifest.tsv")
# shutil.rmtree(path)
# depth = 1
# sub_directories = 1
# files_per_directory = 100
# size_mib = 10240
# create_folder_structure(
# path=path,
# depth_of_directory_tree=depth,
# num_sub_directories=sub_directories,
# num_files_per_directory=files_per_directory,
# total_size_of_files_mib=size_mib,
# )
# folder = await Folder(
# name=FOLDER_100_FILES_100MIB, parent_id=project.id
# ).store_async()
# sync_to_synapse(path=path, syn=syn, project_id=folder.id)
# os.remove(f"{path}/benchmarking_manifest.tsv")
# shutil.rmtree(path)
# depth = 1
# sub_directories = 1
# files_per_directory = 10
# size_mib = 10240
# create_folder_structure(
# path=path,
# depth_of_directory_tree=depth,
# num_sub_directories=sub_directories,
# num_files_per_directory=files_per_directory,
# total_size_of_files_mib=size_mib,
# )
# folder = await Folder(name=FOLDER_10_FILES_1GIB, parent_id=project.id).store_async()
# sync_to_synapse(path=path, syn=syn, project_id=folder.id)
# os.remove(f"{path}/benchmarking_manifest.tsv")
# shutil.rmtree(path)
# depth = 1
# sub_directories = 1
# files_per_directory = 1
# size_mib = 10240
# create_folder_structure(
# path=path,
# depth_of_directory_tree=depth,
# num_sub_directories=sub_directories,
# num_files_per_directory=files_per_directory,
# total_size_of_files_mib=size_mib,
# )
# folder = await Folder(name=FOLDER_1_FILES_10GIB, parent_id=project.id).store_async()
# sync_to_synapse(path=path, syn=syn, project_id=folder.id)
# os.remove(f"{path}/benchmarking_manifest.tsv")
# shutil.rmtree(path)
# depth = 1
# sub_directories = 1
# files_per_directory = 10
# size_mib = 102400
# create_folder_structure(
# path=path,
# depth_of_directory_tree=depth,
# num_sub_directories=sub_directories,
# num_files_per_directory=files_per_directory,
# total_size_of_files_mib=size_mib,
# )
# folder = await Folder(
# name=FOLDER_10_FILES_10GIB, parent_id=project.id
# ).store_async()
# sync_to_synapse(path=path, syn=syn, project_id=folder.id)
# os.remove(f"{path}/benchmarking_manifest.tsv")
# shutil.rmtree(path)
synapse = synapseclient.Synapse(debug=False)
root_path = os.path.expanduser("~/benchmarkingDownload")
# Log-in with ~.synapseConfig `authToken`
synapse.login()
asyncio.run(set_up_folders_one_time(path=root_path, syn=synapse))