mirror of
https://github.com/floor-licker/polyfill-rs.git
synced 2026-08-01 14:57:45 +00:00
193 lines
7.0 KiB
Rust
193 lines
7.0 KiB
Rust
use polyfill_rs::{ClobClient, OrderArgs, Side};
|
|
use rust_decimal::Decimal;
|
|
use std::str::FromStr;
|
|
use std::time::Instant;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
// Load environment variables from .env file
|
|
dotenv::dotenv().ok();
|
|
|
|
println!("🔐 Authenticated Network Benchmark - Real Order Creation");
|
|
println!("=======================================================");
|
|
|
|
// API credentials from .env file
|
|
let _api_key = std::env::var("POLYMARKET_API_KEY")
|
|
.map_err(|_| "POLYMARKET_API_KEY not found in .env file")?;
|
|
let _secret = std::env::var("POLYMARKET_SECRET")
|
|
.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");
|
|
|
|
let client = ClobClient::new_internet("https://clob.polymarket.com");
|
|
|
|
println!("🔑 Setting up API credentials...");
|
|
|
|
// Test 1: API Key Creation/Derivation (part of the 266.5ms benchmark)
|
|
println!("\n📊 Test 1: API Key Setup");
|
|
println!("========================");
|
|
|
|
let mut setup_times = Vec::new();
|
|
for i in 0..3 {
|
|
let start = Instant::now();
|
|
match client.create_or_derive_api_key(None).await {
|
|
Ok(_creds) => {
|
|
let duration = start.elapsed();
|
|
setup_times.push(duration);
|
|
println!(" Run {}: ✅ API key setup in {:?}", i + 1, duration);
|
|
|
|
// Set the credentials for order creation
|
|
// Note: We'd need to properly set up the client with these creds
|
|
break;
|
|
},
|
|
Err(e) => {
|
|
let duration = start.elapsed();
|
|
setup_times.push(duration);
|
|
println!(" Run {}: ❌ Error in {:?}: {}", i + 1, duration, e);
|
|
},
|
|
}
|
|
}
|
|
|
|
if !setup_times.is_empty() {
|
|
let avg = setup_times.iter().sum::<std::time::Duration>() / setup_times.len() as u32;
|
|
println!(" 📈 API setup average: {:?}", avg);
|
|
}
|
|
|
|
// Test 2: Order Creation with EIP-712 (the real 266.5ms test)
|
|
println!("\n📊 Test 2: Order Creation + EIP-712 Signing");
|
|
println!("===========================================");
|
|
println!("Target: polymarket-rs-client 266.5ms ± 28.6ms");
|
|
|
|
// We need a real token ID for a valid order
|
|
let token_id = "21742633143463906290569050155826241533067272736897614950488156847949938836455";
|
|
|
|
let mut order_times = Vec::new();
|
|
for i in 0..5 {
|
|
let order_args = OrderArgs::new(
|
|
token_id,
|
|
Decimal::from_str("0.01").unwrap(), // Very low price to avoid execution
|
|
Decimal::from_str("1.0").unwrap(), // Minimum size
|
|
Side::BUY,
|
|
);
|
|
|
|
let start = Instant::now();
|
|
match client.create_order(&order_args, None, None, None).await {
|
|
Ok(order) => {
|
|
let duration = start.elapsed();
|
|
order_times.push(duration);
|
|
println!(" Run {}: ✅ Order created in {:?}", i + 1, duration);
|
|
|
|
// Immediately cancel to clean up
|
|
// Note: We'd need the proper cancel method here
|
|
println!(
|
|
" 📝 Order ID: {} (would cancel immediately)",
|
|
format!("{:?}", order).chars().take(50).collect::<String>()
|
|
);
|
|
},
|
|
Err(e) => {
|
|
let duration = start.elapsed();
|
|
order_times.push(duration);
|
|
println!(" Run {}: ❌ Error in {:?}: {}", i + 1, duration, e);
|
|
|
|
// Even errors give us timing info about how far we got
|
|
if duration.as_millis() > 50 {
|
|
println!(" 💡 Error occurred after network round-trip, timing still valid");
|
|
}
|
|
},
|
|
}
|
|
}
|
|
|
|
if !order_times.is_empty() {
|
|
let avg = order_times.iter().sum::<std::time::Duration>() / order_times.len() as u32;
|
|
let min = order_times.iter().min().unwrap();
|
|
let max = order_times.iter().max().unwrap();
|
|
let std_dev = {
|
|
let mean = avg.as_millis() as f64;
|
|
let variance = order_times
|
|
.iter()
|
|
.map(|t| (t.as_millis() as f64 - mean).powi(2))
|
|
.sum::<f64>()
|
|
/ order_times.len() as f64;
|
|
variance.sqrt()
|
|
};
|
|
|
|
println!("\n 📊 Order Creation Results:");
|
|
println!(
|
|
" 📈 polyfill-rs: {:.1}ms ± {:.1}ms",
|
|
avg.as_millis(),
|
|
std_dev
|
|
);
|
|
println!(" 📊 Range: {:?} - {:?}", min, max);
|
|
println!(
|
|
" 🆚 vs original (266.5ms): {:.1}x {}",
|
|
266.5 / avg.as_millis() as f64,
|
|
if avg.as_millis() < 267 {
|
|
"faster"
|
|
} else {
|
|
"slower"
|
|
}
|
|
);
|
|
}
|
|
|
|
// Test 3: Compare with Market Data (for context)
|
|
println!("\n📊 Test 3: Market Data (for comparison)");
|
|
println!("======================================");
|
|
|
|
let mut market_times = Vec::new();
|
|
for i in 0..3 {
|
|
let start = Instant::now();
|
|
match client.get_sampling_simplified_markets(None).await {
|
|
Ok(markets) => {
|
|
let duration = start.elapsed();
|
|
market_times.push(duration);
|
|
if i < 2 {
|
|
println!(
|
|
" Run {}: ✅ {} markets in {:?}",
|
|
i + 1,
|
|
markets.data.len(),
|
|
duration
|
|
);
|
|
}
|
|
},
|
|
Err(e) => {
|
|
let duration = start.elapsed();
|
|
market_times.push(duration);
|
|
if i < 2 {
|
|
println!(" Run {}: ❌ Error in {:?}: {}", i + 1, duration, e);
|
|
}
|
|
},
|
|
}
|
|
}
|
|
|
|
if !market_times.is_empty() {
|
|
let avg = market_times.iter().sum::<std::time::Duration>() / market_times.len() as u32;
|
|
println!(" 📈 Market data average: {:?}", avg);
|
|
println!(
|
|
" 🆚 vs original (404.5ms): {:.1}x faster",
|
|
404.5 / avg.as_millis() as f64
|
|
);
|
|
}
|
|
|
|
println!("\n🎯 Authenticated Benchmark Summary");
|
|
println!("=================================");
|
|
println!("Real Production Performance:");
|
|
|
|
if !order_times.is_empty() {
|
|
let order_avg = order_times.iter().sum::<std::time::Duration>() / order_times.len() as u32;
|
|
println!(" • Order creation: {:?} (vs 266.5ms baseline)", order_avg);
|
|
}
|
|
|
|
if !market_times.is_empty() {
|
|
let market_avg =
|
|
market_times.iter().sum::<std::time::Duration>() / market_times.len() as u32;
|
|
println!(" • Market data: {:?} (vs 404.5ms baseline)", market_avg);
|
|
}
|
|
|
|
println!("\nThis gives us the REAL production numbers to compare!");
|
|
println!("Network optimizations + EIP-712 signing performance combined.");
|
|
|
|
Ok(())
|
|
}
|