rust_nrm/utils/
speed_test.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use colored::Colorize;
use reqwest;
use std::time::Instant;
use super::Logger;

#[derive(Debug)]
pub struct SpeedTestResult {
    pub name: String,
    pub url: String,
    pub response_time: f64,
    pub is_success: bool,
}

pub struct SpeedTester {
    client: reqwest::Client,
}

impl SpeedTester {
    pub fn new() -> Self {
        SpeedTester {
            client: reqwest::Client::builder()
                .timeout(std::time::Duration::from_secs(5))  // 5 seconds timeout
                .build()
                .unwrap_or_else(|_| reqwest::Client::new()),
        }
    }

    pub async fn test_registry(&self, name: &str, url: &str) -> SpeedTestResult {
        let start = Instant::now();
        let result = self.client.get(url).send().await;
        let elapsed = start.elapsed().as_secs_f64();

        SpeedTestResult {
            name: name.to_string(),
            url: url.to_string(),
            response_time: elapsed,
            is_success: result.is_ok(),
        }
    }

    fn format_time(seconds: f64) -> String {
        format!("{:.0}ms", seconds * 1000.0)
    }

    pub async fn test_all(&self, registries: &[(String, String)]) -> Vec<SpeedTestResult> {
        let mut results = Vec::new();

        Logger::info("Testing registry speeds...");

        for (name, url) in registries {
            let result = self.test_registry(name, url).await;
            results.push(result);
        }

        // Sort by response time
        results.sort_by(|a, b| a.response_time.partial_cmp(&b.response_time).unwrap());

        // Print results
        Logger::list("Registry Speed Test Results:");
        
        for (index, result) in results.iter().enumerate() {
            let status = if result.is_success {
                "✓".green()
            } else {
                "✗".red()
            };

            let time_str = Self::format_time(result.response_time);
            let time_display = match index {
                0 => time_str.green().bold(),   // Fastest
                1 => time_str.yellow(),         // Second
                2 => time_str.blue(),           // Third
                _ => time_str.normal(),
            };

            println!(
                "{} {} {} {} {}",
                format!("[{}]", index + 1).bold(),
                status,
                result.name.bold(),
                "->".dimmed(),
                time_display
            );
        }

        if let Some(fastest) = results.first() {
            if fastest.is_success {
                Logger::success(&format!(
                    "Fastest registry is {} ({})",
                    fastest.name.bold(),
                    Self::format_time(fastest.response_time)
                ));
            }
        }

        results
    }
}