forked from FerasKSR/Unittest-Python-synapsePythonClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploadBenchmark.py
More file actions
537 lines (459 loc) · 18.1 KB
/
Copy pathuploadBenchmark.py
File metadata and controls
537 lines (459 loc) · 18.1 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
"""
Handle running a few tests for benchmark upload times to synapse and S3. This has the ability
to create a directory and file structure, sync to synapse using synapseutils, sync to synapse
using os.walk, and sync to S3 using the AWS CLI.
For the Synapse tests we are also adding annotations to the uploaded files.
# For running this benchmarking script:
- Inside `execute_test_suite` there are a few tests to possibly run. Uncomment for
the test you want to run
- The cleanup script doesn't need to delete local files, but it can be changed to do so
- If you want to run a different file/folder size manually remove the folders from disk
or change the `delete_local` parameter to `True` in the `cleanup` function
- This script can be re-run with the same sets of files since it will clear the
syn.cache and synapse project before running the tests. It also updates the file MD5s.
- This script is not reccommended to run on a personal computer, it is best to run on a
service catalog EC2 instance. Mainly because this will purge your local Synapse cache.
"""
import asyncio
import datetime
import os
import shutil
import subprocess # nosec
from time import perf_counter
from opentelemetry import trace
import synapseclient
import synapseutils
from synapseclient.annotations import Annotations
from synapseclient.entity import File as SynFile
from synapseclient.entity import Folder as SynFolder
from synapseclient.models import File, Folder, Project
# from opentelemetry.sdk.trace import TracerProvider
# from opentelemetry.sdk.trace.export import BatchSpanProcessor
# from opentelemetry.sdk.resources import SERVICE_NAME, Resource
# from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
# trace.set_tracer_provider(
# TracerProvider(resource=Resource(attributes={SERVICE_NAME: "upload_benchmarking"}))
# )
# trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
tracer = trace.get_tracer("my_tracer")
PARENT_PROJECT = "syn$FILL_ME_IN"
S3_BUCKET = "s3://$FILL_ME_IN"
S3_PROFILE = "$FILL_ME_IN"
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, current_depth):
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 cleanup(
path: str,
delete_synapse: bool = True,
delete_s3: bool = False,
delete_local: bool = True,
) -> None:
"""Cleanup data in synapse, local, and s3.
Arguments:
path: The path to the root directory
delete_synapse: Whether to delete data in synapse
delete_s3: Whether to delete data in S3
delete_local: Whether to delete data locally
"""
if delete_s3:
subprocess.run(
["aws", "s3", "rm", S3_BUCKET, "--recursive", "--profile", S3_PROFILE]
) # nosec
if delete_synapse:
for child in syn.getChildren(PARENT_PROJECT, includeTypes=["folder"]):
syn.delete(child["id"])
syn.cache.purge(after_date=datetime.datetime(2021, 1, 1))
if delete_local and os.path.exists(path):
shutil.rmtree(path)
def execute_synapseutils_test(
path: str,
test_name: str,
) -> None:
"""Execute the test that uses synapseutils to sync all files/folders to synapse.
Arguments:
path: The path to the root directory
test_name: The name of the test to add to the span name
"""
with tracer.start_as_current_span(f"synapseutils__{test_name}"):
manifest_path = f"{path}/benchmarking_manifest.tsv"
with open(manifest_path, "w", encoding="utf-8") as f:
pass
time_before_syncToSynapse = perf_counter()
synapseutils.generate_sync_manifest(
syn,
directory_path=path,
parent_id=PARENT_PROJECT,
manifest_path=manifest_path,
)
# Write annotations to the manifest file -----------------------------------------
# Open the `manifest_path` tab-delimited file and read its contents
with open(manifest_path, "r") as file:
lines = file.readlines()
# Append 3 columns "annot1", "annot2", "annot3" to the header
lines[0] = lines[0].strip() + "\tannot1\tannot2\tannot3\tannot4\tannot5\n"
# Append the values to each line
for i in range(1, len(lines)):
lines[i] = lines[i].strip() + "\tvalue1\t1\t1.2\ttrue\t2020-01-01\n"
# Write the modified contents back to the file
with open(manifest_path, "w") as file:
file.writelines(lines)
# Finish writing annotations to the manifest file --------------------------------
time_before_syncToSynapse = perf_counter()
synapseutils.syncToSynapse(
syn,
manifestFile=manifest_path,
sendMessages=False,
)
print(
f"\nTime to sync to Synapse: {perf_counter() - time_before_syncToSynapse}"
)
def execute_walk_test(
path: str,
test_name: str,
) -> None:
"""Execute the test that uses os.walk to sync all files/folders to synapse.
Arguments:
path: The path to the root directory
test_name: The name of the test to add to the span name
"""
with tracer.start_as_current_span(f"manual_walk__{test_name}"):
time_before_walking_tree = perf_counter()
parents = {path: PARENT_PROJECT}
saved_files = []
saved_folders = []
for directory_path, directory_names, file_names in os.walk(path):
# Replicate the folders on Synapse
for directory_name in directory_names:
folder_path = os.path.join(directory_path, directory_name)
parent_id = parents[directory_path]
folder = SynFolder(
name=directory_name,
parent=parent_id,
)
# Store Synapse ID for sub-folders/files
folder = syn.store(folder)
saved_folders.append(folder)
parents[folder_path] = folder["id"]
# Replicate the files on Synapse
for filename in file_names:
filepath = os.path.join(directory_path, filename)
file = SynFile(
path=filepath,
parent=parents[directory_path],
)
saved_file = syn.store(file)
saved_files.append(saved_file)
# Store annotations on the file ------------------------------------------
syn.set_annotations(
annotations=Annotations(
id=saved_file.id,
etag=saved_file.etag,
**{
"annot1": "value1",
"annot2": 1,
"annot3": 1.2,
"annot4": True,
"annot5": "2020-01-01",
},
)
)
# Finish storing annotations on the file ---------------------------------
print(
f"\nTime to walk and sync tree: {perf_counter() - time_before_walking_tree}"
)
def execute_walk_test_oop(
path: str,
test_name: str,
) -> None:
"""Execute the test that uses os.walk to sync all files/folders to synapse.
Arguments:
path: The path to the root directory
test_name: The name of the test to add to the span name
"""
with tracer.start_as_current_span(f"manual_walk__{test_name}"):
time_before_walking_tree = perf_counter()
root_project = Project(id=PARENT_PROJECT)
parents = {path: root_project}
for directory_path, directory_names, file_names in os.walk(path):
# Replicate the folders on Synapse
for directory_name in directory_names:
folder_path = os.path.join(directory_path, directory_name)
parent_container = parents[directory_path]
new_folder = Folder(name=directory_name)
parent_container.folders.append(new_folder)
parents[folder_path] = new_folder
# Replicate the files on Synapse
for filename in file_names:
filepath = os.path.join(directory_path, filename)
parent_container = parents[directory_path]
new_file = File(
path=filepath,
annotations={
"annot1": "value1",
"annot2": 1,
"annot3": 1.2,
"annot4": True,
"annot5": "2020-01-01",
},
description="This is a Test File",
)
parent_container.files.append(new_file)
asyncio.run(root_project.store_async())
print(
f"\nTime to walk and sync tree - OOP: {perf_counter() - time_before_walking_tree}"
)
def execute_sync_to_s3(
path: str,
test_name: str,
) -> None:
"""Executes the AWS CLI sync command. Expected to run last as this will delete local files.
:param path: The path to the root directory
:param test_name: The name of the test to add to the span name
"""
with tracer.start_as_current_span(f"s3_sync__{test_name}"):
time_before_sync = perf_counter()
subprocess.run(
f"aws s3 sync {path} {S3_BUCKET} --profile {S3_PROFILE}",
shell=True,
check=False,
) # nosec
print(f"\nTime to S3 sync: {perf_counter() - time_before_sync}")
def execute_test_suite(
path: str,
depth_of_directory_tree: int,
num_sub_directories: int,
num_files_per_directory: int,
total_size_of_files_mib: int,
) -> None:
"""Execute the test suite.
:param path: _description_
:param depth_of_directory_tree: _description_
:param num_sub_directories: _description_
:param num_files_per_directory: _description_
:param total_size_of_files_mib: _description_
"""
# Cleanup can be changed to delete_local=True when we want to clear the files out
# This can be kept as False to allow multiple tests with the same file/folder
# structure to re-use the files on Disk.
cleanup(path=path, delete_synapse=True, delete_s3=True, delete_local=False)
_, total_files, _ = create_folder_structure(
path=path,
depth_of_directory_tree=depth_of_directory_tree,
num_sub_directories=num_sub_directories,
num_files_per_directory=num_files_per_directory,
total_size_of_files_mib=total_size_of_files_mib,
)
test_name = f"{total_files}_files_{total_size_of_files_mib}MiB"
# execute_synapseutils_test(path, test_name)
# execute_walk_test(path, test_name)
# execute_walk_test_oop(path, test_name)
# execute_sync_to_s3(path, test_name)
syn = synapseclient.Synapse(debug=False)
root_path = os.path.expanduser("~/benchmarking")
# Log-in with ~.synapseConfig `authToken`
syn.login()
print("25 Files - 25MiB")
# 25 Files - 25MiB -----------------------------------------------------------------------
depth = 1
sub_directories = 1
files_per_directory = 25
size_mib = 25
execute_test_suite(
path=root_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,
)
# print("1 Files - 10MiB")
# ## 1 Files - 10MiB -----------------------------------------------------------------------
# depth = 1
# sub_directories = 1
# files_per_directory = 1
# size_mib = 10
# execute_test_suite(
# path=root_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,
# )
# print("1000 Files - 1GiB")
# ## 1000 Files - 1GiB -----------------------------------------------------------------------
# depth = 1
# sub_directories = 1
# files_per_directory = 1000
# size_mib = 1024
# execute_test_suite(
# path=root_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,
# )
# print("100 Files - 1GiB")
# ## 100 Files - 1GiB -----------------------------------------------------------------------
# depth = 1
# sub_directories = 1
# files_per_directory = 100
# size_mib = 1024
# execute_test_suite(
# path=root_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,
# )
# print("10 Files - 1GiB")
# ## 10 Files - 1GiB -----------------------------------------------------------------------
# depth = 1
# sub_directories = 1
# files_per_directory = 10
# size_mib = 1024
# execute_test_suite(
# path=root_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,
# )
# print("100 Files - 10GiB")
# ## 100 Files - 10GiB -----------------------------------------------------------------------
# depth = 1
# sub_directories = 1
# files_per_directory = 100
# size_mib = 10240
# execute_test_suite(
# path=root_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,
# )
# print("10 Files - 10GiB")
# ## 10 Files - 10GiB -----------------------------------------------------------------------
# depth = 1
# sub_directories = 1
# files_per_directory = 10
# size_mib = 10240
# execute_test_suite(
# path=root_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,
# )
# print("1 Files - 10GiB")
# ## 1 Files - 10GiB -----------------------------------------------------------------------
# depth = 1
# sub_directories = 1
# files_per_directory = 1
# size_mib = 10240
# execute_test_suite(
# path=root_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,
# )
# print("10 Files - 100GiB")
# ## 10 Files - 100GiB -----------------------------------------------------------------------
# depth = 1
# sub_directories = 1
# files_per_directory = 10
# size_mib = 102400
# execute_test_suite(
# path=root_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,
# )
# print("1 Files - 100GiB")
# ## 1 Files - 100GiB -----------------------------------------------------------------------
# depth = 1
# sub_directories = 1
# files_per_directory = 1
# size_mib = 102400
# execute_test_suite(
# path=root_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,
# )