Skip to content

Commit

Permalink
feat: test mirrors
Browse files Browse the repository at this point in the history
  • Loading branch information
Loonphy authored and Loonphy committed Sep 25, 2024
0 parents commit 0e00629
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/test-mirrors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Test Registry Mirrors

on:
workflow_dispatch:
schedule:
- cron: '0 0 * * 0'
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test-mirrors:
runs-on: self-hosted
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.11'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pandas
- name: Run mirror tests
run: |
python scripts/test_mirrors.py
- name: Commit and push changes
if: github.event_name != 'pull_request'
run: |
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
git add README.md
git add history/
git diff --quiet && git diff --staged --quiet || (git commit -m "Update mirror test results" && git push)
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
| Name | URL | Pull Time (s) | Speed (MB/s) | Status | Last Tested |
|------|-----|---------------|--------------|--------|-------------|
| 1Panel 面板 | https://registry-1.docker.io | | | | |
| 耗子面板 | https://hub.rat.dev | | | | |
| DaoCloud| https://docker.m.daocloud.io | | | | |
25 changes: 25 additions & 0 deletions scripts/pull_image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

MIRROR_URL=$1
IMAGE="nginx:latest" # 使用 nginx 作为测试镜像

# 配置 Docker 使用指定的 mirror
echo "{\"registry-mirrors\": [\"$MIRROR_URL\"]}" | sudo tee /etc/docker/daemon.json
sudo systemctl restart docker

# 删除已存在的镜像(如果有)
docker rmi $IMAGE >/dev/null 2>&1

# 拉取镜像并计时
start_time=$(date +%s.%N)
docker pull $IMAGE >/dev/null 2>&1
end_time=$(date +%s.%N)

# 计算耗时
duration=$(echo "$end_time - $start_time" | bc)

# 输出耗时(秒)
echo $duration

# 清理
docker rmi $IMAGE >/dev/null 2>&1
79 changes: 79 additions & 0 deletions scripts/test_mirrors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import pandas as pd
import subprocess
import re
from datetime import datetime
import time
import os

IMAGE_SIZE_MB = 133
MAX_RETRIES = 3
RETRY_DELAY = 30
HISTORY_DIR = 'history'

def ensure_history_dir():
if not os.path.exists(HISTORY_DIR):
os.makedirs(HISTORY_DIR)

def extract_table_from_readme():
# ... [保持不变] ...

def update_readme_with_results(df):
with open('README.md', 'r') as file:
content = file.read()

table = df.to_markdown(index=False)
updated_content = re.sub(r'\|.*?\|\n\|[-:|\s]+\|\n((?:\|.*?\|\n)*)', table + '\n', content, flags=re.DOTALL)

# 添加最后更新时间
last_updated = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
updated_content += f"\n\nLast updated: {last_updated}\n"

with open('README.md', 'w') as file:
file.write(updated_content)

def test_mirror(url):
# ... [保持不变] ...

def calculate_speed(pull_time):
# ... [保持不变] ...

def format_pull_time(seconds):
# ... [保持不变] ...

def format_speed(speed):
# ... [保持不变] ...

def save_daily_result(df):
ensure_history_dir()
date = datetime.now().strftime("%Y-%m-%d")
filename = f"{HISTORY_DIR}/result_{date}.csv"
df.to_csv(filename, index=False)
print(f"Daily result saved to {filename}")

def main():
df = extract_table_from_readme()
if df is None:
print("Table not found in README.md")
return

results = df['URL'].apply(test_mirror)
df['Pull Time (s)'] = results.apply(lambda x: x[0])
df['Status'] = results.apply(lambda x: x[1])
df['Speed (MB/s)'] = df['Pull Time (s)'].apply(calculate_speed)
df['Last Tested'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

# 按速度降序排序,将失败的放在最后
df = df.sort_values('Speed (MB/s)', ascending=False)

# 格式化结果
df['Pull Time (s)'] = df['Pull Time (s)'].apply(format_pull_time)
df['Speed (MB/s)'] = df['Speed (MB/s)'].apply(format_speed)

# 保存每日结果
save_daily_result(df)

# 更新 README
update_readme_with_results(df)

if __name__ == "__main__":
main()

0 comments on commit 0e00629

Please sign in to comment.