kegani 0.1.1

A developer-friendly, ergonomic, production-ready Rust web framework
Documentation
//! HTTP and performance tests for Kegani Framework
//!
//! Run with: cargo test --test performance_tests

use std::time::{Duration, Instant};

#[cfg(test)]
mod http_tests {
    use super::*;

    #[test]
    fn test_json_serialization_speed() {
        let data = serde_json::json!({
            "id": 1,
            "name": "Test",
            "items": vec!["a", "b", "c"],
            "nested": {"key": "value"}
        });

        let start = Instant::now();
        for _ in 0..10000 {
            let _ = serde_json::to_vec(&data).unwrap();
        }
        let elapsed = start.elapsed();
        assert!(elapsed < Duration::from_millis(100), "JSON serialization too slow: {:?}", elapsed);
    }

    #[test]
    fn test_json_deserialization_speed() {
        let json_str = r#"{"id":12345,"name":"Test User","email":"test@example.com","active":true}"#;

        #[derive(serde::Deserialize)]
        struct TestData {
            id: u64,
            name: String,
            email: String,
            active: bool,
        }

        let start = Instant::now();
        for _ in 0..10000 {
            let _ = serde_json::from_str::<TestData>(json_str).unwrap();
        }
        let elapsed = start.elapsed();
        assert!(elapsed < Duration::from_millis(100), "JSON deserialization too slow: {:?}", elapsed);
    }

    #[test]
    fn test_path_parsing() {
        let paths = vec!["/api/users", "/api/users/123", "/api/posts/456", "/health"];

        let start = Instant::now();
        for _ in 0..10000 {
            for path in &paths {
                let _ = path.starts_with("/api/");
                let _ = path.contains('/');
            }
        }
        let elapsed = start.elapsed();
        assert!(elapsed < Duration::from_millis(20), "Path parsing too slow: {:?}", elapsed);
    }
}

#[cfg(test)]
mod cache_tests {
    use super::*;

    #[test]
    fn test_cache_set_performance() {
        let mut cache = std::collections::HashMap::new();

        let start = Instant::now();
        for i in 0..10000 {
            cache.insert(format!("key{}", i), format!("value{}", i));
        }
        let elapsed = start.elapsed();
        assert!(elapsed < Duration::from_millis(50), "Cache set too slow: {:?}", elapsed);
    }

    #[test]
    fn test_cache_get_performance() {
        let mut cache = std::collections::HashMap::new();
        for i in 0..1000 {
            cache.insert(format!("key{}", i), format!("value{}", i));
        }

        let start = Instant::now();
        for _ in 0..10000 {
            let _ = cache.get("key500");
        }
        let elapsed = start.elapsed();
        assert!(elapsed < Duration::from_millis(10), "Cache get too slow: {:?}", elapsed);
    }
}

#[cfg(test)]
mod string_tests {
    use super::*;

    #[test]
    fn test_string_concat() {
        let start = Instant::now();
        for _ in 0..10000 {
            let mut s = String::new();
            for i in 0..10 {
                s.push_str("item");
                s.push_str(&i.to_string());
            }
            let _ = s;
        }
        let elapsed = start.elapsed();
        assert!(elapsed < Duration::from_millis(100), "String concat too slow: {:?}", elapsed);
    }

    #[test]
    fn test_regex_match() {
        let re = regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
        let valid_date = "2024-01-15";

        let start = Instant::now();
        for _ in 0..100000 {
            let _ = re.is_match(valid_date);
        }
        let elapsed = start.elapsed();
        assert!(elapsed < Duration::from_millis(100), "Regex match too slow: {:?}", elapsed);
    }
}

#[cfg(test)]
mod uuid_tests {
    use super::*;

    #[test]
    fn test_uuid_generation() {
        let start = Instant::now();
        for _ in 0..10000 {
            let _ = uuid::Uuid::new_v4().to_string();
        }
        let elapsed = start.elapsed();
        assert!(elapsed < Duration::from_millis(100), "UUID generation too slow: {:?}", elapsed);
    }

    #[test]
    fn test_uuid_parsing() {
        let uuid_str = "550e8400-e29b-41d4-a716-446655440000";

        let start = Instant::now();
        for _ in 0..10000 {
            let _ = uuid::Uuid::parse_str(uuid_str);
        }
        let elapsed = start.elapsed();
        assert!(elapsed < Duration::from_millis(50), "UUID parsing too slow: {:?}", elapsed);
    }
}

#[cfg(test)]
mod hashmap_tests {
    use super::*;

    #[test]
    fn test_hashmap_insert() {
        let start = Instant::now();
        for _ in 0..1000 {
            let mut map = std::collections::HashMap::new();
            for i in 0..100 {
                map.insert(i, format!("value{}", i));
            }
        }
        let elapsed = start.elapsed();
        assert!(elapsed < Duration::from_millis(200), "HashMap insert too slow: {:?}", elapsed);
    }

    #[test]
    fn test_hashmap_lookup() {
        let mut map = std::collections::HashMap::new();
        for i in 0..10000 {
            map.insert(i, format!("value{}", i));
        }

        let start = Instant::now();
        for _ in 0..100000 {
            let _ = map.get(&5000);
        }
        let elapsed = start.elapsed();
        assert!(elapsed < Duration::from_millis(50), "HashMap lookup too slow: {:?}", elapsed);
    }
}

#[cfg(test)]
mod vector_tests {
    use super::*;

    #[test]
    fn test_vec_push() {
        let start = Instant::now();
        for _ in 0..1000 {
            let mut v = Vec::new();
            for i in 0..1000 {
                v.push(i);
            }
        }
        let elapsed = start.elapsed();
        assert!(elapsed < Duration::from_millis(200), "Vec push too slow: {:?}", elapsed);
    }

    #[test]
    fn test_vec_iteration() {
        let v: Vec<i32> = (0..10000).collect();

        let start = Instant::now();
        for _ in 0..1000 {
            let mut sum = 0i64;
            for i in &v {
                sum += *i as i64;
            }
            let _ = sum;
        }
        let elapsed = start.elapsed();
        assert!(elapsed < Duration::from_millis(100), "Vec iteration too slow: {:?}", elapsed);
    }
}