measure_download_speed
Measure download speed using incremental file sizes with adjustable maximum size and sustain duration. Returns speed test results.
Instructions
Measure download speed using incremental file sizes.
Args:
size_limit: Maximum file size to test (default: 128MB)
sustain_time: Duration in seconds for each test (1-8, default: 8)
Returns:
Dictionary with download speed resultsInput Schema
| Name | Required | Description | Default |
|---|---|---|---|
| size_limit | No | 128MB | |
| sustain_time | No |
Implementation Reference
- mcp_internet_speed_test/main.py:495-500 (registration)Tool registered with FastMCP using the @mcp.tool decorator with a download icon
@mcp.tool(icons=[ICON_DOWNLOAD]) async def measure_download_speed( size_limit: str = "128MB", sustain_time: int = 8, context: Context[ServerSession, None] = None, ) -> dict: - mcp_internet_speed_test/main.py:496-605 (handler)Main handler function that measures download speed by iterating through incremental file sizes (128KB to 128MB) and streaming downloads via httpx, returning speed in Mbps
async def measure_download_speed( size_limit: str = "128MB", sustain_time: int = 8, context: Context[ServerSession, None] = None, ) -> dict: """ Measure download speed using incremental file sizes. Args: size_limit: Maximum file size to test (default: 128MB) sustain_time: Duration in seconds for each test (1-8, default: 8) Returns: Dictionary with download speed results """ # Validate sustain_time sustain_time = max(MIN_TEST_DURATION, min(MAX_TEST_DURATION, float(sustain_time))) results = [] final_result = None # Find the index of the size limit in our progression max_index = ( SIZE_PROGRESSION.index(size_limit) if size_limit in SIZE_PROGRESSION else len(SIZE_PROGRESSION) - 1 ) total_steps = max_index + 1 current_step = 0 await safe_log_info(context, "Starting download speed test...") # Test each file size in order, up to the specified limit # Methodology: download each size, if it takes < 8s move to next, # if it takes >= 8s use that result as final and stop. async with httpx.AsyncClient() as client: for size_key in SIZE_PROGRESSION[: max_index + 1]: current_step += 1 progress_message = f"Testing {size_key} file..." await safe_report_progress(context, current_step, total_steps, progress_message) url = DEFAULT_DOWNLOAD_URLS[size_key] start = time.time() total_size = 0 current_result = None async with client.stream( "GET", url, ) as response: # Extract server information from headers server_info = extract_server_info(dict(response.headers)) async for chunk in response.aiter_bytes(chunk_size=1024): if chunk: total_size += len(chunk) # Check elapsed time during download elapsed_time = time.time() - start # Update current result continuously speed_mbps = ((total_size * 8) / (1024 * 1024)) / elapsed_time current_result = { "download_speed": round(speed_mbps, 2), "elapsed_time": round(elapsed_time, 2), "data_size": total_size, "size": size_key, "url": url, "server_info": server_info, } # If test duration exceeded, stop streaming this file if elapsed_time >= sustain_time: break # Record final measurement for this file size if current_result is None: elapsed_time = time.time() - start current_result = { "download_speed": 0, "elapsed_time": round(elapsed_time, 2), "data_size": total_size, "size": size_key, "url": url, "server_info": server_info if total_size > 0 else None, } results.append(current_result) final_result = current_result # If this download took >= sustain_time, we found our measurement if current_result["elapsed_time"] >= sustain_time: break # Return the final result or an error if all tests failed if final_result: return { "download_speed": final_result["download_speed"], "unit": "Mbps", "elapsed_time": final_result["elapsed_time"], "data_size": final_result["data_size"], "size_used": final_result["size"], "server_info": final_result["server_info"], "all_tests": results, } return { "error": True, "message": "All download tests failed", "details": results, } - Input schema: size_limit (str, default '128MB') and sustain_time (int, 1-8, default 8)
async def measure_download_speed( size_limit: str = "128MB", sustain_time: int = 8, context: Context[ServerSession, None] = None, ) -> dict: - Map of size keys (128KB to 128MB) to GitHub media download URLs used by the download speed tool
DEFAULT_DOWNLOAD_URLS = { "128KB": f"{GITHUB_MEDIA_URL}/128KB.bin", "256KB": f"{GITHUB_MEDIA_URL}/256KB.bin", "512KB": f"{GITHUB_MEDIA_URL}/512KB.bin", "1MB": f"{GITHUB_MEDIA_URL}/1MB.bin", "2MB": f"{GITHUB_MEDIA_URL}/2MB.bin", "4MB": f"{GITHUB_MEDIA_URL}/4MB.bin", "8MB": f"{GITHUB_MEDIA_URL}/8MB.bin", "16MB": f"{GITHUB_MEDIA_URL}/16MB.bin", "32MB": f"{GITHUB_MEDIA_URL}/32MB.bin", "64MB": f"{GITHUB_MEDIA_URL}/64MB.bin", "128MB": f"{GITHUB_MEDIA_URL}/128MB.bin", } - Ordered list of size keys used to progressively test larger file sizes during download speed measurement
SIZE_PROGRESSION = [ "128KB", "256KB", "512KB", "1MB", "2MB", "4MB", "8MB", "16MB", "32MB", "64MB", "128MB", ]