davanstrien HF Staff commited on
Commit
71d6f99
·
1 Parent(s): 76fb80b

speed test script

Browse files
Files changed (1) hide show
  1. network-speed-test.py +126 -0
network-speed-test.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # /// script
3
+ # requires-python = ">=3.10"
4
+ # dependencies = []
5
+ # ///
6
+
7
+ """
8
+ Simple network speed test for HF Jobs.
9
+
10
+ Tests download speed by fetching a file from Hugging Face Hub.
11
+ Useful for verifying network performance in different HF Jobs flavors.
12
+
13
+ Usage:
14
+ # Run locally
15
+ uv run network-speed-test.py
16
+
17
+ # Run on HF Jobs
18
+ hfjobs run --flavor l4x1 \
19
+ -e HF_TOKEN=$(python3 -c "from huggingface_hub import get_token; print(get_token())") \
20
+ uv run https://huggingface.co/datasets/uv-scripts/jobs-utils/raw/main/network-speed-test.py
21
+
22
+ Example output:
23
+ Testing download speed...
24
+ Downloaded 100.0 MB in 2.34 seconds
25
+ Download speed: 42.74 MB/s (341.89 Mbps)
26
+ """
27
+
28
+ import sys
29
+ import time
30
+ import urllib.request
31
+ import argparse
32
+ from typing import Optional
33
+
34
+
35
+ def format_bytes(bytes_value: int) -> str:
36
+ """Convert bytes to human-readable format."""
37
+ for unit in ["B", "KB", "MB", "GB"]:
38
+ if bytes_value < 1024.0:
39
+ return f"{bytes_value:.2f} {unit}"
40
+ bytes_value /= 1024.0
41
+ return f"{bytes_value:.2f} TB"
42
+
43
+
44
+ def test_download_speed(url: str, size_hint: Optional[int] = None) -> tuple[float, int]:
45
+ """
46
+ Download a file and measure speed.
47
+
48
+ Returns:
49
+ Tuple of (duration_seconds, bytes_downloaded)
50
+ """
51
+ print(f"Testing download speed from: {url}")
52
+ print("Downloading...", flush=True)
53
+
54
+ start_time = time.time()
55
+
56
+ with urllib.request.urlopen(url) as response:
57
+ data = response.read()
58
+
59
+ duration = time.time() - start_time
60
+ bytes_downloaded = len(data)
61
+
62
+ return duration, bytes_downloaded
63
+
64
+
65
+ def main():
66
+ parser = argparse.ArgumentParser(
67
+ description="Test network speed for HF Jobs",
68
+ formatter_class=argparse.RawDescriptionHelpFormatter,
69
+ )
70
+ parser.add_argument(
71
+ "--url",
72
+ type=str,
73
+ default="https://huggingface.co/datasets/OpenWebText/resolve/main/README.md",
74
+ help="URL to download for testing (default: sample HF file)",
75
+ )
76
+ parser.add_argument(
77
+ "--large",
78
+ action="store_true",
79
+ help="Use a larger file for testing (~100MB model file)",
80
+ )
81
+
82
+ args = parser.parse_args()
83
+
84
+ # If --large flag is set, use a bigger file
85
+ test_url = args.url
86
+ if args.large:
87
+ # Use a model file that's about 100MB
88
+ test_url = "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/pytorch_model.bin"
89
+ print("Using large file test (~100MB)")
90
+
91
+ print("=" * 60)
92
+ print("HF Jobs Network Speed Test")
93
+ print("=" * 60)
94
+ print()
95
+
96
+ try:
97
+ duration, bytes_downloaded = test_download_speed(test_url)
98
+
99
+ # Calculate speeds
100
+ mb_downloaded = bytes_downloaded / (1024 * 1024)
101
+ speed_mbps = (bytes_downloaded * 8) / (duration * 1_000_000) # Megabits per second
102
+ speed_mb_s = mb_downloaded / duration # Megabytes per second
103
+
104
+ print()
105
+ print("=" * 60)
106
+ print("Results:")
107
+ print("=" * 60)
108
+ print(f"Downloaded: {format_bytes(bytes_downloaded)}")
109
+ print(f"Time: {duration:.2f} seconds")
110
+ print(f"Speed: {speed_mb_s:.2f} MB/s ({speed_mbps:.2f} Mbps)")
111
+ print("=" * 60)
112
+
113
+ # Exit successfully
114
+ return 0
115
+
116
+ except urllib.error.URLError as e:
117
+ print(f"\n❌ Error downloading file: {e}", file=sys.stderr)
118
+ print("\nThis could indicate network connectivity issues.", file=sys.stderr)
119
+ return 1
120
+ except Exception as e:
121
+ print(f"\n❌ Unexpected error: {e}", file=sys.stderr)
122
+ return 1
123
+
124
+
125
+ if __name__ == "__main__":
126
+ sys.exit(main())