Guest User

MacOS Python script - Test external USB/SSD performance

a guest
Jan 16th, 2026
14
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | Source Code | 1 0
  1. import os
  2. import time
  3. import sys
  4. import secrets
  5.  
  6. def get_size(bytes, suffix="B"):
  7. """Scale bytes to its proper format"""
  8. factor = 1024
  9. for unit in ["", "K", "M", "G", "T", "P"]:
  10. if bytes < factor:
  11. return f"{bytes:.2f}{unit}{suffix}"
  12. bytes /= factor
  13.  
  14. def run_test():
  15. print("--- macOS Python SSD Benchmark ---")
  16. path = input("Drag and drop the target folder here: ").strip().replace("'", "").replace('"', '').strip()
  17.  
  18. if not os.path.isdir(path):
  19. print(f"Error: {path} is not a valid directory.")
  20. return
  21.  
  22. filename = os.path.join(path, "py_test_file.bin")
  23. # File size: 1GB (adjust strictly if needed)
  24. file_size_mb = 1024
  25. file_size_bytes = file_size_mb * 1024 * 1024
  26. buffer_size = 1024 * 1024 # 1MB chunks
  27.  
  28. print(f"\nTarget: {path}")
  29. print(f"File Size: {file_size_mb} MB")
  30.  
  31. # Generate random data chunk (1MB) to defeat compression algorithms
  32. print("Preparing random data to ensure accuracy...")
  33. random_data = secrets.token_bytes(buffer_size)
  34.  
  35. # --- WRITE TEST ---
  36. print(f"\n[1/3] Running Write Test...")
  37. try:
  38. start_time = time.time()
  39. with open(filename, 'wb') as f:
  40. for _ in range(file_size_mb):
  41. f.write(random_data)
  42. # Force write to disk (bypass OS write cache)
  43. f.flush()
  44. os.fsync(f.fileno())
  45. end_time = time.time()
  46.  
  47. duration = end_time - start_time
  48. speed = (file_size_bytes / duration) / (1024 * 1024)
  49. print(f"✅ Write Speed: {speed:.2f} MB/s")
  50.  
  51. except Exception as e:
  52. print(f"Write failed: {e}")
  53. return
  54.  
  55. # --- FLUSH CACHE ---
  56. print("\n[2/3] Flushing RAM Cache...")
  57. print("NOTE: We need to purge macOS file cache to measure real disk Read speed.")
  58. print("You may be asked for your password (sudo purge).")
  59. os.system("sudo purge")
  60. # Wait a moment for system to settle
  61. time.sleep(2)
  62.  
  63. # --- READ TEST ---
  64. print("\n[3/3] Running Read Test...")
  65. try:
  66. start_time = time.time()
  67. with open(filename, 'rb') as f:
  68. while True:
  69. data = f.read(buffer_size)
  70. if not data:
  71. break
  72. end_time = time.time()
  73.  
  74. duration = end_time - start_time
  75. speed = (file_size_bytes / duration) / (1024 * 1024)
  76. print(f"✅ Read Speed: {speed:.2f} MB/s")
  77.  
  78. except Exception as e:
  79. print(f"Read failed: {e}")
  80.  
  81. # --- CLEANUP ---
  82. try:
  83. os.remove(filename)
  84. print("\nTest file removed.")
  85. except:
  86. print("\nCould not remove test file.")
  87.  
  88. if __name__ == "__main__":
  89. run_test()
Advertisement
Add Comment
Please, Sign In to add comment