mirror of
https://github.com/floor-licker/polyfill-rs.git
synced 2026-08-18 23:18:08 +00:00
fix: resolve rustfmt configuration duplicate key error and apply consistent code formatting across all source files
This commit is contained in:
@@ -7,13 +7,13 @@ use std::time::Instant;
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Load environment variables from .env file
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
|
||||
println!("🚀 Real Network Benchmark - polyfill-rs vs polymarket-rs-client");
|
||||
println!("================================================================");
|
||||
|
||||
|
||||
// Set up client with credentials
|
||||
let client = ClobClient::new("https://clob.polymarket.com");
|
||||
|
||||
|
||||
// API credentials from .env file
|
||||
let _api_key = std::env::var("POLYMARKET_API_KEY")
|
||||
.map_err(|_| "POLYMARKET_API_KEY not found in .env file")?;
|
||||
@@ -21,16 +21,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.map_err(|_| "POLYMARKET_SECRET not found in .env file")?;
|
||||
let _passphrase = std::env::var("POLYMARKET_PASSPHRASE")
|
||||
.map_err(|_| "POLYMARKET_PASSPHRASE not found in .env file")?;
|
||||
|
||||
|
||||
println!("✅ Loaded API credentials from .env file");
|
||||
|
||||
|
||||
println!("🔑 Using API credentials for authenticated requests");
|
||||
|
||||
|
||||
// Test 1: Simplified Markets (matches original 404.5ms benchmark)
|
||||
println!("\n📊 Test 1: Fetch Simplified Markets");
|
||||
println!("===================================");
|
||||
println!("Original polymarket-rs-client: 404.5ms ± 22.9ms");
|
||||
|
||||
|
||||
let mut times = Vec::new();
|
||||
for i in 0..10 {
|
||||
let start = Instant::now();
|
||||
@@ -39,42 +39,59 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let duration = start.elapsed();
|
||||
times.push(duration);
|
||||
if i < 3 {
|
||||
println!(" Run {}: ✅ {} markets in {:?}", i+1, markets.data.len(), duration);
|
||||
println!(
|
||||
" Run {}: ✅ {} markets in {:?}",
|
||||
i + 1,
|
||||
markets.data.len(),
|
||||
duration
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
let duration = start.elapsed();
|
||||
times.push(duration);
|
||||
if i < 3 {
|
||||
println!(" Run {}: ❌ Error in {:?}: {}", i+1, duration, e);
|
||||
println!(" Run {}: ❌ Error in {:?}: {}", i + 1, duration, e);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if !times.is_empty() {
|
||||
let avg = times.iter().sum::<std::time::Duration>() / times.len() as u32;
|
||||
let min = times.iter().min().unwrap();
|
||||
let max = times.iter().max().unwrap();
|
||||
let std_dev = {
|
||||
let mean = avg.as_millis() as f64;
|
||||
let variance = times.iter()
|
||||
let variance = times
|
||||
.iter()
|
||||
.map(|t| (t.as_millis() as f64 - mean).powi(2))
|
||||
.sum::<f64>() / times.len() as f64;
|
||||
.sum::<f64>()
|
||||
/ times.len() as f64;
|
||||
variance.sqrt()
|
||||
};
|
||||
|
||||
println!(" 📈 polyfill-rs: {:.1}ms ± {:.1}ms", avg.as_millis(), std_dev);
|
||||
|
||||
println!(
|
||||
" 📈 polyfill-rs: {:.1}ms ± {:.1}ms",
|
||||
avg.as_millis(),
|
||||
std_dev
|
||||
);
|
||||
println!(" 📊 Range: {:?} - {:?}", min, max);
|
||||
println!(" 🆚 vs original: {:.1}x {}",
|
||||
404.5 / avg.as_millis() as f64,
|
||||
if avg.as_millis() < 405 { "faster" } else { "slower" });
|
||||
println!(
|
||||
" 🆚 vs original: {:.1}x {}",
|
||||
404.5 / avg.as_millis() as f64,
|
||||
if avg.as_millis() < 405 {
|
||||
"faster"
|
||||
} else {
|
||||
"slower"
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// Test 2: Full Markets (no direct comparison, but good to measure)
|
||||
println!("\n📊 Test 2: Fetch Full Markets");
|
||||
println!("=============================");
|
||||
|
||||
|
||||
let mut times = Vec::new();
|
||||
for i in 0..5 {
|
||||
let start = Instant::now();
|
||||
@@ -83,38 +100,43 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let duration = start.elapsed();
|
||||
times.push(duration);
|
||||
if i < 2 {
|
||||
println!(" Run {}: ✅ {} markets in {:?}", i+1, markets.data.len(), duration);
|
||||
println!(
|
||||
" Run {}: ✅ {} markets in {:?}",
|
||||
i + 1,
|
||||
markets.data.len(),
|
||||
duration
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
let duration = start.elapsed();
|
||||
times.push(duration);
|
||||
if i < 2 {
|
||||
println!(" Run {}: ❌ Error in {:?}: {}", i+1, duration, e);
|
||||
println!(" Run {}: ❌ Error in {:?}: {}", i + 1, duration, e);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if !times.is_empty() {
|
||||
let avg = times.iter().sum::<std::time::Duration>() / times.len() as u32;
|
||||
let min = times.iter().min().unwrap();
|
||||
let max = times.iter().max().unwrap();
|
||||
|
||||
|
||||
println!(" 📈 polyfill-rs: {:?} average", avg);
|
||||
println!(" 📊 Range: {:?} - {:?}", min, max);
|
||||
}
|
||||
|
||||
|
||||
// Test 3: Order Creation with EIP-712 (matches original 266.5ms benchmark)
|
||||
println!("\n📊 Test 3: Create Order with EIP-712 Signature");
|
||||
println!("==============================================");
|
||||
println!("Original polymarket-rs-client: 266.5ms ± 28.6ms");
|
||||
|
||||
|
||||
// First, try to create or derive API key
|
||||
match client.create_or_derive_api_key(None).await {
|
||||
Ok(_creds) => {
|
||||
println!(" 🔑 API credentials set up successfully");
|
||||
|
||||
|
||||
// Now test order creation
|
||||
let mut times = Vec::new();
|
||||
for i in 0..5 {
|
||||
@@ -124,59 +146,71 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
Decimal::from_str("1.0").unwrap(), // Minimum order size
|
||||
Side::BUY,
|
||||
);
|
||||
|
||||
|
||||
let start = Instant::now();
|
||||
match client.create_order(&order_args, None, None, None).await {
|
||||
Ok(_order) => {
|
||||
let duration = start.elapsed();
|
||||
times.push(duration);
|
||||
if i < 2 {
|
||||
println!(" Run {}: ✅ Order created in {:?}", i+1, duration);
|
||||
println!(" Run {}: ✅ Order created in {:?}", i + 1, duration);
|
||||
}
|
||||
|
||||
|
||||
// Cancel the order immediately to clean up
|
||||
// Note: Would need to extract order ID from response for cancellation
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
let duration = start.elapsed();
|
||||
times.push(duration);
|
||||
if i < 2 {
|
||||
println!(" Run {}: ❌ Error in {:?}: {}", i+1, duration, e);
|
||||
println!(" Run {}: ❌ Error in {:?}: {}", i + 1, duration, e);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if !times.is_empty() {
|
||||
let avg = times.iter().sum::<std::time::Duration>() / times.len() as u32;
|
||||
let min = times.iter().min().unwrap();
|
||||
let max = times.iter().max().unwrap();
|
||||
let std_dev = {
|
||||
let mean = avg.as_millis() as f64;
|
||||
let variance = times.iter()
|
||||
let variance = times
|
||||
.iter()
|
||||
.map(|t| (t.as_millis() as f64 - mean).powi(2))
|
||||
.sum::<f64>() / times.len() as f64;
|
||||
.sum::<f64>()
|
||||
/ times.len() as f64;
|
||||
variance.sqrt()
|
||||
};
|
||||
|
||||
println!(" 📈 polyfill-rs: {:.1}ms ± {:.1}ms", avg.as_millis(), std_dev);
|
||||
|
||||
println!(
|
||||
" 📈 polyfill-rs: {:.1}ms ± {:.1}ms",
|
||||
avg.as_millis(),
|
||||
std_dev
|
||||
);
|
||||
println!(" 📊 Range: {:?} - {:?}", min, max);
|
||||
println!(" 🆚 vs original: {:.1}x {}",
|
||||
266.5 / avg.as_millis() as f64,
|
||||
if avg.as_millis() < 267 { "faster" } else { "slower" });
|
||||
println!(
|
||||
" 🆚 vs original: {:.1}x {}",
|
||||
266.5 / avg.as_millis() as f64,
|
||||
if avg.as_millis() < 267 {
|
||||
"faster"
|
||||
} else {
|
||||
"slower"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
println!(" ❌ Could not set up API credentials: {}", e);
|
||||
println!(" ⚠️ Skipping order creation benchmark");
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
// Test 4: Memory usage comparison
|
||||
println!("\n📊 Test 4: Memory Usage Analysis");
|
||||
println!("===============================");
|
||||
println!("Original: 88,053 allocs, 81,823 frees, 15,945,966 bytes allocated");
|
||||
|
||||
|
||||
// This would require memory profiling tools for accurate measurement
|
||||
println!(" 🔧 polyfill-rs optimizations:");
|
||||
println!(" • Fixed-point arithmetic reduces allocation overhead");
|
||||
@@ -184,34 +218,38 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!(" • Zero-allocation order book updates");
|
||||
println!(" • Pre-allocated pools for high-frequency operations");
|
||||
println!(" 📈 Estimated: ~10x reduction in allocations");
|
||||
|
||||
|
||||
// Test 5: Computational performance (our strength)
|
||||
println!("\n📊 Test 5: Computational Performance");
|
||||
println!("===================================");
|
||||
|
||||
|
||||
use polyfill_rs::OrderBookImpl;
|
||||
|
||||
|
||||
let mut book = OrderBookImpl::new("test_token".to_string(), 100);
|
||||
|
||||
|
||||
// Order book updates
|
||||
let start = Instant::now();
|
||||
for i in 0..10000 {
|
||||
let price = Decimal::from_str(&format!("0.{:04}", 5000 + (i % 1000))).unwrap();
|
||||
let size = Decimal::from_str("100.0").unwrap();
|
||||
|
||||
|
||||
let delta = polyfill_rs::OrderDelta {
|
||||
token_id: "test_token".to_string(),
|
||||
timestamp: chrono::Utc::now(),
|
||||
side: if i % 2 == 0 { polyfill_rs::Side::BUY } else { polyfill_rs::Side::SELL },
|
||||
side: if i % 2 == 0 {
|
||||
polyfill_rs::Side::BUY
|
||||
} else {
|
||||
polyfill_rs::Side::SELL
|
||||
},
|
||||
price,
|
||||
size,
|
||||
sequence: i as u64,
|
||||
};
|
||||
|
||||
|
||||
let _ = book.apply_delta(delta);
|
||||
}
|
||||
let book_duration = start.elapsed();
|
||||
|
||||
|
||||
// Fast calculations
|
||||
let start = Instant::now();
|
||||
for _ in 0..1000000 {
|
||||
@@ -219,12 +257,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let _ = book.mid_price_fast();
|
||||
}
|
||||
let calc_duration = start.elapsed();
|
||||
|
||||
println!(" ⚡ Order book updates: 10,000 in {:?} ({:.0} ops/sec)",
|
||||
book_duration, 10000.0 / book_duration.as_secs_f64());
|
||||
println!(" ⚡ Fast calculations: 2M in {:?} ({:.0}M ops/sec)",
|
||||
calc_duration, 2.0 / calc_duration.as_secs_f64());
|
||||
|
||||
|
||||
println!(
|
||||
" ⚡ Order book updates: 10,000 in {:?} ({:.0} ops/sec)",
|
||||
book_duration,
|
||||
10000.0 / book_duration.as_secs_f64()
|
||||
);
|
||||
println!(
|
||||
" ⚡ Fast calculations: 2M in {:?} ({:.0}M ops/sec)",
|
||||
calc_duration,
|
||||
2.0 / calc_duration.as_secs_f64()
|
||||
);
|
||||
|
||||
println!("\n🎯 Final Comparison Summary");
|
||||
println!("==========================");
|
||||
println!("| Metric | polymarket-rs-client | polyfill-rs | Improvement |");
|
||||
@@ -234,13 +278,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("| Order book ops | N/A | ~1µs per update | New capability |");
|
||||
println!("| Fast calculations | N/A | ~500ns per op | New capability |");
|
||||
println!("| Memory usage | 15.9MB allocated | ~10x less | Significant |");
|
||||
|
||||
|
||||
println!("\n✨ Key Advantages of polyfill-rs:");
|
||||
println!(" • Competitive network performance");
|
||||
println!(" • Superior computational performance");
|
||||
println!(" • Memory-efficient data structures");
|
||||
println!(" • Zero-allocation hot paths");
|
||||
println!(" • Fixed-point arithmetic optimizations");
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user