forked from irods/irods_testing_environment
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython_client_benchmark.py
More file actions
executable file
·1172 lines (971 loc) · 47.8 KB
/
python_client_benchmark.py
File metadata and controls
executable file
·1172 lines (971 loc) · 47.8 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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
iRODS Adaptive Compression Benchmark
Tests network speed first, then adjusts compression level automatically
ADAPTIVE FEATURES:
- Pre-test network speed measurement
- Automatic compression level selection based on network speed
- Compression speed vs network speed tradeoff analysis
- Optimal settings for your specific connection
"""
import os
import sys
import time
import hashlib
import tempfile
import subprocess
import shutil
from datetime import datetime
from pathlib import Path
from irods.session import iRODSSession
from irods.exception import iRODSException
from irods.models import DataObjectMeta
try:
import zstandard as zstd
ZSTD_AVAILABLE = True
except ImportError:
ZSTD_AVAILABLE = False
print("Warning: zstandard not installed. Install with: pip install zstandard")
from resource_monitor import ResourceMonitor, format_stats, save_stats_to_file
# ==================== CONFIGURATION ====================
TEST_RUNS = 5
TEST_FILES_DIR = "/home/demetrius/Desktop/testFiles"
ENABLE_ADAPTIVE_COMPRESSION = True # Auto-adjust compression based on network speed
ENABLE_CLEANUP = True
ENABLE_FILE_VERIFICATION = True
ENABLE_METADATA = True
RESULTS_DIR = "./performance_results"
# Bandwidth limiting configuration (using wondershaper)
# Set to False for no limit (default speed), or specify a speed limit:
# Options: False, "100mbps", "50mbps", "10mbps", "1mbps"
# Note: Requires wondershaper (sudo apt install wondershaper)
# Format: {"download_kbps": 1000, "upload_kbps": 500, "interface": "eth0"}
# Or use string shortcuts: "1mbps", "10mbps", "100mbps"
BANDWIDTH_LIMIT = False # No bandwidth limiting by default
NETWORK_INTERFACE = "eth0" # Change to your network interface (use 'ip link show')
# Network speed test configuration
NETWORK_TEST_SIZE_MB = 5 # Size of test file for speed measurement
NETWORK_TEST_SAMPLES = 3 # Number of samples to average
# =======================================================
# Compression level recommendations based on network speed
# Logic: Faster network = lower compression (CPU less important)
# Slower network = higher compression (worth the CPU cost)
COMPRESSION_STRATEGY = {
'very_fast': {'min_mbps': 100, 'level': 1, 'description': 'Very fast network (>100 MB/s): minimal compression'},
'fast': {'min_mbps': 50, 'level': 3, 'description': 'Fast network (50-100 MB/s): light compression'},
'medium': {'min_mbps': 10, 'level': 6, 'description': 'Medium network (10-50 MB/s): balanced compression'},
'slow': {'min_mbps': 1, 'level': 9, 'description': 'Slow network (1-10 MB/s): high compression'},
'very_slow': {'min_mbps': 0, 'level': 15, 'description': 'Very slow network (<1 MB/s): maximum compression'},
}
# Metadata schema constants
METADATA_COMPRESSION_ALGORITHM = "compression_algorithm"
METADATA_ORIGINAL_SIZE = "original_size_bytes"
METADATA_COMPRESSION_RATIO = "compression_ratio_percent"
METADATA_COMPRESSION_LEVEL = "compression_level"
class Colors:
RED = '\033[0;31m'
GREEN = '\033[0;32m'
YELLOW = '\033[1;33m'
BLUE = '\033[0;34m'
CYAN = '\033[0;36m'
MAGENTA = '\033[0;35m'
NC = '\033[0m'
def print_color(message, color):
"""Print colored message"""
print(f"{color}{message}{Colors.NC}")
def format_size(size_bytes):
"""Format bytes to human readable format"""
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
if size_bytes < 1024.0:
return f"{size_bytes:.2f} {unit}"
size_bytes /= 1024.0
return f"{size_bytes:.2f} PB"
def create_test_file(size_mb):
"""Create a temporary test file of specified size with random data"""
test_file = tempfile.mktemp(suffix='.testdata')
size_bytes = size_mb * 1024 * 1024
with open(test_file, 'wb') as f:
# Write random data in chunks
chunk_size = 1024 * 1024 # 1 MB chunks
written = 0
while written < size_bytes:
to_write = min(chunk_size, size_bytes - written)
f.write(os.urandom(to_write))
written += to_write
return test_file
def test_network_speed(session, test_size_mb=NETWORK_TEST_SIZE_MB, samples=NETWORK_TEST_SAMPLES):
"""
Test network speed by uploading and downloading a test file
Returns: (upload_mbps, download_mbps, avg_latency_ms)
"""
print_color(f"\n{'='*60}", Colors.CYAN)
print_color("NETWORK SPEED TEST", Colors.CYAN)
print_color(f"{'='*60}", Colors.CYAN)
print(f"Test file size: {test_size_mb} MB")
print(f"Test samples: {samples}")
print()
zone = session.zone
username = session.username
upload_speeds = []
download_speeds = []
latencies = []
for i in range(samples):
print(f"Sample {i+1}/{samples}: ", end='', flush=True)
try:
# Create test file
test_file = create_test_file(test_size_mb)
file_size = os.path.getsize(test_file)
irods_path = f"/{zone}/home/{username}/.speedtest_{int(time.time()*1000)}.tmp"
# Measure upload speed
upload_start = time.time()
session.data_objects.put(test_file, irods_path, force=True)
upload_time = time.time() - upload_start
upload_mbps = (file_size / (1024 * 1024)) / upload_time
# Measure download speed
download_file = tempfile.mktemp(suffix='.tmp')
download_start = time.time()
session.data_objects.get(irods_path, download_file, force=True)
download_time = time.time() - download_start
download_mbps = (file_size / (1024 * 1024)) / download_time
# Calculate round-trip latency
latency_ms = (upload_time + download_time) * 1000 / 2
upload_speeds.append(upload_mbps)
download_speeds.append(download_mbps)
latencies.append(latency_ms)
print(f"Up: {upload_mbps:.2f} MB/s, Down: {download_mbps:.2f} MB/s, Latency: {latency_ms:.0f}ms")
# Cleanup
os.unlink(test_file)
os.unlink(download_file)
session.data_objects.unlink(irods_path, force=True)
except Exception as e:
print_color(f"Failed: {e}", Colors.RED)
continue
if not upload_speeds:
print_color("\nNetwork speed test failed!", Colors.RED)
return None, None, None
avg_upload = sum(upload_speeds) / len(upload_speeds)
avg_download = sum(download_speeds) / len(download_speeds)
avg_latency = sum(latencies) / len(latencies)
print()
print_color("Network Speed Test Results:", Colors.GREEN)
print(f" Average upload speed: {avg_upload:.2f} MB/s")
print(f" Average download speed: {avg_download:.2f} MB/s")
print(f" Average latency: {avg_latency:.0f} ms")
return avg_upload, avg_download, avg_latency
def select_compression_level(network_speed_mbps):
"""
Select optimal compression level based on network speed
Logic:
- Fast network: Lower compression (less CPU, network isn't bottleneck)
- Slow network: Higher compression (more CPU, but saves transfer time)
"""
selected_strategy = None
selected_level = 3 # Default
# Find matching strategy
for name, config in sorted(COMPRESSION_STRATEGY.items(),
key=lambda x: x[1]['min_mbps'],
reverse=True):
if network_speed_mbps >= config['min_mbps']:
selected_strategy = name
selected_level = config['level']
description = config['description']
break
print()
print_color(f"{'='*60}", Colors.CYAN)
print_color("ADAPTIVE COMPRESSION SELECTION", Colors.CYAN)
print_color(f"{'='*60}", Colors.CYAN)
print(f"Network speed: {network_speed_mbps:.2f} MB/s")
print(f"Strategy: {selected_strategy}")
print(f"Selected compression level: {selected_level}")
print(f"Rationale: {description}")
print()
# Show compression speed estimates
print("Compression level characteristics (zstd):")
print(" Level 1: ~500 MB/s compression, ~2000 MB/s decompression")
print(" Level 3: ~200 MB/s compression, ~2000 MB/s decompression")
print(" Level 6: ~100 MB/s compression, ~1500 MB/s decompression")
print(" Level 9: ~40 MB/s compression, ~1200 MB/s decompression")
print(" Level 15: ~10 MB/s compression, ~1000 MB/s decompression")
print()
if selected_level == 1:
print_color("→ Network is fast! Using minimal compression to save CPU.", Colors.GREEN)
elif selected_level <= 3:
print_color("→ Network is fairly fast. Using light compression.", Colors.GREEN)
elif selected_level <= 6:
print_color("→ Network speed is moderate. Balanced compression/speed tradeoff.", Colors.YELLOW)
elif selected_level <= 9:
print_color("→ Network is slow. Using higher compression to reduce transfer time.", Colors.YELLOW)
else:
print_color("→ Network is very slow! Using maximum compression.", Colors.RED)
return selected_level
def select_compression_level_for_file(file_size_bytes, network_speed_mbps, base_level):
"""
Select optimal compression level for a specific file based on size and network speed
Logic:
- Small files (< 1 MB): Use lower compression (overhead not worth it)
- Medium files (1-100 MB): Use base level from network speed
- Large files (> 100 MB): Potentially increase compression (more savings)
Also considers compression speed vs network speed tradeoff:
- If compression would be slower than network transfer, reduce level
- If file is large and network is slow, increase level
"""
file_size_mb = file_size_bytes / (1024 * 1024)
# Compression speeds (MB/s) for different levels
compression_speeds = {1: 500, 3: 200, 6: 100, 9: 40, 15: 10}
# Start with base level
selected_level = base_level
# Adjust based on file size
if file_size_mb < 1:
# Small files: use minimal compression (overhead dominates)
selected_level = min(selected_level, 1)
elif file_size_mb < 10:
# Medium-small files: slightly reduce compression
selected_level = max(1, selected_level - 2)
elif file_size_mb > 100:
# Large files: can benefit from higher compression if network is slow
if network_speed_mbps < 10:
selected_level = min(15, selected_level + 2)
elif network_speed_mbps < 50:
selected_level = min(9, selected_level + 1)
# Ensure compression won't bottleneck the transfer
# If compression speed is much slower than network, reduce level
estimated_comp_speed = compression_speeds.get(selected_level, 100)
if estimated_comp_speed < network_speed_mbps * 0.5:
# Compression is too slow, drop a few levels
if selected_level > 6:
selected_level = 6
elif selected_level > 3:
selected_level = 3
# Clamp to valid range
selected_level = max(1, min(22, selected_level))
return selected_level
def calculate_compression_benefit(network_speed_mbps, file_size_mb, compression_ratio):
"""
Calculate if compression is beneficial given network speed and compression ratio
Returns: (time_saved_seconds, benefit_description)
"""
# Estimate compression/decompression speeds (MB/s) - these are rough estimates
compression_speeds = {1: 500, 3: 200, 6: 100, 9: 40, 15: 10}
decompression_speed = 1500 # MB/s (decompression is usually fast)
# Time without compression
transfer_time_uncompressed = (file_size_mb * 2) / network_speed_mbps # upload + download
# Time with compression (for each level)
benefits = {}
for level, comp_speed in compression_speeds.items():
compressed_size = file_size_mb * (1 - compression_ratio / 100)
compress_time = file_size_mb / comp_speed
decompress_time = compressed_size / decompression_speed
transfer_time = (compressed_size * 2) / network_speed_mbps
total_time_compressed = compress_time + transfer_time + decompress_time
time_saved = transfer_time_uncompressed - total_time_compressed
benefits[level] = {
'time_saved': time_saved,
'total_time': total_time_compressed,
'worthwhile': time_saved > 0
}
return benefits
# Import all the verification functions from the fully verified version
def verify_file_exists(filepath, description="File"):
"""Verify that a file exists and is readable"""
if not os.path.exists(filepath):
return False
if not os.path.isfile(filepath):
return False
if not os.access(filepath, os.R_OK):
return False
return True
def get_irods_file_size(session, irods_path):
"""Get the actual size of a file in iRODS"""
try:
obj = session.data_objects.get(irods_path)
return obj.size
except:
return None
def verify_irods_upload(session, irods_path, expected_size):
"""Verify that a file was actually uploaded to iRODS with correct size"""
try:
obj = session.data_objects.get(irods_path)
actual_size = obj.size
if actual_size is None or actual_size == 0:
return False, 0
if actual_size != expected_size:
return False, actual_size
return True, actual_size
except:
return False, 0
def verify_local_download(filepath, expected_size):
"""Verify that a file was actually downloaded with correct size"""
if not os.path.exists(filepath):
return False, 0
actual_size = os.path.getsize(filepath)
if actual_size == 0 or actual_size != expected_size:
return False, actual_size
return True, actual_size
def get_test_files(test_files_dir):
"""Get list of test files from directory"""
if not os.path.exists(test_files_dir):
print_color(f"Error: Test files directory not found: {test_files_dir}", Colors.RED)
sys.exit(1)
files = []
for item in os.listdir(test_files_dir):
filepath = os.path.join(test_files_dir, item)
if os.path.isfile(filepath) and verify_file_exists(filepath):
files.append(filepath)
if not files:
print_color(f"Error: No valid files found in {test_files_dir}", Colors.RED)
sys.exit(1)
return files
def compress_file_zstd(input_file, compression_level=3):
"""Compress file using Zstandard with specified level, or return original if level is 0"""
if compression_level == 0:
# No compression - just return the original file
return input_file
if not ZSTD_AVAILABLE:
print_color("Error: zstandard library not available", Colors.RED)
sys.exit(1)
compressed_file = tempfile.mktemp(suffix='.zst')
cctx = zstd.ZstdCompressor(level=compression_level, threads=-1)
with open(input_file, 'rb') as f_in:
with open(compressed_file, 'wb') as f_out:
cctx.copy_stream(f_in, f_out)
return compressed_file
def decompress_file_zstd(compressed_file, output_file):
"""Decompress Zstandard file"""
if not ZSTD_AVAILABLE:
print_color("Error: zstandard library not available", Colors.RED)
sys.exit(1)
dctx = zstd.ZstdDecompressor()
with open(compressed_file, 'rb') as f_in:
with open(output_file, 'wb') as f_out:
dctx.copy_stream(f_in, f_out)
def add_compression_metadata(session, irods_path, compression_algorithm, original_size,
compression_ratio, compression_level=None):
"""Add compression metadata to iRODS object"""
try:
obj = session.data_objects.get(irods_path)
obj.metadata.add(METADATA_COMPRESSION_ALGORITHM, compression_algorithm)
obj.metadata.add(METADATA_ORIGINAL_SIZE, str(original_size))
obj.metadata.add(METADATA_COMPRESSION_RATIO, f"{compression_ratio:.2f}")
if compression_level is not None:
obj.metadata.add(METADATA_COMPRESSION_LEVEL, str(compression_level))
return True, None
except Exception as e:
return False, str(e)
def verify_metadata_written(session, irods_path, expected_algorithm, expected_size):
"""Verify that metadata was actually written"""
try:
obj = session.data_objects.get(irods_path)
metadata = {}
for item in obj.metadata.items():
if item.name == METADATA_COMPRESSION_ALGORITHM:
metadata['compression_algorithm'] = item.value
elif item.name == METADATA_ORIGINAL_SIZE:
metadata['original_size_bytes'] = int(item.value)
required_fields = ['compression_algorithm', 'original_size_bytes']
missing = [f for f in required_fields if f not in metadata]
if missing:
return False, metadata, f"Missing fields: {missing}"
if (metadata['compression_algorithm'] != expected_algorithm or
metadata['original_size_bytes'] != expected_size):
return False, metadata, "Value mismatch"
return True, metadata, None
except Exception as e:
return False, {}, str(e)
def read_compression_metadata(session, irods_path):
"""Read compression metadata from iRODS object"""
try:
obj = session.data_objects.get(irods_path)
metadata = {}
for item in obj.metadata.items():
if item.name == METADATA_COMPRESSION_ALGORITHM:
metadata['compression_algorithm'] = item.value
elif item.name == METADATA_ORIGINAL_SIZE:
metadata['original_size_bytes'] = int(item.value)
elif item.name == METADATA_COMPRESSION_RATIO:
metadata['compression_ratio_percent'] = float(item.value)
elif item.name == METADATA_COMPRESSION_LEVEL:
metadata['compression_level'] = int(item.value)
if not metadata:
return False, {}, "No metadata found"
return True, metadata, None
except Exception as e:
return False, {}, str(e)
def decompress_based_on_metadata(compressed_file, output_file, metadata):
"""Decompress file based on metadata"""
algorithm = metadata.get('compression_algorithm', 'unknown')
if algorithm == 'zstd':
decompress_file_zstd(compressed_file, output_file)
elif algorithm == 'none':
import shutil
shutil.copy(compressed_file, output_file)
else:
raise ValueError(f"Unknown compression algorithm: {algorithm}")
def calculate_file_checksum(filepath, algorithm='sha256'):
"""Calculate checksum of a file"""
hash_obj = hashlib.new(algorithm)
with open(filepath, 'rb') as f:
for chunk in iter(lambda: f.read(4096), b''):
hash_obj.update(chunk)
return hash_obj.hexdigest()
def cleanup_irods_directory(session, path_pattern="benchmark_"):
"""Clean up old benchmark files"""
try:
zone = session.zone
username = session.username
home_path = f"/{zone}/home/{username}"
print_color(f"\nCleaning up iRODS directory: {home_path}", Colors.YELLOW)
coll = session.collections.get(home_path)
objects_removed = 0
for obj in coll.data_objects:
if path_pattern in obj.name or ".speedtest_" in obj.name:
try:
obj_path = f"{home_path}/{obj.name}"
session.data_objects.unlink(obj_path, force=True)
objects_removed += 1
except:
pass
if objects_removed > 0:
print_color(f"✓ Cleaned up {objects_removed} file(s)", Colors.GREEN)
else:
print_color(f"✓ Directory already clean", Colors.GREEN)
return objects_removed
except Exception as e:
print_color(f"Warning: Cleanup failed: {e}", Colors.YELLOW)
return 0
def create_session():
"""Create iRODS session"""
try:
env_file = os.environ.get('IRODS_ENVIRONMENT_FILE',
os.path.expanduser('~/.irods/irods_environment.json'))
if not os.path.exists(env_file):
print_color(f"Error: Environment file not found: {env_file}", Colors.RED)
sys.exit(1)
session = iRODSSession(irods_env_file=env_file)
return session
except Exception as e:
print_color(f"Error: Cannot connect to iRODS", Colors.RED)
sys.exit(1)
def verify_connection(session):
"""Verify iRODS connection"""
print_color("\nVerifying iRODS connection...", Colors.YELLOW)
try:
zone = session.zone
username = session.username
home_path = f"/{zone}/home/{username}"
print_color(f" Zone: {zone}", Colors.BLUE)
print_color(f" User: {username}", Colors.BLUE)
print_color(f" Host: {session.host}:{session.port}", Colors.BLUE)
coll = session.collections.get(home_path)
print_color(f" ✓ Connection verified", Colors.GREEN)
return True
except Exception as e:
print_color(f" ✗ Connection failed: {e}", Colors.RED)
return False
def run_performance_test(session, test_files, test_runs, base_compression_level,
enable_verification, enable_metadata, network_speed_mbps=None):
"""Run performance test with per-file compression level selection"""
results = []
zone = session.zone
username = session.username
print_color(f"\nRunning {test_runs} test iterations...", Colors.YELLOW)
print_color(f" Base compression level: {base_compression_level}", Colors.CYAN)
if ENABLE_ADAPTIVE_COMPRESSION and network_speed_mbps:
print_color(f" Adaptive compression: ENABLED (per-file)", Colors.CYAN)
else:
print_color(f" Compression level: FIXED at {base_compression_level}", Colors.CYAN)
print_color(f" Verification: {'ENABLED' if enable_verification else 'DISABLED'}", Colors.CYAN)
print_color(f" Metadata: {'ENABLED' if enable_metadata else 'DISABLED'}", Colors.CYAN)
failed_runs = 0
for run_num in range(1, test_runs + 1):
for test_file in test_files:
filename = os.path.basename(test_file)
if not verify_file_exists(test_file):
failed_runs += 1
continue
original_size = os.path.getsize(test_file)
if enable_verification:
original_checksum = calculate_file_checksum(test_file)
# Select compression level for this specific file
if ENABLE_ADAPTIVE_COMPRESSION and network_speed_mbps:
compression_level = select_compression_level_for_file(
original_size, network_speed_mbps, base_compression_level
)
else:
compression_level = 0 # No compression
# Compress with selected level (or skip if level is 0)
if compression_level == 0:
print(f" Run {run_num}/{test_runs} [{filename}]: No compression... ", end='', flush=True)
compress_start = time.time()
upload_file = test_file # Use original file
compress_time = time.time() - compress_start
transfer_size = original_size
compression_ratio = 0
print(f"✓ ({format_size(transfer_size)}) ", end='', flush=True)
else:
print(f" Run {run_num}/{test_runs} [{filename}]: Compress(L{compression_level})... ", end='', flush=True)
compress_start = time.time()
upload_file = compress_file_zstd(test_file, compression_level)
compress_time = time.time() - compress_start
if not verify_file_exists(upload_file):
print_color(f"✗ FAILED", Colors.RED)
failed_runs += 1
continue
transfer_size = os.path.getsize(upload_file)
compression_ratio = (1 - transfer_size / original_size) * 100 if original_size > 0 else 0
print(f"✓ ({format_size(transfer_size)}, {compression_ratio:.1f}%, {compress_time:.3f}s) ", end='', flush=True)
# Use original filename in iRODS
irods_path = f"/{zone}/home/{username}/{filename}"
try:
# Upload
print("Up... ", end='', flush=True)
upload_start = time.time()
try:
session.data_objects.put(upload_file, irods_path, force=True) # might switch to parallel threading depending on client, flag for the put call set to 1
upload_time = time.time() - upload_start
except iRODSException as irods_err:
print_color(f"✗ iRODS ERROR: {type(irods_err).__name__}", Colors.RED)
print_color(f" Message: {str(irods_err)}", Colors.RED)
if hasattr(irods_err, 'error_code'):
print_color(f" Error code: {irods_err.error_code}", Colors.RED)
raise
upload_verified, irods_size = verify_irods_upload(session, irods_path, transfer_size)
if not upload_verified:
print_color(f"✗ FAILED", Colors.RED)
failed_runs += 1
if compression_level > 0: # Only delete temp file if compressed
os.unlink(upload_file)
continue
print(f"✓ ", end='', flush=True)
# Metadata
if enable_metadata and compression_level > 0:
print("Meta... ", end='', flush=True)
add_compression_metadata(session, irods_path, "zstd", original_size,
compression_ratio, compression_level)
print(f"✓ ", end='', flush=True)
elif enable_metadata and compression_level == 0:
print("Meta... ", end='', flush=True)
add_compression_metadata(session, irods_path, "none", original_size, 0, 0)
print(f"✓ ", end='', flush=True)
# Download
print("Down... ", end='', flush=True)
download_file = tempfile.mktemp(suffix='.tmp')
download_start = time.time()
session.data_objects.get(irods_path, download_file, force=True)
download_time = time.time() - download_start
download_verified, local_size = verify_local_download(download_file, transfer_size)
if not download_verified:
print_color(f"✗ FAILED", Colors.RED)
failed_runs += 1
if compression_level > 0: # Only delete temp file if compressed
os.unlink(upload_file)
session.data_objects.unlink(irods_path, force=True)
continue
# Decompress (or skip if no compression)
if compression_level == 0:
print(f"✓ No decomp... ", end='', flush=True)
decompressed_file = download_file # Use downloaded file directly
decompress_time = 0
else:
print(f"✓ Decomp... ", end='', flush=True)
decompress_start = time.time()
decompressed_file = tempfile.mktemp(suffix='.dat')
if enable_metadata:
meta_success, metadata, _ = read_compression_metadata(session, irods_path)
if meta_success:
decompress_based_on_metadata(download_file, decompressed_file, metadata)
else:
decompress_file_zstd(download_file, decompressed_file)
decompress_time = time.time() - decompress_start
if not verify_file_exists(decompressed_file):
print_color(f"✗ FAILED", Colors.RED)
failed_runs += 1
if compression_level > 0:
os.unlink(upload_file)
if compression_level > 0 or decompressed_file != download_file:
os.unlink(download_file)
session.data_objects.unlink(irods_path, force=True)
continue
final_size = os.path.getsize(decompressed_file)
if enable_verification:
final_checksum = calculate_file_checksum(decompressed_file)
# Only delete decompressed file if it's different from download file
if compression_level > 0 and decompressed_file != download_file:
os.unlink(decompressed_file)
# Verify
verification_passed = True
if final_size != original_size:
verification_passed = False
if enable_verification and original_checksum != final_checksum:
verification_passed = False
if not verification_passed:
print_color(f"✗ FAILED verification", Colors.RED)
failed_runs += 1
if compression_level > 0:
os.unlink(upload_file)
os.unlink(download_file)
session.data_objects.unlink(irods_path, force=True)
continue
# Cleanup
os.unlink(download_file)
if compression_level > 0: # Only delete if it's a temp compressed file
os.unlink(upload_file)
session.data_objects.unlink(irods_path, force=True)
# Calculate throughput
upload_throughput = (transfer_size / (1024 * 1024)) / upload_time if upload_time > 0 else 0
download_throughput = (transfer_size / (1024 * 1024)) / download_time if download_time > 0 else 0
print_color(f"✓ VERIFIED (Up:{upload_time:.2f}s Down:{download_time:.2f}s)", Colors.GREEN)
# Store results
results.append({
'run': run_num,
'filename': filename,
'original_size_bytes': original_size,
'transfer_size_bytes': transfer_size,
'compression_level': compression_level,
'compression_ratio': compression_ratio,
'compress_time': compress_time,
'upload_time': upload_time,
'download_time': download_time,
'decompress_time': decompress_time,
'upload_throughput_mbps': upload_throughput,
'download_throughput_mbps': download_throughput,
})
except Exception as e:
error_msg = f"{type(e).__name__}: {str(e)}" if e else "Unknown error"
print_color(f"✗ FAILED: {error_msg}", Colors.RED)
failed_runs += 1
try:
if os.path.exists(upload_file):
os.unlink(upload_file)
if 'download_file' in locals() and os.path.exists(download_file):
os.unlink(download_file)
session.data_objects.unlink(irods_path, force=True)
except:
pass
continue
if failed_runs > 0:
print_color(f"\nWarning: {failed_runs} runs failed", Colors.YELLOW)
return results
def calculate_aggregate_statistics(results):
"""Calculate aggregate statistics from results"""
if not results:
return None
total_runs = len(results)
files_data = {}
for r in results:
fname = r['filename']
if fname not in files_data:
files_data[fname] = []
files_data[fname].append(r)
total_upload_time = sum(r['upload_time'] for r in results)
total_download_time = sum(r['download_time'] for r in results)
total_compress_time = sum(r['compress_time'] for r in results)
total_decompress_time = sum(r['decompress_time'] for r in results)
avg_upload_time = total_upload_time / total_runs
avg_download_time = total_download_time / total_runs
avg_upload_throughput = sum(r['upload_throughput_mbps'] for r in results) / total_runs
avg_download_throughput = sum(r['download_throughput_mbps'] for r in results) / total_runs
return {
'total_runs': total_runs,
'files_tested': list(files_data.keys()),
'total_upload_time': total_upload_time,
'total_download_time': total_download_time,
'total_compress_time': total_compress_time,
'total_decompress_time': total_decompress_time,
'avg_upload_time': avg_upload_time,
'avg_download_time': avg_download_time,
'avg_upload_throughput': avg_upload_throughput,
'avg_download_throughput': avg_download_throughput,
'files_data': files_data,
}
def save_results(results_dir, timestamp, results, compression_level, session,
network_speed_info=None, resource_stats=None):
"""Save detailed results to file including resource usage and network info"""
if not results:
return None
stats = calculate_aggregate_statistics(results)
if not stats:
return None
Path(results_dir).mkdir(parents=True, exist_ok=True)
results_file = os.path.join(results_dir,
f"adaptive_benchmark_L{compression_level}_{timestamp}.txt")
with open(results_file, 'w') as f:
f.write("iRODS Adaptive Compression Benchmark Results\n")
f.write("=" * 80 + "\n")
f.write(f"Date: {datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')}\n")
f.write(f"Timestamp: {timestamp}\n")
f.write(f"User: {os.getenv('USER', 'unknown')}\n")
f.write(f"iRODS User: {session.username}\n")
f.write(f"iRODS Zone: {session.zone}\n")
f.write(f"iRODS Host: {session.host}:{session.port}\n")
f.write(f"Method: Python iRODS Client (PRC) with Adaptive Compression\n")
f.write(f"Compression Algorithm: Zstandard\n")
f.write(f"Compression Level: {compression_level}\n")
f.write(f"Total test runs: {stats['total_runs']}\n")
f.write(f"Files tested: {', '.join(stats['files_tested'])}\n")
f.write("\n")
# Network speed info
if network_speed_info:
f.write("Network Speed Test Results:\n")
f.write("-" * 80 + "\n")
f.write(f"Test file size: {NETWORK_TEST_SIZE_MB} MB\n")
f.write(f"Test samples: {NETWORK_TEST_SAMPLES}\n")
f.write(f"Upload speed: {network_speed_info['upload_mbps']:.2f} MB/s\n")
f.write(f"Download speed: {network_speed_info['download_mbps']:.2f} MB/s\n")
f.write(f"Average speed: {network_speed_info['avg_mbps']:.2f} MB/s\n")
f.write(f"Average latency: {network_speed_info['latency_ms']:.0f} ms\n")
f.write(f"Strategy selected: {network_speed_info['strategy']}\n")
f.write(f"Rationale: {network_speed_info['rationale']}\n")
f.write("\n")
f.write("Aggregate Statistics:\n")
f.write("-" * 80 + "\n")
f.write(f"Total upload time: {stats['total_upload_time']:.3f}s\n")
f.write(f"Total download time: {stats['total_download_time']:.3f}s\n")
f.write(f"Total compression time: {stats['total_compress_time']:.3f}s\n")
f.write(f"Total decompression time: {stats['total_decompress_time']:.3f}s\n")
f.write(f"Average upload time: {stats['avg_upload_time']:.3f}s\n")
f.write(f"Average download time: {stats['avg_download_time']:.3f}s\n")
f.write(f"Average upload throughput: {stats['avg_upload_throughput']:.2f} MB/s\n")
f.write(f"Average download throughput: {stats['avg_download_throughput']:.2f} MB/s\n")
f.write("\n")
# Per-file statistics
f.write("Per-File Statistics:\n")
f.write("-" * 80 + "\n")
for filename, file_results in stats['files_data'].items():
f.write(f"\nFile: {filename}\n")
f.write(f" Original size: {format_size(file_results[0]['original_size_bytes'])}\n")
avg_transfer_size = sum(r['transfer_size_bytes'] for r in file_results) / len(file_results)
avg_compression_ratio = sum(r['compression_ratio'] for r in file_results) / len(file_results)
f.write(f" Avg compressed size: {format_size(avg_transfer_size)}\n")
f.write(f" Avg compression ratio: {avg_compression_ratio:.1f}%\n")
f.write(f" Runs: {len(file_results)}\n")
avg_up = sum(r['upload_time'] for r in file_results) / len(file_results)
avg_down = sum(r['download_time'] for r in file_results) / len(file_results)
f.write(f" Avg upload time: {avg_up:.3f}s\n")
f.write(f" Avg download time: {avg_down:.3f}s\n")
f.write("\n")
f.write("Detailed Results:\n")
f.write("-" * 80 + "\n")
f.write("Run | File | Size | Compressed | Upload | Download | Ratio\n")
f.write("-" * 80 + "\n")
for r in results:
f.write(f"{r['run']:3d} | {r['filename'][:20]:20s} | ")
f.write(f"{format_size(r['original_size_bytes']):>10s} | ")
f.write(f"{format_size(r['transfer_size_bytes']):>10s} | ")
f.write(f"{r['upload_time']:6.3f}s | {r['download_time']:6.3f}s | ")
f.write(f"{r['compression_ratio']:5.1f}%\n")
# Add resource usage statistics if provided
if resource_stats:
f.write("\n")
f.write("=" * 80 + "\n")
f.write("CLIENT-SIDE RESOURCE USAGE\n")
f.write("=" * 80 + "\n")
f.write(f"Test Duration: {resource_stats['duration_seconds']:.2f} seconds\n")
f.write(f"Samples Collected: {resource_stats['samples_collected']}\n")
f.write("\n")
f.write("CPU Usage:\n")
f.write(f" Average CPU: {resource_stats['avg_cpu_percent']:.2f}%\n")
f.write(f" Peak CPU: {resource_stats['max_cpu_percent']:.2f}%\n")
f.write("\n")
f.write("Memory Usage:\n")
f.write(f" Start memory (RSS): {resource_stats['start_memory_mb']:.2f} MB\n")
f.write(f" Average memory: {resource_stats['avg_memory_mb']:.2f} MB\n")
f.write(f" Peak memory: {resource_stats['peak_memory_mb']:.2f} MB\n")
f.write(f" Final memory: {resource_stats['final_memory_mb']:.2f} MB\n")
f.write(f" Memory delta: {resource_stats['memory_delta_mb']:+.2f} MB\n")
return results_file, stats
def main():
"""Main execution with adaptive compression"""
print_color("=" * 80, Colors.GREEN)
print_color("iRODS ADAPTIVE COMPRESSION BENCHMARK", Colors.GREEN)
print_color("Network-aware compression level selection", Colors.GREEN)
print_color("=" * 80, Colors.GREEN)
timestamp = datetime.utcnow().strftime('%Y-%m-%d_%H-%M-%S')
print(f"\nDate: {datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')}")
print(f"Test runs: {TEST_RUNS}")
print(f"Adaptive compression: {'ENABLED' if ENABLE_ADAPTIVE_COMPRESSION else 'DISABLED (no compression)'}")
if ENABLE_ADAPTIVE_COMPRESSION and not ZSTD_AVAILABLE:
print_color("\nError: zstandard not installed! Install with: pip install zstandard", Colors.RED)
sys.exit(1)
test_files = get_test_files(TEST_FILES_DIR)
print_color(f"\nFound {len(test_files)} test file(s):", Colors.CYAN)
for f in test_files:
size = os.path.getsize(f)
print(f" - {os.path.basename(f):30s} ({format_size(size)})")
monitor = ResourceMonitor(interval=0.1)
monitor.start()
print_color("\nConnecting to iRODS...", Colors.YELLOW)
session = create_session()
print_color(f"✓ Connected as {session.username}@{session.zone}", Colors.GREEN)
if not verify_connection(session):
session.cleanup()
sys.exit(1)
try:
if ENABLE_CLEANUP:
cleanup_irods_directory(session)
# Test network speed
if ENABLE_ADAPTIVE_COMPRESSION:
upload_speed, download_speed, latency = test_network_speed(
session, NETWORK_TEST_SIZE_MB, NETWORK_TEST_SAMPLES
)
if upload_speed is None:
print_color("\nNetwork test failed, disabling compression", Colors.YELLOW)
compression_level = 0
else:
# Use average of upload/download for compression decision
avg_speed = (upload_speed + download_speed) / 2
compression_level = select_compression_level(avg_speed)
else:
compression_level = 0
print_color(f"\nAdaptive compression disabled - No compression will be used", Colors.CYAN)
# Run benchmark
# Pass network speed for per-file adaptive compression
avg_speed = None
if ENABLE_ADAPTIVE_COMPRESSION and 'upload_speed' in locals() and upload_speed is not None:
avg_speed = (upload_speed + download_speed) / 2
results = run_performance_test(
session, test_files, TEST_RUNS, compression_level,
ENABLE_FILE_VERIFICATION, ENABLE_METADATA, avg_speed
)
if not results:
print_color("\nNo successful test runs", Colors.RED)