use grpc_graphql_gateway::gbp::GbpEncoder;
use serde_json::json;
fn main() {
println!("\n╔══════════════════════════════════════════════════════════╗");
println!("║ GraphQL Binary Protocol - Compression Analysis ║");
println!("╚══════════════════════════════════════════════════════════╝\n");
println!("📊 Scenario 1: Single User Query");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
let single_user = json!({
"data": {
"user": {
"id": "123",
"name": "Alice Johnson",
"email": "alice@example.com",
"role": "ADMIN",
"createdAt": "2024-01-15T10:30:00Z"
}
}
});
analyze_compression("Single User", &single_user);
println!("\n📊 Scenario 2: List of 10 Users");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
let users_10 = json!({
"data": {
"users": (0..10).map(|i| json!({
"id": format!("user-{}", i),
"name": format!("User {}", i),
"email": format!("user{}@example.com", i),
"role": "MEMBER",
"status": "ACTIVE",
"organization": {
"id": "org-1",
"name": "Acme Corp",
"tier": "ENTERPRISE"
},
"createdAt": "2024-01-15T10:30:00Z"
})).collect::<Vec<_>>()
}
});
analyze_compression("10 Users", &users_10);
println!("\n📊 Scenario 3: List of 100 Users");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
let users_100 = json!({
"data": {
"users": (0..100).map(|i| json!({
"id": format!("user-{}", i),
"name": format!("User {}", i),
"email": format!("user{}@example.com", i),
"role": if i % 10 == 0 { "ADMIN" } else { "MEMBER" },
"status": "ACTIVE",
"organization": {
"id": "org-1",
"name": "Acme Corp",
"tier": "ENTERPRISE",
"settings": {
"theme": "dark",
"notifications": true,
"language": "en"
}
},
"permissions": ["READ", "WRITE", "EXECUTE"],
"createdAt": "2024-01-15T10:30:00Z",
"updatedAt": "2024-01-20T14:45:00Z"
})).collect::<Vec<_>>()
}
});
analyze_compression("100 Users", &users_100);
println!("\n📊 Scenario 4: List of 1000 Users (Production Scale)");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
let users_1000 = json!({
"data": {
"users": (0..1000).map(|i| json!({
"id": format!("user-{}", i),
"name": format!("User {}", i),
"email": format!("user{}@example.com", i),
"role": if i % 10 == 0 { "ADMIN" } else { "MEMBER" },
"status": "ACTIVE",
"organization": {
"id": "org-1",
"name": "Acme Corp",
"tier": "ENTERPRISE",
"settings": {
"theme": "dark",
"notifications": true,
"language": "en"
}
},
"permissions": ["READ", "WRITE", "EXECUTE"],
"profile": {
"avatar": format!("https://cdn.example.com/avatars/{}.jpg", i),
"bio": "Software engineer passionate about GraphQL and performance",
"location": "San Francisco, CA"
},
"createdAt": "2024-01-15T10:30:00Z",
"updatedAt": "2024-01-20T14:45:00Z"
})).collect::<Vec<_>>()
}
});
analyze_compression("1000 Users", &users_1000);
println!("\n📊 Scenario 5: List of 5000 Users (Large Scale)");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
let users_5000 = json!({
"data": {
"users": (0..5000).map(|i| json!({
"id": format!("user-{}", i),
"name": format!("User {}", i),
"email": format!("user{}@example.com", i),
"role": if i % 10 == 0 { "ADMIN" } else { "MEMBER" },
"status": "ACTIVE",
"organization": {
"id": "org-1",
"name": "Acme Corp",
"tier": "ENTERPRISE",
"settings": {
"theme": "dark",
"notifications": true,
"language": "en"
}
},
"permissions": ["READ", "WRITE", "EXECUTE"],
"profile": {
"avatar": format!("https://cdn.example.com/avatars/{}.jpg", i),
"bio": "Software engineer passionate about GraphQL and performance",
"location": "San Francisco, CA",
"company": "Tech Corp",
"title": "Senior Engineer"
},
"metadata": {
"lastLogin": "2024-01-20T14:45:00Z",
"loginCount": 1523,
"apiKey": format!("key_{}_abcdef123456", i)
},
"createdAt": "2024-01-15T10:30:00Z",
"updatedAt": "2024-01-20T14:45:00Z"
})).collect::<Vec<_>>()
}
});
analyze_compression("5000 Users", &users_5000);
println!("\n📊 Scenario 6: List of 10000 Users (Very Large Scale)");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
let users_10000 = json!({
"data": {
"users": (0..10000).map(|i| json!({
"id": format!("user-{}", i),
"name": format!("User {}", i),
"email": format!("user{}@example.com", i),
"role": if i % 10 == 0 { "ADMIN" } else { "MEMBER" },
"status": "ACTIVE",
"organization": {
"id": "org-1",
"name": "Acme Corp",
"tier": "ENTERPRISE",
"settings": {
"theme": "dark",
"notifications": true,
"language": "en"
}
},
"permissions": ["READ", "WRITE", "EXECUTE"],
"profile": {
"avatar": format!("https://cdn.example.com/avatars/{}.jpg", i),
"bio": "Software engineer passionate about GraphQL and performance optimization",
"location": "San Francisco, CA",
"company": "Tech Corp",
"title": "Senior Engineer"
},
"metadata": {
"lastLogin": "2024-01-20T14:45:00Z",
"loginCount": 1523,
"apiKey": format!("key_{}_abcdef123456", i),
"sessionToken": format!("sess_{}_xyz789", i)
},
"createdAt": "2024-01-15T10:30:00Z",
"updatedAt": "2024-01-20T14:45:00Z"
})).collect::<Vec<_>>()
}
});
analyze_compression("10000 Users", &users_10000);
println!("\n📊 Scenario 7: List of 50000 Users with Random Enums (EXTREME SCALE)");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
let roles = ["ADMIN", "MEMBER", "MODERATOR", "GUEST", "DEVELOPER"];
let statuses = ["ACTIVE", "INACTIVE", "PENDING", "SUSPENDED", "VERIFIED"];
let tiers = ["FREE", "BASIC", "PRO", "ENTERPRISE", "ULTIMATE"];
let themes = ["light", "dark", "auto", "midnight", "ocean"];
let languages = ["en", "es", "fr", "de", "ja", "zh", "pt", "ru"];
let locations = [
"San Francisco, CA",
"New York, NY",
"London, UK",
"Tokyo, Japan",
"Berlin, Germany",
];
let users_50000 = json!({
"data": {
"users": (0..50000).map(|i| {
let role_idx = i % roles.len();
let status_idx = (i * 3) % statuses.len();
let tier_idx = (i * 7) % tiers.len();
let theme_idx = (i * 11) % themes.len();
let lang_idx = (i * 13) % languages.len();
let loc_idx = (i * 17) % locations.len();
json!({
"id": format!("user-{}", i),
"name": format!("User {}", i),
"email": format!("user{}@example.com", i),
"role": roles[role_idx],
"status": statuses[status_idx],
"organization": {
"id": format!("org-{}", i / 100),
"name": "Acme Corp",
"tier": tiers[tier_idx],
"settings": {
"theme": themes[theme_idx],
"notifications": i % 2 == 0,
"language": languages[lang_idx]
}
},
"permissions": ["READ", "WRITE", "EXECUTE"],
"profile": {
"avatar": format!("https://cdn.example.com/avatars/{}.jpg", i),
"bio": "Software engineer passionate about GraphQL and performance optimization",
"location": locations[loc_idx],
"company": "Tech Corp",
"title": if i % 10 == 0 { "Senior Engineer" } else { "Engineer" },
"verified": i % 3 == 0
},
"metadata": {
"lastLogin": "2024-01-20T14:45:00Z",
"loginCount": 1000 + (i % 5000),
"apiKey": format!("key_{}_abcdef123456", i),
"sessionToken": format!("sess_{}_xyz789", i),
"deviceType": if i % 3 == 0 { "mobile" } else if i % 3 == 1 { "desktop" } else { "tablet" }
},
"createdAt": "2024-01-15T10:30:00Z",
"updatedAt": "2024-01-20T14:45:00Z"
})
}).collect::<Vec<_>>()
}
});
analyze_compression("50000 Users", &users_50000);
println!("\n📊 Scenario 8: 50000 Users - Medium Repetition (Shared Orgs)");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
let users_50k_medium = json!({
"data": {
"users": (0..50000).map(|i| json!({
"id": format!("user-{}", i),
"name": format!("User {}", i),
"email": format!("user{}@example.com", i),
"role": "MEMBER", "status": "ACTIVE", "organization": {
"id": "org-1", "name": "Acme Corp", "tier": "ENTERPRISE", "settings": {
"theme": "dark", "notifications": true, "language": "en" }
},
"permissions": ["READ", "WRITE", "EXECUTE"], "profile": {
"avatar": format!("https://cdn.example.com/avatars/{}.jpg", i),
"bio": "Software engineer passionate about GraphQL and performance optimization", "location": "San Francisco, CA", "company": "Tech Corp", "title": "Engineer", "verified": true },
"metadata": {
"lastLogin": "2024-01-20T14:45:00Z", "loginCount": 1500, "apiKey": format!("key_{}_abcdef123456", i),
"sessionToken": format!("sess_{}_xyz789", i),
"deviceType": "desktop" },
"createdAt": "2024-01-15T10:30:00Z", "updatedAt": "2024-01-20T14:45:00Z" })).collect::<Vec<_>>()
}
});
analyze_compression("50000 Users (Medium Repetition)", &users_50k_medium);
println!("\n📊 Scenario 8b: 50000 Products - Realistic Mid-Case (85-90% Zone)");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
let categories = ["Electronics", "Clothing", "Books", "Home", "Sports"];
let brands = ["BrandA", "BrandB", "BrandC", "BrandD", "BrandE"];
let statuses = ["IN_STOCK", "LOW_STOCK", "OUT_OF_STOCK"];
let conditions = ["NEW", "REFURBISHED", "USED"];
let products_50k = json!({
"data": {
"products": (0..50000).map(|i| {
let cat_idx = i % categories.len();
let brand_idx = (i / 100) % brands.len();
let status_idx = (i / 10) % statuses.len();
let condition_idx = i % conditions.len();
json!({
"id": i, "sku": format!("SKU-{}", i), "name": format!("Product {}", i),
"category": categories[cat_idx],
"brand": brands[brand_idx],
"price": 99.99, "currency": "USD",
"status": statuses[status_idx],
"condition": conditions[condition_idx],
"inventory": {
"warehouse": "WH-01", "location": "A-1", "quantity": 100, "reserved": 10 },
"shipping": {
"weight": 1.5, "dimensions": {
"length": 10.0,
"width": 8.0,
"height": 2.0
},
"freeShipping": true
},
"ratings": {
"average": 4.5, "count": 1234, "distribution": {
"5star": 60,
"4star": 25,
"3star": 10,
"2star": 3,
"1star": 2
}
},
"tags": ["featured", "bestseller", "new"], "metadata": {
"createdAt": "2024-01-15T10:30:00Z",
"updatedAt": "2024-01-20T14:45:00Z",
"createdBy": "admin",
"version": 1
}
})
}).collect::<Vec<_>>()
}
});
analyze_compression("50000 Products (Mid-Case)", &products_50k);
println!("\n📊 Scenario 9: 50000 Users - Extreme Repetition (97%+ Zone)");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
let users_50k_extreme = json!({
"data": {
"users": (0..50000).map(|i| json!({
"id": i, "name": "User", "email": "user@example.com", "role": "MEMBER",
"status": "ACTIVE",
"organization": {
"id": "org-1",
"name": "Acme Corp",
"tier": "ENTERPRISE",
"settings": {
"theme": "dark",
"notifications": true,
"language": "en"
}
},
"permissions": ["READ", "WRITE", "EXECUTE"],
"profile": {
"avatar": "https://cdn.example.com/default.jpg", "bio": "User profile", "location": "San Francisco, CA",
"company": "Tech Corp",
"title": "Engineer",
"verified": true
},
"metadata": {
"lastLogin": "2024-01-20T14:45:00Z",
"loginCount": 1500,
"apiKey": "key_default", "sessionToken": "sess_default", "deviceType": "desktop"
},
"createdAt": "2024-01-15T10:30:00Z",
"updatedAt": "2024-01-20T14:45:00Z"
})).collect::<Vec<_>>()
}
});
analyze_compression("50000 Users (Extreme Repetition)", &users_50k_extreme);
println!("\n╔══════════════════════════════════════════════════════════╗");
println!("║ 💡 Key Insights ║");
println!("╠══════════════════════════════════════════════════════════╣");
println!("║ • Small payloads (<1KB): 10-20% savings ║");
println!("║ • Medium payloads (1-10KB): 30-50% savings ║");
println!("║ • Large payloads (10KB+): 70-99% savings ║");
println!("║ ║");
println!("║ Why GBP is so effective: ║");
println!("║ 1. Field names stored once in string pool ║");
println!("║ 2. Object shapes cached and reused ║");
println!("║ 3. Columnar encoding for arrays ║");
println!("║ 4. Run-length encoding for repeated values ║");
println!("╚══════════════════════════════════════════════════════════╝\n");
}
fn analyze_compression(label: &str, data: &serde_json::Value) {
let json_bytes = serde_json::to_vec(data).unwrap();
let json_size = json_bytes.len();
let mut encoder = GbpEncoder::new();
let gbp_bytes = encoder.encode(data);
let gbp_size = gbp_bytes.len();
let saved_bytes = json_size as i64 - gbp_size as i64;
let saved_percent = (saved_bytes as f64 / json_size as f64) * 100.0;
let ratio = json_size as f64 / gbp_size as f64;
println!("Query: {}", label);
println!(" JSON: {:>10}", format_bytes(json_size));
println!(" Binary (GBP): {:>10}", format_bytes(gbp_size));
println!(" ─────────────────────────────");
if saved_bytes >= 0 {
println!(
" Saved: {:>10} ({:.1}%)",
format_bytes(saved_bytes as usize),
saved_percent
);
} else {
println!(
" Overhead: {:>10} ({:.1}%)",
format_bytes((-saved_bytes) as usize),
-saved_percent
);
}
println!(" Ratio: {:.2}x", ratio);
let monthly_requests = 1_000_000;
let monthly_json = json_size * monthly_requests;
let monthly_gbp = gbp_size * monthly_requests;
let monthly_saved = monthly_json as i64 - monthly_gbp as i64;
println!("\n 📈 At 1M requests/month:");
println!(" JSON: {}", format_bytes(monthly_json));
println!(" Binary: {}", format_bytes(monthly_gbp));
if monthly_saved >= 0 {
println!(
" 💰 Saved: {} ({:.1}%)",
format_bytes(monthly_saved as usize),
saved_percent
);
} else {
println!(
" ⚠️ Cost: {} extra ({:.1}%)",
format_bytes((-monthly_saved) as usize),
-saved_percent
);
}
}
fn format_bytes(bytes: usize) -> String {
if bytes < 1024 {
format!("{} B", bytes)
} else if bytes < 1024 * 1024 {
format!("{:.2} KB", bytes as f64 / 1024.0)
} else if bytes < 1024 * 1024 * 1024 {
format!("{:.2} MB", bytes as f64 / (1024.0 * 1024.0))
} else {
format!("{:.2} GB", bytes as f64 / (1024.0 * 1024.0 * 1024.0))
}
}