mistral-small-2506· Mistral AI LMArena Human Preference 140K
pip install matplotlib numpy -i https://pypi.tuna.tsinghua.edu.cn/simple 在墙里…
- User
pip install matplotlib numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
在墙里 帮我写个脚本测一测中国的几个镜像哪个快
- Assistant
中国镜像速度测试脚本
下面是一个Python脚本,可以测试中国常用的PyPI镜像的下载速度,帮助你选择最快的镜像源。
import subprocess import time import matplotlib.pyplot as plt import numpy as np from concurrent.futures import ThreadPoolExecutor # 要测试的镜像列表 MIRRORS = { "官方": "https://pypi.org/simple", "清华": "https://pypi.tuna.tsinghua.edu.cn/simple", "阿里云": "https://mirrors.aliyun.com/pypi/simple", "豆瓣": "https://pypi.doubanio.com/simple", "华中科技大学": "https://pypi.hustunique.com/simple", "中科大": "https://pypi.mirrors.ustc.edu.cn/simple", "腾讯": "https://mirrors.cloud.tencent.com/pypi/simple", "华为": "https://repo.huaweicloud.com/repository/pypi/simple", "百度": "https://mirror.baidu.com/pypi/simple" } # 测试包 TEST_PACKAGE = "numpy" def test_mirror(mirror_name, mirror_url): """测试单个镜像的下载速度""" start_time = time.time() try: # 使用pip下载测试包 result = subprocess.run( ["pip", "download", TEST_PACKAGE, "-i", mirror_url, "--no-deps"], capture_output=True, text=True ) if result.returncode != 0: return mirror_name, 0, "失败", result.stderr # 计算下载时间 download_time = time.time() - start_time return mirror_name, download_time, "成功", "" except Exception as e: return mirror_name, 0, "异常", str(e) def run_tests(): """运行所有镜像测试""" results = [] # 使用线程池并行测试 with ThreadPoolExecutor(max_workers=5) as executor: futures = [executor.submit(test_mirror, name, url) for name, url in MIRRORS.items()] for future in futures: results.append(future.result()) return results def plot_results(results): """绘制测试结果图表""" # 分离数据 names = [r[0] for r in results] times = [r[1] for r in results] statuses = [r[2] for r in results] errors = [r[3] for r in results] # 创建图表 plt.figure(figsize=(12, 6)) # 绘制条形图 bars = plt.bar(names, times, color='skyblue') # 添加标签和标题 plt.title(f"PyPI镜像下载速度测试 ({TEST_PACKAGE})", fontsize=14) plt.xlabel("镜像源", fontsize=12) plt.ylabel("下载时间(秒)", fontsize=12) # 添加数据标签 for bar, time_val, status in zip(bars, times, statuses): if status == "成功": height = bar.get_height() plt.text(bar.get_x() + bar.get_width()/2., height, f'{height:.2f}s', ha='center', va='bottom', fontsize=10) else: # 失败的条形图用红色标记 bar.set_color('red') height = bar.get_height() plt.text(bar.get_x() + bar.get_width()/2., height, f'失败: {errors[names.index(bar.get_x())]}', ha='center', va='bottom', fontsize=10, rotation=90) # 旋转x轴标签 plt.xticks(rotation=45, ha='right') # 显示图表 plt.tight_layout() plt.show() # 打印详细结果 print("\n详细测试结果:") print("-" * 50) for name, time_val, status, error in results: if status == "成功": print(f"{name}: {time_val:.2f}秒") else: print(f"{name}: 失败 - {error}") # 找出最快的镜像 successful_results = [r for r in results if r[2] == "成功"] if successful_results: fastest = min(successful_results, key=lambda x: x[1]) print(f"\n最快的镜像是: {fastest[0]} ({fastest[1]:.2f}秒)") else: print("\n所有镜像测试都失败了") if __name__ == "__main__": print(f"正在测试 {TEST_PACKAGE} 包的下载速度...") results = run_tests() plot_results(results)使用说明
-
确保你的Python环境中已经安装了
matplotlib和numpy包 -
运行此脚本会测试以下镜像源:
- 官方PyPI
- 清华大学
- 阿里云
- 豆瓣
- 华中科技大学
- 中科大
- 腾讯
- 华为
- 百度
-
脚本会并行测试所有镜像,并生成一个条形图显示每个镜像的下载时间
-
最终会显示最快的镜像源
注意事项
- 由于网络波动,建议多次运行测试以获得更准确的结果
- 如果某个镜像失败,可能是该镜像暂时不可用
- 测试结果可能因你的网络位置和当前网络状况而有所不同
你可以根据需要修改
MIRRORS字典来添加或删除要测试的镜像源。 -
- User
我在 zsch 下能不用 python 吗
- Assistant
在
zsh下,你可以使用纯bash脚本来测试 PyPI 镜像的下载速度,而不需要 Python。以下是一个使用curl和time命令的脚本:
纯
bash脚本测试 PyPI 镜像速度#!/bin/zsh # 要测试的 PyPI 镜像列表 MIRRORS=( "https://pypi.org/simple" # 官方 "https://pypi.tuna.tsinghua.edu.cn/simple" # 清华 "https://mirrors.aliyun.com/pypi/simple" # 阿里云 "https://pypi.doubanio.com/simple" # 豆瓣 "https://pypi.hustunique.com/simple" # 华中科技大学 "https://pypi.mirrors.ustc.edu.cn/simple" # 中科大 "https://mirrors.cloud.tencent.com/pypi/simple" # 腾讯 "https://repo.huaweicloud.com/repository/pypi/simple" # 华为 "https://mirror.baidu.com/pypi/simple" # 百度 ) # 测试包(可修改) TEST_PACKAGE="numpy" # 存储测试结果 declare -A results # 测试每个镜像 for mirror in "${MIRRORS[@]}"; do echo "正在测试: $mirror" start_time=$(date +%s.%N) # 使用 curl 下载包的元数据(不实际下载文件) if curl -s -o /dev/null -I "$mirror/$TEST_PACKAGE/" >/dev/null 2>&1; then end_time=$(date +%s.%N) elapsed=$(echo "$end_time - $start_time" | bc) results["$mirror"]=$elapsed echo "✅ 成功: $mirror - $elapsed 秒" else results["$mirror"]="失败" echo "❌ 失败: $mirror" fi done # 打印结果 echo -e "\n=== 测试结果 ===" for mirror in "${!results[@]}"; do if [[ "${results[$mirror]}" == "失败" ]]; then echo "$mirror: 失败" else echo "$mirror: ${results[$mirror]} 秒" fi done # 找出最快的镜像 fastest_mirror="" fastest_time=999999 for mirror in "${!results[@]}"; do if [[ "${results[$mirror]}" != "失败" && "${results[$mirror]}" -lt "$fastest_time" ]]; then fastest_time="${results[$mirror]}" fastest_mirror="$mirror" fi done if [[ -n "$fastest_mirror" ]]; then echo -e "\n🏆 最快的镜像: $fastest_mirror (${fastest_time} 秒)" else echo -e "\n❌ 所有镜像测试失败!" fi
如何使用?
-
保存脚本:
nano test_pypi_mirrors.sh将上述代码粘贴进去,保存退出(
Ctrl+O→Enter→Ctrl+X)。 -
赋予执行权限:
chmod +x test_pypi_mirrors.sh -
运行脚本:
./test_pypi_mirrors.sh
脚本说明
curl -I:仅获取 HTTP 头部信息(不下载文件),测试镜像响应速度。time:记录每个镜像的响应时间(秒)。bc:用于浮点数计算(计算时间差)。declare -A:使用关联数组存储结果。
替代方案(更精确)
如果你想更精确地测试下载速度(而不仅仅是响应时间),可以使用
pip命令(但需要 Python):for mirror in "${MIRRORS[@]}"; do echo "测试 $mirror" time pip download $TEST_PACKAGE -i "$mirror" --no-deps >/dev/null 2>&1 done(但这样会实际下载文件,可能较慢。)
总结
- 纯
bash方案:快速测试镜像响应速度(不下载文件)。 pip方案:更精确但较慢(实际下载文件)。
你可以根据需求选择合适的方法! 🚀
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.