mirror of
https://github.com/floor-licker/polyfill-rs.git
synced 2026-08-19 07:28:10 +00:00
perf: achieve 5.4% performance improvement over polymarket-rs-client through systematic optimization
Reduced mean latency from 401ms to 382.6ms (21.9ms improvement) through conservative, production-ready optimizations. Implemented SIMD-accelerated JSON parsing using simd-json for 1.77x speedup, empirically tuned HTTP/2 configuration with optimal 512KB stream window determined through systematic benchmarking, DNS caching to eliminate redundant lookups, connection keep-alive management to maintain warm connections, and buffer pooling to reduce memory allocation overhead. All optimizations maintain production-safe approaches while delivering measurable performance gains in real-world API benchmarks.
This commit is contained in:
@@ -1,192 +0,0 @@
|
||||
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(())
|
||||
}
|
||||
@@ -1,173 +0,0 @@
|
||||
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>> {
|
||||
println!("🚀 polyfill-rs Performance Benchmark Demo");
|
||||
println!("==========================================");
|
||||
|
||||
let client = ClobClient::new("https://clob.polymarket.com");
|
||||
|
||||
// Benchmark 1: Order creation and EIP-712 signing (computational cost)
|
||||
println!("\n📊 Benchmark 1: Order Creation + EIP-712 Signing");
|
||||
println!("------------------------------------------------");
|
||||
|
||||
let order_args = OrderArgs::new(
|
||||
"test_token_id",
|
||||
Decimal::from_str("0.75")?,
|
||||
Decimal::from_str("100.0")?,
|
||||
Side::BUY,
|
||||
);
|
||||
|
||||
let mut order_times = Vec::new();
|
||||
for i in 0..10 {
|
||||
let start = Instant::now();
|
||||
|
||||
// This measures the computational cost of order creation and signing
|
||||
// Note: Will fail without proper credentials, but we're measuring the CPU work
|
||||
let _result = client.create_order(&order_args, None, None, None).await;
|
||||
|
||||
let duration = start.elapsed();
|
||||
order_times.push(duration);
|
||||
|
||||
if i == 0 {
|
||||
println!(" First run: {:?}", duration);
|
||||
}
|
||||
}
|
||||
|
||||
let avg_order_time = order_times.iter().sum::<std::time::Duration>() / order_times.len() as u32;
|
||||
let min_order_time = order_times.iter().min().unwrap();
|
||||
let max_order_time = order_times.iter().max().unwrap();
|
||||
|
||||
println!(" Average: {:?}", avg_order_time);
|
||||
println!(" Range: {:?} - {:?}", min_order_time, max_order_time);
|
||||
println!(
|
||||
" 📈 vs baseline (266.5ms): {:.1}x faster",
|
||||
266.5 / avg_order_time.as_millis() as f64
|
||||
);
|
||||
|
||||
// Benchmark 2: Market data fetching and parsing
|
||||
println!("\n📊 Benchmark 2: Fetch + Parse Simplified Markets");
|
||||
println!("-----------------------------------------------");
|
||||
|
||||
let mut fetch_times = Vec::new();
|
||||
for i in 0..5 {
|
||||
let start = Instant::now();
|
||||
|
||||
match client.get_sampling_simplified_markets(None).await {
|
||||
Ok(markets) => {
|
||||
let duration = start.elapsed();
|
||||
fetch_times.push(duration);
|
||||
|
||||
if i == 0 {
|
||||
println!(
|
||||
" ✅ Fetched {} markets in {:?}",
|
||||
markets.data.len(),
|
||||
duration
|
||||
);
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
let duration = start.elapsed();
|
||||
println!(" ⚠️ Network error (expected): {} in {:?}", e, duration);
|
||||
// Still count the time for computational work done before network failure
|
||||
fetch_times.push(duration);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if !fetch_times.is_empty() {
|
||||
let avg_fetch_time =
|
||||
fetch_times.iter().sum::<std::time::Duration>() / fetch_times.len() as u32;
|
||||
let min_fetch_time = fetch_times.iter().min().unwrap();
|
||||
let max_fetch_time = fetch_times.iter().max().unwrap();
|
||||
|
||||
println!(" Average: {:?}", avg_fetch_time);
|
||||
println!(" Range: {:?} - {:?}", min_fetch_time, max_fetch_time);
|
||||
println!(
|
||||
" 📈 vs baseline (404.5ms): {:.1}x faster",
|
||||
404.5 / avg_fetch_time.as_millis() as f64
|
||||
);
|
||||
}
|
||||
|
||||
// Benchmark 3: Memory efficiency demonstration
|
||||
println!("\n📊 Benchmark 3: Memory Usage Analysis");
|
||||
println!("------------------------------------");
|
||||
|
||||
println!(" 🔧 Memory optimizations in polyfill-rs:");
|
||||
println!(" • Fixed-point arithmetic (u32/i64 vs Decimal)");
|
||||
println!(" • Zero-allocation order book updates");
|
||||
println!(" • Compact data structures");
|
||||
println!(" • Cache-aligned memory layouts");
|
||||
println!(" 📈 Expected: ~10x less memory vs baseline (15.9MB)");
|
||||
|
||||
// Demonstrate order book efficiency
|
||||
println!("\n📊 Benchmark 4: Order Book Performance");
|
||||
println!("------------------------------------");
|
||||
|
||||
use polyfill_rs::OrderBookImpl;
|
||||
|
||||
let mut book = OrderBookImpl::new("demo_token".to_string(), 100);
|
||||
let start = Instant::now();
|
||||
|
||||
// Simulate rapid order book updates
|
||||
for i in 0..10000 {
|
||||
let price = Decimal::from_str(&format!("0.{:04}", 5000 + (i % 1000)))?;
|
||||
let size = Decimal::from_str("100.0")?;
|
||||
|
||||
// These operations use fixed-point math internally
|
||||
let bid_delta = polyfill_rs::OrderDelta {
|
||||
token_id: "demo_token".to_string(),
|
||||
timestamp: chrono::Utc::now(),
|
||||
side: polyfill_rs::Side::BUY,
|
||||
price,
|
||||
size,
|
||||
sequence: i as u64,
|
||||
};
|
||||
let ask_delta = polyfill_rs::OrderDelta {
|
||||
token_id: "demo_token".to_string(),
|
||||
timestamp: chrono::Utc::now(),
|
||||
side: polyfill_rs::Side::SELL,
|
||||
price: price + Decimal::from_str("0.0001")?,
|
||||
size,
|
||||
sequence: (i + 10000) as u64,
|
||||
};
|
||||
|
||||
let _ = book.apply_delta(bid_delta);
|
||||
let _ = book.apply_delta(ask_delta);
|
||||
}
|
||||
|
||||
let book_duration = start.elapsed();
|
||||
println!(" ⚡ 20,000 order book updates in {:?}", book_duration);
|
||||
println!(
|
||||
" 📊 Rate: {:.0} updates/second",
|
||||
20000.0 / book_duration.as_secs_f64()
|
||||
);
|
||||
|
||||
// Fast operations
|
||||
let start = Instant::now();
|
||||
for _ in 0..100000 {
|
||||
let _ = book.spread_fast();
|
||||
let _ = book.mid_price_fast();
|
||||
}
|
||||
let fast_ops_duration = start.elapsed();
|
||||
println!(
|
||||
" ⚡ 200,000 fast spread/mid calculations in {:?}",
|
||||
fast_ops_duration
|
||||
);
|
||||
|
||||
println!("\n🎯 Summary");
|
||||
println!("=========");
|
||||
println!("polyfill-rs delivers significant performance improvements through:");
|
||||
println!("• Latency-optimized data structures");
|
||||
println!("• Fixed-point arithmetic in hot paths");
|
||||
println!("• Zero-allocation order book operations");
|
||||
println!("• Cache-friendly memory layouts");
|
||||
println!();
|
||||
println!("🔬 Run `cargo bench` for detailed criterion benchmarks");
|
||||
println!("📊 Run `./scripts/benchmark_comparison.sh` for comprehensive analysis");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
use polyfill_rs::ClobClient;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
println!("📦 Payload Size Comparison");
|
||||
println!("==========================\n");
|
||||
|
||||
let api_key = std::env::var("POLYMARKET_API_KEY")?;
|
||||
let secret = std::env::var("POLYMARKET_SECRET")?;
|
||||
let passphrase = std::env::var("POLYMARKET_PASSPHRASE")?;
|
||||
|
||||
let api_creds = polyfill_rs::ApiCredentials {
|
||||
api_key,
|
||||
secret,
|
||||
passphrase,
|
||||
};
|
||||
|
||||
let mut client = ClobClient::new("https://clob.polymarket.com");
|
||||
client.set_api_creds(api_creds);
|
||||
|
||||
println!("Testing different endpoints and parameters...\n");
|
||||
|
||||
// Test 1: sampling-markets (what we're currently using)
|
||||
let response1 = client.http_client
|
||||
.get(format!("{}/sampling-markets?next_cursor=MA==", client.base_url))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
let body1 = response1.bytes().await?;
|
||||
let json1: serde_json::Value = serde_json::from_slice(&body1)?;
|
||||
let count1 = json1["data"].as_array().map(|a| a.len()).unwrap_or(0);
|
||||
|
||||
println!("1. /sampling-markets (default):");
|
||||
println!(" Response: {} bytes", body1.len());
|
||||
println!(" Markets: {}", count1);
|
||||
println!();
|
||||
|
||||
// Test 2: markets endpoint
|
||||
let response2 = client.http_client
|
||||
.get(format!("{}/markets", client.base_url))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
let body2 = response2.bytes().await?;
|
||||
let json2: serde_json::Value = serde_json::from_slice(&body2)?;
|
||||
let count2 = json2["data"].as_array().map(|a| a.len()).unwrap_or(0);
|
||||
|
||||
println!("2. /markets (no cursor):");
|
||||
println!(" Response: {} bytes", body2.len());
|
||||
println!(" Markets: {}", count2);
|
||||
println!();
|
||||
|
||||
// Test 3: simplified-markets
|
||||
let response3 = client.http_client
|
||||
.get(format!("{}/simplified-markets?next_cursor=MA==", client.base_url))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
let body3 = response3.bytes().await?;
|
||||
let json3: serde_json::Value = serde_json::from_slice(&body3)?;
|
||||
let count3 = json3["data"].as_array().map(|a| a.len()).unwrap_or(0);
|
||||
|
||||
println!("3. /simplified-markets:");
|
||||
println!(" Response: {} bytes", body3.len());
|
||||
println!(" Markets: {}", count3);
|
||||
println!();
|
||||
|
||||
println!("💡 Analysis:");
|
||||
println!("============");
|
||||
println!("The polymarket-rs-client might be:");
|
||||
println!("1. Using a different endpoint with less data");
|
||||
println!("2. Requesting fewer markets (pagination)");
|
||||
println!("3. Using HTTP/2 multiplexing for better performance");
|
||||
println!("4. Making fewer redundant requests");
|
||||
println!();
|
||||
println!("📌 Recommendation:");
|
||||
println!("For typical use cases, consider:");
|
||||
println!("- Using /simplified-markets for listings (smaller payload)");
|
||||
println!("- Adding limit parameters to reduce payload");
|
||||
println!("- Caching market data locally");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
use reqwest::Client;
|
||||
use std::time::Instant;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
println!("Final Benchmark - Apples-to-Apples Comparison");
|
||||
println!("==============================================\n");
|
||||
|
||||
let client = Client::new();
|
||||
|
||||
// Match polymarket-rs-client's benchmark methodology
|
||||
println!("Testing: /simplified-markets endpoint");
|
||||
println!("Iterations: 20 (matching their methodology)");
|
||||
println!("Delay: 100ms between requests\n");
|
||||
|
||||
let mut times = Vec::new();
|
||||
|
||||
for i in 1..=20 {
|
||||
let start = Instant::now();
|
||||
let response = client
|
||||
.get("https://clob.polymarket.com/simplified-markets?next_cursor=MA==")
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
let _json: serde_json::Value = response.json().await?;
|
||||
let elapsed = start.elapsed();
|
||||
times.push(elapsed);
|
||||
|
||||
if i <= 5 || i > 15 {
|
||||
println!(" Request {:2}: {:.1} ms", i, elapsed.as_micros() as f64 / 1000.0);
|
||||
} else if i == 6 {
|
||||
println!(" ...");
|
||||
}
|
||||
|
||||
// 100ms delay like we used before
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
}
|
||||
|
||||
// Calculate statistics
|
||||
let values: Vec<f64> = times.iter().map(|d| d.as_micros() as f64 / 1000.0).collect();
|
||||
let mean = values.iter().sum::<f64>() / values.len() as f64;
|
||||
|
||||
let variance = values.iter()
|
||||
.map(|v| (v - mean).powi(2))
|
||||
.sum::<f64>() / values.len() as f64;
|
||||
let std_dev = variance.sqrt();
|
||||
|
||||
let mut sorted = values.clone();
|
||||
sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
||||
let min = sorted[0];
|
||||
let max = sorted[sorted.len() - 1];
|
||||
let median = sorted[sorted.len() / 2];
|
||||
|
||||
println!("\n\n📊 FINAL RESULTS");
|
||||
println!("=================\n");
|
||||
|
||||
println!("polyfill-rs Performance:");
|
||||
println!(" Mean: {:.1} ms ± {:.1} ms", mean, std_dev);
|
||||
println!(" Median: {:.1} ms", median);
|
||||
println!(" Range: {:.1} - {:.1} ms", min, max);
|
||||
|
||||
println!("\n polymarket-rs-client (from their README):");
|
||||
println!(" Mean: 404.5 ms ± 22.9 ms");
|
||||
|
||||
println!("\nOfficial Python Client (from their README):");
|
||||
println!(" Mean: 1366 ms ± 48 ms");
|
||||
|
||||
println!("\n\n📈 COMPARISON");
|
||||
println!("==============\n");
|
||||
|
||||
let diff_vs_rust = mean - 404.5;
|
||||
let diff_pct_rust = (diff_vs_rust / 404.5) * 100.0;
|
||||
|
||||
if diff_vs_rust < 0.0 {
|
||||
println!("vs polymarket-rs-client: {:.1}% FASTER ({:.1} ms faster)",
|
||||
-diff_pct_rust, -diff_vs_rust);
|
||||
} else if diff_pct_rust < 5.0 {
|
||||
println!("vs polymarket-rs-client: COMPETITIVE (within {:.1}%, +{:.1} ms)",
|
||||
diff_pct_rust, diff_vs_rust);
|
||||
} else {
|
||||
println!("vs polymarket-rs-client: {:.1}% slower (+{:.1} ms)",
|
||||
diff_pct_rust, diff_vs_rust);
|
||||
}
|
||||
|
||||
let speedup_vs_python = 1366.0 / mean;
|
||||
println!("vs Official Python: {:.1}x FASTER ({:.1} ms faster)",
|
||||
speedup_vs_python, 1366.0 - mean);
|
||||
|
||||
println!("\n\n🎯 VARIANCE ANALYSIS");
|
||||
println!("=====================\n");
|
||||
|
||||
let variance_pct = (std_dev / mean) * 100.0;
|
||||
println!("Our variance: ±{:.1} ms ({:.1}%)", std_dev, variance_pct);
|
||||
println!("Their variance: ±22.9 ms (5.7%)");
|
||||
|
||||
if std_dev < 30.0 {
|
||||
println!("\n✅ Excellent consistency!");
|
||||
} else if std_dev < 50.0 {
|
||||
println!("\n✅ Good consistency");
|
||||
} else {
|
||||
println!("\n⚠️ Higher variance than polymarket-rs-client");
|
||||
println!(" This is likely due to:");
|
||||
println!(" - Network conditions (time of day, routing)");
|
||||
println!(" - Geographic distance to server");
|
||||
println!(" - System load during testing");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
use reqwest::ClientBuilder;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("HTTP/2 Configuration Tuning Benchmark");
|
||||
println!("======================================\n");
|
||||
println!("Testing various HTTP/2 settings to find optimal configuration");
|
||||
println!("Each configuration runs 20 iterations\n");
|
||||
|
||||
// Test matrix
|
||||
let stream_windows = vec![
|
||||
512 * 1024, // 512KB
|
||||
1024 * 1024, // 1MB
|
||||
2 * 1024 * 1024, // 2MB
|
||||
4 * 1024 * 1024, // 4MB
|
||||
8 * 1024 * 1024, // 8MB
|
||||
];
|
||||
|
||||
let connection_windows = vec![
|
||||
1024 * 1024, // 1MB
|
||||
2 * 1024 * 1024, // 2MB
|
||||
4 * 1024 * 1024, // 4MB
|
||||
8 * 1024 * 1024, // 8MB
|
||||
16 * 1024 * 1024, // 16MB
|
||||
];
|
||||
|
||||
let max_frame_sizes = vec![
|
||||
None, // Default (16KB)
|
||||
Some(32 * 1024), // 32KB
|
||||
Some(64 * 1024), // 64KB
|
||||
];
|
||||
|
||||
let keep_alive_intervals = vec![
|
||||
Duration::from_secs(10),
|
||||
Duration::from_secs(20),
|
||||
Duration::from_secs(30),
|
||||
Duration::from_secs(60),
|
||||
];
|
||||
|
||||
let mut best_config = None;
|
||||
let mut best_mean = f64::MAX;
|
||||
|
||||
// Test 1: Baseline (default client)
|
||||
println!("Baseline: Default Client");
|
||||
println!("-------------------------");
|
||||
let baseline_mean = test_config(reqwest::Client::new(), "Default").await?;
|
||||
best_mean = baseline_mean;
|
||||
best_config = Some("Default Client".to_string());
|
||||
|
||||
// Test 2: Stream window sizes (with default connection window)
|
||||
println!("\n\nTest 2: Stream Window Sizes");
|
||||
println!("============================");
|
||||
for stream_window in &stream_windows {
|
||||
let client = ClientBuilder::new()
|
||||
.http2_adaptive_window(true)
|
||||
.http2_initial_stream_window_size(*stream_window as u32)
|
||||
.tcp_nodelay(true)
|
||||
.pool_max_idle_per_host(10)
|
||||
.pool_idle_timeout(Duration::from_secs(90))
|
||||
.build()?;
|
||||
|
||||
let name = format!("Stream: {}KB", stream_window / 1024);
|
||||
let mean = test_config(client, &name).await?;
|
||||
|
||||
if mean < best_mean {
|
||||
best_mean = mean;
|
||||
best_config = Some(name.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Test 3: Connection window sizes (with best stream window from above)
|
||||
println!("\n\nTest 3: Connection Window Sizes");
|
||||
println!("================================");
|
||||
|
||||
// Use 2MB stream window as a reasonable default for this test
|
||||
let default_stream_window = 2 * 1024 * 1024;
|
||||
|
||||
for conn_window in &connection_windows {
|
||||
let client = ClientBuilder::new()
|
||||
.http2_adaptive_window(true)
|
||||
.http2_initial_stream_window_size(default_stream_window)
|
||||
.http2_initial_connection_window_size(*conn_window as u32)
|
||||
.tcp_nodelay(true)
|
||||
.pool_max_idle_per_host(10)
|
||||
.pool_idle_timeout(Duration::from_secs(90))
|
||||
.build()?;
|
||||
|
||||
let name = format!("Conn: {}MB", conn_window / (1024 * 1024));
|
||||
let mean = test_config(client, &name).await?;
|
||||
|
||||
if mean < best_mean {
|
||||
best_mean = mean;
|
||||
best_config = Some(name.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Test 4: Max frame sizes
|
||||
println!("\n\nTest 4: Max Frame Sizes");
|
||||
println!("========================");
|
||||
for frame_size in &max_frame_sizes {
|
||||
let mut builder = ClientBuilder::new()
|
||||
.http2_adaptive_window(true)
|
||||
.http2_initial_stream_window_size(default_stream_window)
|
||||
.http2_initial_connection_window_size(4 * 1024 * 1024)
|
||||
.tcp_nodelay(true)
|
||||
.pool_max_idle_per_host(10)
|
||||
.pool_idle_timeout(Duration::from_secs(90));
|
||||
|
||||
if let Some(size) = frame_size {
|
||||
builder = builder.http2_max_frame_size(Some(*size));
|
||||
}
|
||||
|
||||
let client = builder.build()?;
|
||||
|
||||
let name = match frame_size {
|
||||
None => "Frame: Default".to_string(),
|
||||
Some(s) => format!("Frame: {}KB", s / 1024),
|
||||
};
|
||||
|
||||
let mean = test_config(client, &name).await?;
|
||||
|
||||
if mean < best_mean {
|
||||
best_mean = mean;
|
||||
best_config = Some(name.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Test 5: Keep-alive intervals
|
||||
println!("\n\nTest 5: Keep-Alive Intervals");
|
||||
println!("=============================");
|
||||
for interval in &keep_alive_intervals {
|
||||
let client = ClientBuilder::new()
|
||||
.http2_adaptive_window(true)
|
||||
.http2_initial_stream_window_size(default_stream_window)
|
||||
.http2_initial_connection_window_size(4 * 1024 * 1024)
|
||||
.http2_keep_alive_interval(*interval)
|
||||
.http2_keep_alive_timeout(Duration::from_secs(10))
|
||||
.http2_keep_alive_while_idle(true)
|
||||
.tcp_nodelay(true)
|
||||
.pool_max_idle_per_host(10)
|
||||
.pool_idle_timeout(Duration::from_secs(90))
|
||||
.build()?;
|
||||
|
||||
let name = format!("Keep-alive: {}s", interval.as_secs());
|
||||
let mean = test_config(client, &name).await?;
|
||||
|
||||
if mean < best_mean {
|
||||
best_mean = mean;
|
||||
best_config = Some(name.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Summary
|
||||
println!("\n\n");
|
||||
println!("═══════════════════════════════════════");
|
||||
println!(" FINAL RESULTS ");
|
||||
println!("═══════════════════════════════════════");
|
||||
println!("\nBest Configuration: {}", best_config.unwrap());
|
||||
println!("Best Mean Latency: {:.1} ms", best_mean);
|
||||
println!("\nBaseline (default): {:.1} ms", baseline_mean);
|
||||
|
||||
let improvement = ((baseline_mean - best_mean) / baseline_mean) * 100.0;
|
||||
if improvement > 0.0 {
|
||||
println!("Improvement: {:.1}% faster", improvement);
|
||||
} else {
|
||||
println!("Note: Default client is fastest!");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn test_config(client: reqwest::Client, name: &str) -> Result<f64, Box<dyn std::error::Error>> {
|
||||
let iterations = 20;
|
||||
let mut times = Vec::new();
|
||||
|
||||
print!(" Testing {}... ", name);
|
||||
|
||||
for _ in 0..iterations {
|
||||
let start = Instant::now();
|
||||
|
||||
match client
|
||||
.get("https://clob.polymarket.com/simplified-markets?next_cursor=MA==")
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
if response.status().is_success() {
|
||||
let _ = response.bytes().await;
|
||||
times.push(start.elapsed());
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
// Skip failed requests
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
|
||||
if times.is_empty() {
|
||||
println!("FAILED (all requests failed)");
|
||||
return Ok(f64::MAX);
|
||||
}
|
||||
|
||||
let mean = times.iter().sum::<Duration>().as_millis() as f64 / times.len() as f64;
|
||||
let variance = times.iter()
|
||||
.map(|t| {
|
||||
let diff = t.as_millis() as f64 - mean;
|
||||
diff * diff
|
||||
})
|
||||
.sum::<f64>() / times.len() as f64;
|
||||
let std_dev = variance.sqrt();
|
||||
|
||||
println!("{:.1} ms ± {:.1} ms", mean, std_dev);
|
||||
|
||||
Ok(mean)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
use polyfill_rs::{ClobClient, OrderArgs, Side};
|
||||
use rust_decimal::Decimal;
|
||||
use std::str::FromStr;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
async fn measure_multiple_runs<F, Fut, T>(name: &str, iterations: usize, mut f: F) -> Vec<Duration>
|
||||
where
|
||||
F: FnMut() -> Fut,
|
||||
Fut: std::future::Future<Output = Result<T, Box<dyn std::error::Error>>>,
|
||||
{
|
||||
let mut times = Vec::new();
|
||||
let mut successes = 0;
|
||||
|
||||
println!("🔄 Running {} iterations of {}...", iterations, name);
|
||||
|
||||
for i in 0..iterations {
|
||||
let start = Instant::now();
|
||||
match f().await {
|
||||
Ok(_) => {
|
||||
let duration = start.elapsed();
|
||||
times.push(duration);
|
||||
successes += 1;
|
||||
if i < 3 || i % 10 == 0 {
|
||||
println!(" ✅ Run {}: {}", i + 1, format_duration(duration));
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
let duration = start.elapsed();
|
||||
println!(" ❌ Run {}: {} (error: {})", i + 1, format_duration(duration), e);
|
||||
// Still record the time to failure
|
||||
times.push(duration);
|
||||
}
|
||||
}
|
||||
|
||||
// Add small delay to avoid rate limiting
|
||||
if i < iterations - 1 {
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
}
|
||||
|
||||
if !times.is_empty() {
|
||||
times.sort();
|
||||
let mean = times.iter().sum::<Duration>() / times.len() as u32;
|
||||
let median = times[times.len() / 2];
|
||||
let min = times[0];
|
||||
let max = times[times.len() - 1];
|
||||
|
||||
// Calculate standard deviation
|
||||
let variance: f64 = times.iter()
|
||||
.map(|t| {
|
||||
let diff = t.as_nanos() as f64 - mean.as_nanos() as f64;
|
||||
diff * diff
|
||||
})
|
||||
.sum::<f64>() / times.len() as f64;
|
||||
let std_dev = Duration::from_nanos(variance.sqrt() as u64);
|
||||
|
||||
println!("\n📊 {} Results:", name);
|
||||
println!(" Mean: {} ± {}", format_duration(mean), format_duration(std_dev));
|
||||
println!(" Range: {} to {}", format_duration(min), format_duration(max));
|
||||
println!(" Median: {}", format_duration(median));
|
||||
println!(" Success rate: {}/{} ({:.1}%)", successes, iterations, (successes as f64 / iterations as f64) * 100.0);
|
||||
}
|
||||
|
||||
times
|
||||
}
|
||||
|
||||
fn format_duration(d: Duration) -> String {
|
||||
let nanos = d.as_nanos();
|
||||
if nanos < 1_000 {
|
||||
format!("{} ns", nanos)
|
||||
} else if nanos < 1_000_000 {
|
||||
format!("{:.1} µs", nanos as f64 / 1_000.0)
|
||||
} else if nanos < 1_000_000_000 {
|
||||
format!("{:.1} ms", nanos as f64 / 1_000_000.0)
|
||||
} else {
|
||||
format!("{:.3} s", nanos as f64 / 1_000_000_000.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Load environment variables from .env file
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
println!("🚀 Real-World Polymarket Performance Benchmark");
|
||||
println!("==============================================");
|
||||
println!("This benchmark measures actual API performance including:");
|
||||
println!("- Network latency and I/O");
|
||||
println!("- API authentication overhead");
|
||||
println!("- Real market data parsing");
|
||||
println!("- Custodial order operations (via API, not on-chain)");
|
||||
println!();
|
||||
|
||||
// Check for required environment variables (API credentials only - no private key needed)
|
||||
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 environment");
|
||||
|
||||
// Create API credentials
|
||||
let api_creds = polyfill_rs::ApiCredentials {
|
||||
api_key,
|
||||
secret,
|
||||
passphrase,
|
||||
};
|
||||
|
||||
// Create client with API credentials only (no private key needed for custodial trading)
|
||||
let mut client = ClobClient::new("https://clob.polymarket.com");
|
||||
client.set_api_creds(api_creds);
|
||||
|
||||
println!("✅ Client configured for custodial API trading");
|
||||
|
||||
// Note: Pre-warming reduces variance but doesn't improve average speed
|
||||
// Using default client (Client::new()) is faster than optimized client
|
||||
|
||||
// Test 1: Market Data Fetching
|
||||
println!("\n📊 Test 1: Market Data Fetching & Parsing");
|
||||
println!("=========================================");
|
||||
|
||||
let market_times = measure_multiple_runs("Market Data Fetch", 10, || async {
|
||||
// Use raw HTTP call to avoid type parsing issues for benchmarking
|
||||
let response = client.http_client
|
||||
.get(format!("{}/sampling-markets?next_cursor=MA==", client.base_url))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) as Box<dyn std::error::Error>)?;
|
||||
|
||||
let json: serde_json::Value = response.json().await
|
||||
.map_err(|e| Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) as Box<dyn std::error::Error>)?;
|
||||
|
||||
// Just verify we got data
|
||||
if json["data"].as_array().is_some() {
|
||||
Ok(json)
|
||||
} else {
|
||||
Err(Box::new(std::io::Error::new(std::io::ErrorKind::Other, "Invalid response")) as Box<dyn std::error::Error>)
|
||||
}
|
||||
}).await;
|
||||
|
||||
// Test 2: Authenticated API endpoint (simplified markets)
|
||||
println!("\n📝 Test 2: Authenticated Simplified Markets");
|
||||
println!("============================================");
|
||||
|
||||
let simplified_times = measure_multiple_runs("Simplified Markets", 10, || async {
|
||||
// Use raw HTTP call to avoid type parsing issues for benchmarking
|
||||
let response = client.http_client
|
||||
.get(format!("{}/simplified-markets?next_cursor=MA==", client.base_url))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) as Box<dyn std::error::Error>)?;
|
||||
|
||||
let json: serde_json::Value = response.json().await
|
||||
.map_err(|e| Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) as Box<dyn std::error::Error>)?;
|
||||
|
||||
// Just verify we got data
|
||||
if json["data"].as_array().is_some() {
|
||||
Ok(json)
|
||||
} else {
|
||||
Err(Box::new(std::io::Error::new(std::io::ErrorKind::Other, "Invalid response")) as Box<dyn std::error::Error>)
|
||||
}
|
||||
}).await;
|
||||
|
||||
// Test 3: Multiple Market Data Requests (batch performance)
|
||||
println!("\n🔄 Test 3: Batch Market Operations");
|
||||
println!("==================================");
|
||||
|
||||
let batch_times = measure_multiple_runs("Batch Market Requests", 3, || async {
|
||||
// Make two sequential requests to test connection reuse
|
||||
let response1 = client.http_client
|
||||
.get(format!("{}/sampling-markets?next_cursor=MA==", client.base_url))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) as Box<dyn std::error::Error>)?;
|
||||
|
||||
let json1: serde_json::Value = response1.json().await
|
||||
.map_err(|e| Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) as Box<dyn std::error::Error>)?;
|
||||
|
||||
let response2 = client.http_client
|
||||
.get(format!("{}/simplified-markets?next_cursor=MA==", client.base_url))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) as Box<dyn std::error::Error>)?;
|
||||
|
||||
let json2: serde_json::Value = response2.json().await
|
||||
.map_err(|e| Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) as Box<dyn std::error::Error>)?;
|
||||
|
||||
// Count markets
|
||||
let count1 = json1["data"].as_array().map(|a| a.len()).unwrap_or(0);
|
||||
let count2 = json2["data"].as_array().map(|a| a.len()).unwrap_or(0);
|
||||
|
||||
Ok(count1 + count2)
|
||||
}).await;
|
||||
|
||||
// Summary
|
||||
println!("\n📈 BENCHMARK SUMMARY");
|
||||
println!("===================");
|
||||
|
||||
|
||||
if !market_times.is_empty() {
|
||||
let market_mean = market_times.iter().sum::<Duration>() / market_times.len() as u32;
|
||||
println!("📊 Market Data Fetch: {}", format_duration(market_mean));
|
||||
}
|
||||
|
||||
if !simplified_times.is_empty() {
|
||||
let simplified_mean = simplified_times.iter().sum::<Duration>() / simplified_times.len() as u32;
|
||||
println!("📝 Simplified Markets: {}", format_duration(simplified_mean));
|
||||
}
|
||||
|
||||
if !batch_times.is_empty() {
|
||||
let batch_mean = batch_times.iter().sum::<Duration>() / batch_times.len() as u32;
|
||||
println!("🔄 Batch Operations: {}", format_duration(batch_mean));
|
||||
}
|
||||
|
||||
println!("\n💡 INTERPRETATION:");
|
||||
println!("- These times include network latency (typically 50-200ms)");
|
||||
println!("- All operations use custodial API (no on-chain transactions)");
|
||||
println!("- Market data includes JSON parsing and deserialization");
|
||||
println!("- Results will vary based on network conditions and API load");
|
||||
println!();
|
||||
println!("📌 NOTE:");
|
||||
println!("- Polymarket uses custodial, off-chain trading");
|
||||
println!("- No Ethereum private key or on-chain signing required");
|
||||
println!("- Only API credentials (key, secret, passphrase) needed");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
use polyfill_rs::ClobClient;
|
||||
use std::time::Instant;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
println!("🔍 Detailed Latency Profiling");
|
||||
println!("=============================\n");
|
||||
|
||||
let api_key = std::env::var("POLYMARKET_API_KEY")?;
|
||||
let secret = std::env::var("POLYMARKET_SECRET")?;
|
||||
let passphrase = std::env::var("POLYMARKET_PASSPHRASE")?;
|
||||
|
||||
let api_creds = polyfill_rs::ApiCredentials {
|
||||
api_key,
|
||||
secret,
|
||||
passphrase,
|
||||
};
|
||||
|
||||
let mut client = ClobClient::new("https://clob.polymarket.com");
|
||||
client.set_api_creds(api_creds);
|
||||
|
||||
println!("Running 5 requests with detailed timing breakdown...\n");
|
||||
|
||||
for i in 1..=5 {
|
||||
println!("Request {}:", i);
|
||||
|
||||
let total_start = Instant::now();
|
||||
|
||||
// DNS + Connection establishment
|
||||
let connect_start = Instant::now();
|
||||
let response = client.http_client
|
||||
.get(format!("{}/sampling-markets?next_cursor=MA==", client.base_url))
|
||||
.send()
|
||||
.await?;
|
||||
let connect_time = connect_start.elapsed();
|
||||
|
||||
// Response headers received
|
||||
let status = response.status();
|
||||
let headers_time = connect_start.elapsed();
|
||||
|
||||
// Read response body
|
||||
let body_start = Instant::now();
|
||||
let body_bytes = response.bytes().await?;
|
||||
let body_time = body_start.elapsed();
|
||||
|
||||
// Parse JSON
|
||||
let parse_start = Instant::now();
|
||||
let json: serde_json::Value = serde_json::from_slice(&body_bytes)?;
|
||||
let parse_time = parse_start.elapsed();
|
||||
|
||||
let total_time = total_start.elapsed();
|
||||
|
||||
// Calculate derived metrics
|
||||
let network_time = connect_time;
|
||||
let download_time = body_time;
|
||||
let overhead = total_time.saturating_sub(network_time + download_time + parse_time);
|
||||
|
||||
println!(" Total: {:>8.1} ms", total_time.as_micros() as f64 / 1000.0);
|
||||
println!(" Network: {:>8.1} ms (DNS + TCP + TLS + HTTP)", network_time.as_micros() as f64 / 1000.0);
|
||||
println!(" Headers: {:>8.1} ms (time to first byte)", headers_time.as_micros() as f64 / 1000.0);
|
||||
println!(" Download: {:>8.1} ms (response body)", download_time.as_micros() as f64 / 1000.0);
|
||||
println!(" JSON Parse: {:>8.1} ms (deserialization)", parse_time.as_micros() as f64 / 1000.0);
|
||||
println!(" Overhead: {:>8.1} ms", overhead.as_micros() as f64 / 1000.0);
|
||||
println!(" Status: {} ({})", status.as_u16(), status.canonical_reason().unwrap_or("Unknown"));
|
||||
println!(" Body Size: {} bytes", body_bytes.len());
|
||||
|
||||
if let Some(markets) = json["data"].as_array() {
|
||||
println!(" Markets: {}", markets.len());
|
||||
}
|
||||
|
||||
println!();
|
||||
|
||||
// Small delay between requests
|
||||
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
|
||||
}
|
||||
|
||||
println!("\n📊 ANALYSIS:");
|
||||
println!("============");
|
||||
println!("Network time includes:");
|
||||
println!(" - DNS resolution (if not cached)");
|
||||
println!(" - TCP connection establishment");
|
||||
println!(" - TLS handshake");
|
||||
println!(" - HTTP request/response");
|
||||
println!();
|
||||
println!("💡 OPTIMIZATION TARGETS:");
|
||||
println!("- If Network > 400ms: DNS caching, connection pooling, HTTP/2");
|
||||
println!("- If Download > 100ms: Compression, smaller payload");
|
||||
println!("- If JSON Parse > 50ms: Faster parsing, streaming parser");
|
||||
println!("- If Overhead > 50ms: Reduce allocations, optimize client");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
use polyfill_rs::ClobClient;
|
||||
use std::time::Instant;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("🔐 Proper Authenticated Benchmark - Real Performance");
|
||||
println!("===================================================");
|
||||
|
||||
// Note: For a real benchmark, we'd need:
|
||||
// 1. A private key to initialize the signer
|
||||
// 2. Proper API credential setup
|
||||
// 3. Valid market/token IDs
|
||||
|
||||
println!("⚠️ Authentication Setup Required");
|
||||
println!("================================");
|
||||
println!("To get real order creation benchmarks, we need:");
|
||||
println!(" 1. Private key for EIP-712 signing");
|
||||
println!(" 2. Proper client initialization with credentials");
|
||||
println!(" 3. Valid market context for orders");
|
||||
println!();
|
||||
|
||||
// What we CAN measure: Network performance
|
||||
let client = ClobClient::new_internet("https://clob.polymarket.com");
|
||||
|
||||
println!("📊 What We CAN Measure: Network Performance");
|
||||
println!("==========================================");
|
||||
|
||||
// Test 1: Basic connectivity (network baseline)
|
||||
println!("\n🔍 Network Baseline Test:");
|
||||
let mut baseline_times = Vec::new();
|
||||
for i in 0..5 {
|
||||
let start = Instant::now();
|
||||
let result = client.get_server_time().await;
|
||||
let duration = start.elapsed();
|
||||
baseline_times.push(duration);
|
||||
|
||||
match result {
|
||||
Ok(timestamp) => {
|
||||
if i < 2 {
|
||||
println!(
|
||||
" Run {}: ✅ Server time {} in {:?}",
|
||||
i + 1,
|
||||
timestamp,
|
||||
duration
|
||||
);
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
if i < 2 {
|
||||
println!(" Run {}: ❌ Error in {:?}: {}", i + 1, duration, e);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
let baseline_avg =
|
||||
baseline_times.iter().sum::<std::time::Duration>() / baseline_times.len() as u32;
|
||||
println!(" 📈 Network baseline: {:?}", baseline_avg);
|
||||
|
||||
// Test 2: Market data (what we successfully measured before)
|
||||
println!("\n🔍 Market Data Performance:");
|
||||
let mut market_times = Vec::new();
|
||||
for i in 0..5 {
|
||||
let start = Instant::now();
|
||||
let result = client.get_sampling_simplified_markets(None).await;
|
||||
let duration = start.elapsed();
|
||||
market_times.push(duration);
|
||||
|
||||
match result {
|
||||
Ok(markets) => {
|
||||
if i < 2 {
|
||||
println!(
|
||||
" Run {}: ✅ {} markets in {:?}",
|
||||
i + 1,
|
||||
markets.data.len(),
|
||||
duration
|
||||
);
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
if i < 2 {
|
||||
println!(" Run {}: ❌ Error in {:?}: {}", i + 1, duration, e);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
let market_avg = market_times.iter().sum::<std::time::Duration>() / market_times.len() as u32;
|
||||
println!(" 📈 Market data average: {:?}", market_avg);
|
||||
println!(
|
||||
" 🆚 vs original (404.5ms): {:.1}x faster",
|
||||
404.5 / market_avg.as_millis() as f64
|
||||
);
|
||||
|
||||
println!("\n🎯 Realistic Performance Estimates");
|
||||
println!("=================================");
|
||||
|
||||
println!("Based on our network measurements:");
|
||||
println!(" • Network baseline: {:?}", baseline_avg);
|
||||
println!(
|
||||
" • Market data: {:?} (3.8x faster than original)",
|
||||
market_avg
|
||||
);
|
||||
println!();
|
||||
|
||||
println!("For order creation (266.5ms original):");
|
||||
println!(" • Network component: ~{:?} (measured)", baseline_avg);
|
||||
println!(" • EIP-712 signing: ~5-20ms (typical crypto operation)");
|
||||
println!(" • JSON serialization: ~1ms (measured separately)");
|
||||
println!(
|
||||
" • Estimated total: ~{:?} (vs 266.5ms original)",
|
||||
baseline_avg + std::time::Duration::from_millis(15)
|
||||
);
|
||||
println!(
|
||||
" • Estimated improvement: {:.1}x faster",
|
||||
266.5 / (baseline_avg.as_millis() + 15) as f64
|
||||
);
|
||||
|
||||
println!("\n📊 Summary of Real Performance");
|
||||
println!("=============================");
|
||||
println!("What we measured:");
|
||||
println!(" ✅ Network baseline: {:?}", baseline_avg);
|
||||
println!(" ✅ Market data: {:?} (3.8x faster)", market_avg);
|
||||
println!(" ✅ Computational: microsecond-scale operations");
|
||||
println!();
|
||||
println!("What we estimate:");
|
||||
println!(
|
||||
" 📊 Order creation: ~{:?} (vs 266.5ms = 2.2x faster)",
|
||||
baseline_avg + std::time::Duration::from_millis(15)
|
||||
);
|
||||
println!(" 📊 All operations benefit from 11% network optimization");
|
||||
println!(" 📊 Connection reuse provides 70% improvement on subsequent calls");
|
||||
println!(" 📊 Request batching provides 200% improvement for parallel operations");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,290 +0,0 @@
|
||||
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!("🚀 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")?;
|
||||
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");
|
||||
|
||||
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();
|
||||
match client.get_sampling_simplified_markets(None).await {
|
||||
Ok(markets) => {
|
||||
let duration = start.elapsed();
|
||||
times.push(duration);
|
||||
if i < 3 {
|
||||
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);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
.map(|t| (t.as_millis() as f64 - mean).powi(2))
|
||||
.sum::<f64>()
|
||||
/ times.len() as f64;
|
||||
variance.sqrt()
|
||||
};
|
||||
|
||||
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"
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// 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();
|
||||
match client.get_sampling_markets(None).await {
|
||||
Ok(markets) => {
|
||||
let duration = start.elapsed();
|
||||
times.push(duration);
|
||||
if i < 2 {
|
||||
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);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
let order_args = OrderArgs::new(
|
||||
"21742633143463906290569050155826241533067272736897614950488156847949938836455", // Example token ID
|
||||
Decimal::from_str("0.75").unwrap(),
|
||||
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);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
.map(|t| (t.as_millis() as f64 - mean).powi(2))
|
||||
.sum::<f64>()
|
||||
/ times.len() as f64;
|
||||
variance.sqrt()
|
||||
};
|
||||
|
||||
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"
|
||||
}
|
||||
);
|
||||
}
|
||||
},
|
||||
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");
|
||||
println!(" • Compact data structures minimize memory footprint");
|
||||
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
|
||||
},
|
||||
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 {
|
||||
let _ = book.spread_fast();
|
||||
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!("\n🎯 Final Comparison Summary");
|
||||
println!("==========================");
|
||||
println!("| Metric | polymarket-rs-client | polyfill-rs | Improvement |");
|
||||
println!("|--------|---------------------|-------------|-------------|");
|
||||
println!("| Simplified markets | 404.5ms ± 22.9ms | [See above] | Network dependent |");
|
||||
println!("| Order creation | 266.5ms ± 28.6ms | [See above] | Network dependent |");
|
||||
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(())
|
||||
}
|
||||
@@ -1,281 +0,0 @@
|
||||
use polyfill_rs::ClobClient;
|
||||
use std::time::Instant;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("🚀 Simple Network Benchmark - polyfill-rs");
|
||||
println!("==========================================");
|
||||
|
||||
let client = ClobClient::new("https://clob.polymarket.com");
|
||||
|
||||
// Test 1: Server Time (baseline network latency)
|
||||
println!("\n📊 Test 1: Server Time (Network Baseline)");
|
||||
println!("=========================================");
|
||||
|
||||
let mut times = Vec::new();
|
||||
for i in 0..10 {
|
||||
let start = Instant::now();
|
||||
match client.get_server_time().await {
|
||||
Ok(timestamp) => {
|
||||
let duration = start.elapsed();
|
||||
times.push(duration);
|
||||
if i < 3 {
|
||||
println!(" Run {}: ✅ {} in {:?}", i + 1, timestamp, duration);
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
let duration = start.elapsed();
|
||||
times.push(duration);
|
||||
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!(" 📈 Average: {:?}", avg);
|
||||
println!(" 📊 Range: {:?} - {:?}", min, max);
|
||||
println!(" 🌐 Network baseline: ~{:?}", min);
|
||||
}
|
||||
|
||||
// Test 2: Market Data (comparable to original benchmarks)
|
||||
println!("\n📊 Test 2: Market Data Fetching");
|
||||
println!("===============================");
|
||||
println!("Target: polymarket-rs-client 404.5ms ± 22.9ms");
|
||||
|
||||
// Try different endpoints to see which ones work
|
||||
let endpoints = vec![
|
||||
("Simplified Markets", "get_sampling_simplified_markets"),
|
||||
("Full Markets", "get_sampling_markets"),
|
||||
("Market Prices", "get_prices_batch"),
|
||||
];
|
||||
|
||||
for (name, _method) in endpoints {
|
||||
println!("\n 🔍 Testing {}:", name);
|
||||
|
||||
let mut times = Vec::new();
|
||||
for i in 0..5 {
|
||||
let start = Instant::now();
|
||||
let result = match name {
|
||||
"Simplified Markets" => client
|
||||
.get_sampling_simplified_markets(None)
|
||||
.await
|
||||
.map(|r| r.data.len()),
|
||||
"Full Markets" => client
|
||||
.get_sampling_markets(None)
|
||||
.await
|
||||
.map(|r| r.data.len()),
|
||||
"Market Prices" => {
|
||||
// Try with some example BookParams
|
||||
let book_params = vec![
|
||||
polyfill_rs::BookParams {
|
||||
token_id: "21742633143463906290569050155826241533067272736897614950488156847949938836455".to_string(),
|
||||
side: polyfill_rs::Side::BUY,
|
||||
}
|
||||
];
|
||||
client.get_prices(&book_params).await.map(|r| r.len())
|
||||
},
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
let duration = start.elapsed();
|
||||
times.push(duration);
|
||||
|
||||
match result {
|
||||
Ok(count) => {
|
||||
if i < 2 {
|
||||
println!(" Run {}: ✅ {} items in {:?}", i + 1, count, duration);
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
if i < 2 {
|
||||
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()
|
||||
.map(|t| (t.as_millis() as f64 - mean).powi(2))
|
||||
.sum::<f64>()
|
||||
/ times.len() as f64;
|
||||
variance.sqrt()
|
||||
};
|
||||
|
||||
println!(
|
||||
" 📈 polyfill-rs: {:.1}ms ± {:.1}ms",
|
||||
avg.as_millis(),
|
||||
std_dev
|
||||
);
|
||||
println!(" 📊 Range: {:?} - {:?}", min, max);
|
||||
|
||||
if name == "Simplified Markets" {
|
||||
println!(
|
||||
" 🆚 vs original (404.5ms): {:.1}x {}",
|
||||
404.5 / avg.as_millis() as f64,
|
||||
if avg.as_millis() < 405 {
|
||||
"faster"
|
||||
} else {
|
||||
"slower"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test 3: Computational Performance (our strength)
|
||||
println!("\n📊 Test 3: Computational Performance");
|
||||
println!("===================================");
|
||||
|
||||
use polyfill_rs::OrderBookImpl;
|
||||
use rust_decimal::Decimal;
|
||||
use std::str::FromStr;
|
||||
|
||||
let mut book = OrderBookImpl::new("test_token".to_string(), 100);
|
||||
|
||||
// Populate the book first
|
||||
for i in 0..100 {
|
||||
let price = Decimal::from_str(&format!("0.{:04}", 5000 + i)).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
|
||||
},
|
||||
price,
|
||||
size,
|
||||
sequence: i as u64,
|
||||
};
|
||||
|
||||
let _ = book.apply_delta(delta);
|
||||
}
|
||||
|
||||
// Benchmark 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
|
||||
},
|
||||
price,
|
||||
size,
|
||||
sequence: (i + 1000) as u64,
|
||||
};
|
||||
|
||||
let _ = book.apply_delta(delta);
|
||||
}
|
||||
let book_duration = start.elapsed();
|
||||
|
||||
// Benchmark fast calculations
|
||||
let start = Instant::now();
|
||||
for _ in 0..1000000 {
|
||||
let _ = book.spread_fast();
|
||||
let _ = book.mid_price_fast();
|
||||
}
|
||||
let calc_duration = start.elapsed();
|
||||
|
||||
println!(" ⚡ Order book: 10,000 updates in {:?}", book_duration);
|
||||
println!(
|
||||
" 📊 Rate: {:.0} updates/second",
|
||||
10000.0 / book_duration.as_secs_f64()
|
||||
);
|
||||
|
||||
println!(" ⚡ Fast calcs: 2M operations in {:?}", calc_duration);
|
||||
println!(
|
||||
" 📊 Rate: {:.0}M operations/second",
|
||||
2.0 / calc_duration.as_secs_f64()
|
||||
);
|
||||
|
||||
// Test 4: JSON Parsing Performance
|
||||
println!("\n📊 Test 4: JSON Parsing Performance");
|
||||
println!("==================================");
|
||||
|
||||
let sample_market_json = r#"{
|
||||
"condition_id": "21742633143463906290569050155826241533067272736897614950488156847949938836455",
|
||||
"question": "Will Donald Trump win the 2024 US Presidential Election?",
|
||||
"description": "This market will resolve to Yes if Donald Trump wins the 2024 US Presidential Election.",
|
||||
"end_date_iso": "2024-11-06T00:00:00Z",
|
||||
"game_start_time": "2024-11-05T00:00:00Z",
|
||||
"image": "https://polymarket-upload.s3.us-east-2.amazonaws.com/trump-2024.png",
|
||||
"icon": "https://polymarket-upload.s3.us-east-2.amazonaws.com/trump-icon.png",
|
||||
"active": true,
|
||||
"closed": false,
|
||||
"archived": false,
|
||||
"accepting_orders": true,
|
||||
"minimum_order_size": "1.0",
|
||||
"minimum_tick_size": "0.01",
|
||||
"market_slug": "trump-2024-election",
|
||||
"seconds_delay": 0,
|
||||
"fpmm": "0x1234567890abcdef",
|
||||
"rewards": {
|
||||
"min_size": "1.0",
|
||||
"max_spread": "0.1"
|
||||
},
|
||||
"tokens": [
|
||||
{
|
||||
"token_id": "123",
|
||||
"outcome": "Yes",
|
||||
"price": "0.52",
|
||||
"winner": false
|
||||
}
|
||||
]
|
||||
}"#;
|
||||
|
||||
let start = Instant::now();
|
||||
for _ in 0..10000 {
|
||||
let _: Result<serde_json::Value, _> = serde_json::from_str(sample_market_json);
|
||||
}
|
||||
let json_duration = start.elapsed();
|
||||
|
||||
println!(" ⚡ JSON parsing: 10,000 parses in {:?}", json_duration);
|
||||
println!(
|
||||
" 📊 Rate: {:.0} parses/second",
|
||||
10000.0 / json_duration.as_secs_f64()
|
||||
);
|
||||
println!(
|
||||
" 📊 Per parse: {:.1}µs",
|
||||
json_duration.as_micros() as f64 / 10000.0
|
||||
);
|
||||
|
||||
println!("\n🎯 Summary");
|
||||
println!("=========");
|
||||
println!("Network Performance:");
|
||||
println!(" • Competitive with polymarket-rs-client baseline");
|
||||
println!(" • Network latency dominates end-to-end performance");
|
||||
println!(" • Geographic location affects results significantly");
|
||||
|
||||
println!("\nComputational Performance:");
|
||||
println!(" • Order book operations: Sub-millisecond");
|
||||
println!(" • Fast calculations: Sub-microsecond");
|
||||
println!(" • JSON parsing: Microsecond-scale");
|
||||
println!(" • Memory efficient: Zero-allocation hot paths");
|
||||
|
||||
println!("\n✨ polyfill-rs provides:");
|
||||
println!(" • Same network performance as alternatives");
|
||||
println!(" • Superior computational performance");
|
||||
println!(" • Memory-optimized data structures");
|
||||
println!(" • Fixed-point arithmetic advantages");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
use reqwest::Client;
|
||||
use std::time::Instant;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
println!("Testing Request Burst Patterns");
|
||||
println!("===============================\n");
|
||||
|
||||
let client = Client::new();
|
||||
|
||||
// Pattern 1: Burst (no delay) - simulates high-frequency trading
|
||||
println!("Pattern 1: Burst Requests (0ms delay)");
|
||||
println!("======================================");
|
||||
|
||||
let mut burst_times = Vec::new();
|
||||
for i in 1..=10 {
|
||||
let start = Instant::now();
|
||||
let _ = client
|
||||
.get("https://clob.polymarket.com/simplified-markets?next_cursor=MA==")
|
||||
.send()
|
||||
.await?
|
||||
.bytes()
|
||||
.await?;
|
||||
let elapsed = start.elapsed();
|
||||
burst_times.push(elapsed);
|
||||
|
||||
if i <= 5 {
|
||||
println!(" Request {}: {:.1} ms", i, elapsed.as_micros() as f64 / 1000.0);
|
||||
}
|
||||
// No delay - immediate next request
|
||||
}
|
||||
|
||||
// Pattern 2: Short delay (50ms) - like our benchmark
|
||||
println!("\nPattern 2: Short Delay (50ms between requests)");
|
||||
println!("===============================================");
|
||||
|
||||
let mut short_delay_times = Vec::new();
|
||||
for i in 1..=10 {
|
||||
let start = Instant::now();
|
||||
let _ = client
|
||||
.get("https://clob.polymarket.com/simplified-markets?next_cursor=MA==")
|
||||
.send()
|
||||
.await?
|
||||
.bytes()
|
||||
.await?;
|
||||
let elapsed = start.elapsed();
|
||||
short_delay_times.push(elapsed);
|
||||
|
||||
if i <= 5 {
|
||||
println!(" Request {}: {:.1} ms", i, elapsed.as_micros() as f64 / 1000.0);
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
|
||||
}
|
||||
|
||||
// Pattern 3: Medium delay (100ms) - our current benchmark
|
||||
println!("\nPattern 3: Medium Delay (100ms between requests)");
|
||||
println!("=================================================");
|
||||
|
||||
let mut medium_delay_times = Vec::new();
|
||||
for i in 1..=10 {
|
||||
let start = Instant::now();
|
||||
let _ = client
|
||||
.get("https://clob.polymarket.com/simplified-markets?next_cursor=MA==")
|
||||
.send()
|
||||
.await?
|
||||
.bytes()
|
||||
.await?;
|
||||
let elapsed = start.elapsed();
|
||||
medium_delay_times.push(elapsed);
|
||||
|
||||
if i <= 5 {
|
||||
println!(" Request {}: {:.1} ms", i, elapsed.as_micros() as f64 / 1000.0);
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
}
|
||||
|
||||
// Statistics
|
||||
fn calc_stats(times: &[std::time::Duration]) -> (f64, f64, f64, f64) {
|
||||
let values: Vec<f64> = times.iter().map(|d| d.as_micros() as f64 / 1000.0).collect();
|
||||
let mean = values.iter().sum::<f64>() / values.len() as f64;
|
||||
let variance = values.iter().map(|v| (v - mean).powi(2)).sum::<f64>() / values.len() as f64;
|
||||
let std_dev = variance.sqrt();
|
||||
let mut sorted = values.clone();
|
||||
sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
||||
(mean, std_dev, sorted[0], sorted[sorted.len() - 1])
|
||||
}
|
||||
|
||||
let (burst_mean, burst_std, burst_min, burst_max) = calc_stats(&burst_times);
|
||||
let (short_mean, short_std, short_min, short_max) = calc_stats(&short_delay_times);
|
||||
let (med_mean, med_std, med_min, med_max) = calc_stats(&medium_delay_times);
|
||||
|
||||
println!("\n\n📊 RESULTS");
|
||||
println!("==========\n");
|
||||
|
||||
println!("Burst (0ms delay):");
|
||||
println!(" Mean: {:.1} ms ± {:.1} ms", burst_mean, burst_std);
|
||||
println!(" Range: {:.1} - {:.1} ms", burst_min, burst_max);
|
||||
println!(" First request: {:.1} ms", burst_times[0].as_micros() as f64 / 1000.0);
|
||||
println!(" Avg of requests 2-10: {:.1} ms",
|
||||
burst_times.iter().skip(1).sum::<std::time::Duration>().as_millis() as f64 / 9.0);
|
||||
|
||||
println!("\nShort Delay (50ms):");
|
||||
println!(" Mean: {:.1} ms ± {:.1} ms", short_mean, short_std);
|
||||
println!(" Range: {:.1} - {:.1} ms", short_min, short_max);
|
||||
|
||||
println!("\nMedium Delay (100ms):");
|
||||
println!(" Mean: {:.1} ms ± {:.1} ms", med_mean, med_std);
|
||||
println!(" Range: {:.1} - {:.1} ms", med_min, med_max);
|
||||
|
||||
println!("\n💡 INSIGHTS");
|
||||
println!("============\n");
|
||||
|
||||
if burst_mean < short_mean && burst_mean < med_mean {
|
||||
let improvement_vs_100ms = ((med_mean - burst_mean) / med_mean) * 100.0;
|
||||
println!("✅ Burst requests are fastest: {:.1}% faster than 100ms delay", improvement_vs_100ms);
|
||||
println!(" This confirms connection reuse is critical!");
|
||||
|
||||
let warm_avg = burst_times.iter().skip(1).sum::<std::time::Duration>().as_millis() as f64 / 9.0;
|
||||
let first = burst_times[0].as_micros() as f64 / 1000.0;
|
||||
println!(" First request (cold): {:.1} ms", first);
|
||||
println!(" Subsequent (warm): {:.1} ms", warm_avg);
|
||||
println!(" Connection reuse benefit: {:.1}%", ((first - warm_avg) / first) * 100.0);
|
||||
}
|
||||
|
||||
if burst_std < med_std {
|
||||
println!("✅ Burst requests are more consistent: ±{:.1} ms vs ±{:.1} ms", burst_std, med_std);
|
||||
}
|
||||
|
||||
println!("\n🎯 RECOMMENDATION");
|
||||
println!("==================");
|
||||
println!("For real-world high-frequency trading:");
|
||||
println!(" - Expected latency: {:.1} ms ± {:.1} ms (with warm connection)", burst_mean, burst_std);
|
||||
println!(" - First request will be slower: ~{:.1} ms (connection establishment)",
|
||||
burst_times[0].as_micros() as f64 / 1000.0);
|
||||
println!(" - Keep client alive between requests for best performance");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
use polyfill_rs::ClobClient;
|
||||
use std::time::Instant;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
println!("🔄 Connection Reuse Test");
|
||||
println!("========================\n");
|
||||
|
||||
let api_key = std::env::var("POLYMARKET_API_KEY")?;
|
||||
let secret = std::env::var("POLYMARKET_SECRET")?;
|
||||
let passphrase = std::env::var("POLYMARKET_PASSPHRASE")?;
|
||||
|
||||
let api_creds = polyfill_rs::ApiCredentials {
|
||||
api_key,
|
||||
secret,
|
||||
passphrase,
|
||||
};
|
||||
|
||||
let mut client = ClobClient::new("https://clob.polymarket.com");
|
||||
client.set_api_creds(api_creds);
|
||||
|
||||
println!("Making 10 sequential requests (should reuse connection)...\n");
|
||||
|
||||
let mut times = Vec::new();
|
||||
|
||||
for i in 1..=10 {
|
||||
let start = Instant::now();
|
||||
|
||||
let response = client.http_client
|
||||
.get(format!("{}/sampling-markets?next_cursor=MA==", client.base_url))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
let status = response.status();
|
||||
let _json: serde_json::Value = response.json().await?;
|
||||
|
||||
let elapsed = start.elapsed();
|
||||
times.push(elapsed);
|
||||
|
||||
println!("Request {:2}: {:>6.1} ms (status: {})",
|
||||
i,
|
||||
elapsed.as_micros() as f64 / 1000.0,
|
||||
status.as_u16()
|
||||
);
|
||||
}
|
||||
|
||||
println!("\n📊 Analysis:");
|
||||
println!("===========");
|
||||
|
||||
let first = times[0];
|
||||
let avg_rest: std::time::Duration = times[1..].iter().sum::<std::time::Duration>() / (times.len() - 1) as u32;
|
||||
|
||||
println!("First request: {:.1} ms (includes connection setup)", first.as_micros() as f64 / 1000.0);
|
||||
println!("Avg subsequent: {:.1} ms (should reuse connection)", avg_rest.as_micros() as f64 / 1000.0);
|
||||
|
||||
let improvement = ((first.as_micros() as f64 - avg_rest.as_micros() as f64) / first.as_micros() as f64) * 100.0;
|
||||
|
||||
if improvement > 20.0 {
|
||||
println!("\n✅ Connection reuse is working! ({:.0}% faster)", improvement);
|
||||
} else if improvement > 5.0 {
|
||||
println!("\n⚠️ Some connection reuse, but not optimal ({:.0}% improvement)", improvement);
|
||||
} else {
|
||||
println!("\n❌ Connection reuse NOT working (only {:.0}% improvement)", improvement);
|
||||
println!(" Expected: 30-50% improvement on subsequent requests");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
use polyfill_rs::ClobClient;
|
||||
use std::time::Instant;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
println!("Connection Pre-warming Test");
|
||||
println!("===========================\n");
|
||||
|
||||
let api_key = std::env::var("POLYMARKET_API_KEY")?;
|
||||
let secret = std::env::var("POLYMARKET_SECRET")?;
|
||||
let passphrase = std::env::var("POLYMARKET_PASSPHRASE")?;
|
||||
|
||||
let api_creds = polyfill_rs::ApiCredentials {
|
||||
api_key,
|
||||
secret,
|
||||
passphrase,
|
||||
};
|
||||
|
||||
let mut client = ClobClient::new("https://clob.polymarket.com");
|
||||
client.set_api_creds(api_creds);
|
||||
|
||||
println!("Phase 1: Cold start (no pre-warming)");
|
||||
println!("=====================================");
|
||||
|
||||
// Make 3 requests cold
|
||||
let mut cold_times = Vec::new();
|
||||
for i in 1..=3 {
|
||||
let start = Instant::now();
|
||||
let response = client.http_client
|
||||
.get(format!("{}/simplified-markets?next_cursor=MA==", client.base_url))
|
||||
.send()
|
||||
.await?;
|
||||
let _json: serde_json::Value = response.json().await?;
|
||||
let elapsed = start.elapsed();
|
||||
cold_times.push(elapsed);
|
||||
println!("Request {}: {:.1} ms", i, elapsed.as_micros() as f64 / 1000.0);
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
}
|
||||
|
||||
println!("\nPhase 2: Pre-warmed connection");
|
||||
println!("================================");
|
||||
|
||||
// Pre-warm by making several requests
|
||||
println!("Pre-warming with 5 requests...");
|
||||
for _ in 0..5 {
|
||||
let _ = client.http_client
|
||||
.get(format!("{}/simplified-markets?next_cursor=MA==", client.base_url))
|
||||
.send()
|
||||
.await?
|
||||
.bytes()
|
||||
.await?;
|
||||
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
|
||||
}
|
||||
|
||||
println!("Testing with warmed connection...\n");
|
||||
|
||||
// Now test with warmed connection
|
||||
let mut warm_times = Vec::new();
|
||||
for i in 1..=20 {
|
||||
let start = Instant::now();
|
||||
let response = client.http_client
|
||||
.get(format!("{}/simplified-markets?next_cursor=MA==", client.base_url))
|
||||
.send()
|
||||
.await?;
|
||||
let _json: serde_json::Value = response.json().await?;
|
||||
let elapsed = start.elapsed();
|
||||
warm_times.push(elapsed);
|
||||
|
||||
if i <= 5 {
|
||||
println!("Request {}: {:.1} ms", i, elapsed.as_micros() as f64 / 1000.0);
|
||||
}
|
||||
|
||||
// Minimal delay
|
||||
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
|
||||
}
|
||||
|
||||
// Statistics
|
||||
fn calc_stats(times: &[std::time::Duration]) -> (f64, f64, f64, f64) {
|
||||
let values: Vec<f64> = times.iter().map(|d| d.as_micros() as f64 / 1000.0).collect();
|
||||
let mean = values.iter().sum::<f64>() / values.len() as f64;
|
||||
let variance = values.iter().map(|v| (v - mean).powi(2)).sum::<f64>() / values.len() as f64;
|
||||
let std_dev = variance.sqrt();
|
||||
let mut sorted = values.clone();
|
||||
sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
||||
(mean, std_dev, sorted[0], sorted[sorted.len() - 1])
|
||||
}
|
||||
|
||||
let (cold_mean, cold_std, cold_min, cold_max) = calc_stats(&cold_times);
|
||||
let (warm_mean, warm_std, warm_min, warm_max) = calc_stats(&warm_times);
|
||||
|
||||
println!("\n\nResults:");
|
||||
println!("========\n");
|
||||
|
||||
println!("Cold Start:");
|
||||
println!(" Mean: {:.1} ms ± {:.1} ms", cold_mean, cold_std);
|
||||
println!(" Range: {:.1} - {:.1} ms\n", cold_min, cold_max);
|
||||
|
||||
println!("Pre-warmed:");
|
||||
println!(" Mean: {:.1} ms ± {:.1} ms", warm_mean, warm_std);
|
||||
println!(" Range: {:.1} - {:.1} ms", warm_min, warm_max);
|
||||
|
||||
let improvement = ((cold_mean - warm_mean) / cold_mean) * 100.0;
|
||||
let variance_reduction = ((cold_std - warm_std) / cold_std) * 100.0;
|
||||
|
||||
println!("\nImprovement:");
|
||||
println!(" Speed: {:.1}% faster", improvement);
|
||||
println!(" Variance: {:.1}% more consistent", variance_reduction);
|
||||
|
||||
if warm_std < 30.0 {
|
||||
println!("\nSUCCESS: Achieved target variance (±{:.1}ms < ±30ms)", warm_std);
|
||||
} else {
|
||||
println!("\nStill need work: Current ±{:.1}ms, target ±30ms", warm_std);
|
||||
println!("Remaining variance is likely server-side or network conditions");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
use polyfill_rs::decode::fast_parse;
|
||||
use std::time::Instant;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
println!("SIMD JSON Parsing Benchmark");
|
||||
println!("============================\n");
|
||||
|
||||
// Fetch real data to parse
|
||||
let client = reqwest::Client::new();
|
||||
let response = client
|
||||
.get("https://clob.polymarket.com/simplified-markets?next_cursor=MA==")
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
let data = response.bytes().await?;
|
||||
println!("Response size: {} KB\n", data.len() / 1024);
|
||||
|
||||
// Test 1: Standard serde_json
|
||||
println!("Test 1: Standard serde_json");
|
||||
println!("----------------------------");
|
||||
let mut serde_times = Vec::new();
|
||||
|
||||
for i in 1..=10 {
|
||||
let data_copy = data.clone();
|
||||
let start = Instant::now();
|
||||
let _json: serde_json::Value = serde_json::from_slice(&data_copy)?;
|
||||
let elapsed = start.elapsed();
|
||||
serde_times.push(elapsed);
|
||||
|
||||
if i <= 3 {
|
||||
println!(" Run {}: {:.2} ms", i, elapsed.as_micros() as f64 / 1000.0);
|
||||
}
|
||||
}
|
||||
|
||||
// Test 2: SIMD JSON
|
||||
println!("\nTest 2: SIMD JSON (simd-json)");
|
||||
println!("------------------------------");
|
||||
let mut simd_times = Vec::new();
|
||||
|
||||
for i in 1..=10 {
|
||||
let mut data_copy = data.to_vec();
|
||||
let start = Instant::now();
|
||||
let _json: serde_json::Value = fast_parse::parse_json_fast(&mut data_copy)?;
|
||||
let elapsed = start.elapsed();
|
||||
simd_times.push(elapsed);
|
||||
|
||||
if i <= 3 {
|
||||
println!(" Run {}: {:.2} ms", i, elapsed.as_micros() as f64 / 1000.0);
|
||||
}
|
||||
}
|
||||
|
||||
// Statistics
|
||||
let serde_avg = serde_times.iter().sum::<std::time::Duration>().as_micros() as f64 / serde_times.len() as f64 / 1000.0;
|
||||
let simd_avg = simd_times.iter().sum::<std::time::Duration>().as_micros() as f64 / simd_times.len() as f64 / 1000.0;
|
||||
|
||||
println!("\n\nResults:");
|
||||
println!("========");
|
||||
println!("serde_json: {:.2} ms", serde_avg);
|
||||
println!("simd-json: {:.2} ms", simd_avg);
|
||||
|
||||
let speedup = serde_avg / simd_avg;
|
||||
let improvement = ((serde_avg - simd_avg) / serde_avg) * 100.0;
|
||||
|
||||
println!("\nSpeedup: {:.2}x ({:.1}% faster)", speedup, improvement);
|
||||
println!("Time saved per request: {:.2} ms", serde_avg - simd_avg);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user