use ai_lib_rust::batch::{BatchCollector, BatchConfig, BatchExecutor, BatchItem, BatchStrategy};
use std::time::Duration;
#[tokio::main]
async fn main() {
println!("=== AI-Lib Batch Processing Demo ===\n");
demo_batch_collector();
demo_batch_configuration();
demo_batch_executor();
demo_batch_strategies();
}
fn demo_batch_collector() {
println!("--- Example 1: Basic Batch Collection ---\n");
let config = BatchConfig::new().with_max_batch_size(5);
let collector: BatchCollector<String> = BatchCollector::new(config);
println!("BatchCollector created with max size: 5\n");
let requests = vec![
"Generate summary for doc1",
"Generate summary for doc2",
"Generate summary for doc3",
"Generate summary for doc4",
"Generate summary for doc5",
];
for (i, request) in requests.iter().enumerate() {
let result = collector.add_data(request.to_string());
println!("Added request {}: {:?}", i + 1, result);
if collector.should_flush() {
println!("\n -> Batch is ready to flush!");
let items = collector.drain();
println!(" -> Drained {} items", items.len());
for item in &items {
println!(" Processing: {}", item.data);
}
}
}
println!();
}
fn demo_batch_configuration() {
println!("--- Example 2: Batch Configuration ---\n");
let configs = vec![
(
"Small batch, quick flush",
BatchConfig::new()
.with_max_batch_size(3)
.with_auto_flush(true),
),
(
"Large batch, no auto-flush",
BatchConfig::new()
.with_max_batch_size(100)
.with_auto_flush(false),
),
];
for (name, config) in configs {
println!("Config: {}", name);
println!(" - Max batch size: {}", config.max_batch_size);
println!(" - Auto flush: {}", config.auto_flush);
println!();
}
}
fn demo_batch_executor() {
println!("--- Example 3: Batch Executor ---\n");
let executor = BatchExecutor::new();
println!("BatchExecutor created\n");
let items = vec![
BatchItem::new("High priority request")
.with_priority(10)
.with_request_id("req-001"),
BatchItem::new("Normal priority request")
.with_priority(5)
.with_request_id("req-002"),
BatchItem::new("Low priority request")
.with_priority(1)
.with_request_id("req-003"),
];
println!("Created batch items with different priorities:");
for item in &items {
println!(
" - {:?}: priority={}, request_id={:?}",
item.data, item.priority, item.request_id
);
}
println!("\nNote: In production, executor would process these items");
println!(" according to the configured strategy.\n");
let _ = executor;
}
fn demo_batch_strategies() {
println!("--- Example 4: Batch Strategies ---\n");
let strategies = vec![
("Sequential", BatchStrategy::Sequential),
("Parallel", BatchStrategy::Parallel),
(
"Concurrent (5)",
BatchStrategy::Concurrent { max_concurrency: 5 },
),
];
for (name, strategy) in strategies {
println!("Strategy: {}", name);
match strategy {
BatchStrategy::Sequential => {
println!(" - Processes items one at a time");
println!(" - Preserves order, simpler error handling");
println!(" - Best for: dependent operations, limited concurrency");
}
BatchStrategy::Parallel => {
println!(" - Processes all items concurrently");
println!(" - Maximum throughput, no concurrency limit");
println!(" - Best for: independent operations, high throughput needs");
}
BatchStrategy::Concurrent { max_concurrency } => {
println!(" - Processes up to {} items concurrently", max_concurrency);
println!(" - Controlled parallelism with bounded resources");
println!(" - Best for: rate-limited APIs, resource-constrained environments");
}
}
println!();
}
println!("Time-based Batching:\n");
println!("BatchConfig supports max_wait_time for time-triggered flushes.");
println!("Default: {:?}\n", Duration::from_secs(5));
let config = BatchConfig {
max_batch_size: 10,
max_wait_time: Duration::from_millis(100),
auto_flush: true,
};
println!("Example config for low-latency batching:");
println!(" - max_batch_size: {}", config.max_batch_size);
println!(" - max_wait_time: {:?}", config.max_wait_time);
println!(" - auto_flush: {}", config.auto_flush);
println!("\n=== Best Practices ===\n");
println!("1. Choose batch size based on API limits and memory");
println!("2. Use time-based flush for consistent latency");
println!("3. Consider priority for mixed workloads");
println!("4. Monitor batch fill rates for optimization");
println!("5. Handle partial batch failures gracefully");
}