zeptoclaw 0.7.3

Ultra-lightweight personal AI assistant
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! ZeptoClaw Load Testing Benchmark
//!
//! Simulates concurrent users to measure:
//! - Memory usage
//! - Response latency
//! - Throughput (messages/second)
//! - CPU utilization
//!
//! Usage:
//!   cargo run --bin benchmark -- --users 100 --duration 30

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use anyhow::Result;
use async_trait::async_trait;
use clap::Parser;
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use tracing::{info, warn};

use zeptoclaw::agent::AgentLoop;
use zeptoclaw::bus::{InboundMessage, MessageBus};
use zeptoclaw::config::Config;
use zeptoclaw::providers::{ChatOptions, LLMProvider, LLMResponse, ToolDefinition};
use zeptoclaw::session::{Message, SessionManager};
use zeptoclaw::tools::EchoTool;

#[derive(Parser)]
#[command(name = "zeptoclaw-benchmark")]
#[command(about = "Load testing benchmark for ZeptoClaw")]
struct Args {
    /// Number of concurrent users to simulate
    #[arg(short, long, default_value = "100")]
    users: usize,

    /// Duration of the benchmark in seconds
    #[arg(short, long, default_value = "30")]
    duration: u64,

    /// Messages per user per second
    #[arg(short, long, default_value = "1.0")]
    rate: f64,

    /// Enable memory profiling
    #[arg(long)]
    memory_profile: bool,

    /// Output format: text, json
    #[arg(short, long, default_value = "text")]
    format: String,
}

/// Statistics collected during benchmark
#[derive(Debug, Default)]
struct Stats {
    messages_sent: AtomicU64,
    messages_received: AtomicU64,
    total_latency_ms: AtomicU64,
    errors: AtomicU64,
}

impl Stats {
    fn new() -> Self {
        Self::default()
    }

    fn record_sent(&self) {
        self.messages_sent.fetch_add(1, Ordering::Relaxed);
    }

    fn record_received(&self, latency_ms: u64) {
        self.messages_received.fetch_add(1, Ordering::Relaxed);
        self.total_latency_ms
            .fetch_add(latency_ms, Ordering::Relaxed);
    }

    fn record_error(&self) {
        self.errors.fetch_add(1, Ordering::Relaxed);
    }

    fn messages_sent(&self) -> u64 {
        self.messages_sent.load(Ordering::Relaxed)
    }

    fn messages_received(&self) -> u64 {
        self.messages_received.load(Ordering::Relaxed)
    }

    fn avg_latency_ms(&self) -> f64 {
        let received = self.messages_received();
        if received == 0 {
            0.0
        } else {
            self.total_latency_ms.load(Ordering::Relaxed) as f64 / received as f64
        }
    }

    fn error_rate(&self) -> f64 {
        let sent = self.messages_sent();
        if sent == 0 {
            0.0
        } else {
            self.errors.load(Ordering::Relaxed) as f64 / sent as f64 * 100.0
        }
    }
}

/// Mock LLM provider for benchmarking - just echoes back the last user message
pub struct MockProvider;

#[async_trait]
impl LLMProvider for MockProvider {
    async fn chat(
        &self,
        messages: Vec<Message>,
        _tools: Vec<ToolDefinition>,
        _model: Option<&str>,
        _options: ChatOptions,
    ) -> std::result::Result<LLMResponse, zeptoclaw::error::ZeptoError> {
        // Find the last user message and echo it back
        let response_text = messages
            .iter()
            .rev()
            .find(|m| matches!(m.role, zeptoclaw::session::Role::User))
            .map(|m| format!("Echo: {}", m.content))
            .unwrap_or_else(|| "Echo: Hello".to_string());

        Ok(LLMResponse::text(&response_text))
    }

    fn default_model(&self) -> &str {
        "mock"
    }

    fn name(&self) -> &str {
        "mock"
    }
}

/// Simulates a single user sending messages
async fn simulate_user(
    user_id: usize,
    bus: Arc<MessageBus>,
    rate: f64,
    duration: Duration,
    stats: Arc<Stats>,
) -> Result<()> {
    let interval = Duration::from_secs_f64(1.0 / rate);
    let chat_id = format!("user_{}", user_id);

    let start = Instant::now();
    let mut message_id = 0u64;

    while start.elapsed() < duration {
        // Create and send message
        let msg = InboundMessage::new(
            "benchmark",
            &chat_id,
            &chat_id,
            &format!("Message {} from user {}", message_id, user_id),
        );

        if let Err(e) = bus.publish_inbound(msg).await {
            warn!(user = user_id, error = %e, "Failed to send message");
            stats.record_error();
        } else {
            stats.record_sent();
        }

        message_id += 1;

        // Wait for interval
        tokio::time::sleep(interval).await;
    }

    Ok(())
}

/// Collects responses from the outbound channel
async fn collect_responses(
    bus: Arc<MessageBus>,
    stats: Arc<Stats>,
    mut stop: mpsc::Receiver<()>,
) -> Result<()> {
    loop {
        tokio::select! {
            _ = stop.recv() => break,
            msg = bus.consume_outbound() => {
                if msg.is_some() {
                    stats.record_received(0);
                }
            }
        }
    }

    Ok(())
}

/// Prints memory statistics
fn print_memory_stats() {
    #[cfg(target_os = "linux")]
    {
        use std::fs;
        if let Ok(status) = fs::read_to_string("/proc/self/status") {
            for line in status.lines() {
                if line.starts_with("VmRSS:") || line.starts_with("VmSize:") {
                    println!("  {}", line.trim());
                }
            }
        }
    }

    #[cfg(target_os = "macos")]
    {
        let output = std::process::Command::new("ps")
            .args(["-o", "rss=", "-p", &format!("{}", std::process::id())])
            .output();

        if let Ok(output) = output {
            let rss_kb = String::from_utf8_lossy(&output.stdout)
                .trim()
                .parse::<usize>()
                .unwrap_or(0);
            println!("  RSS Memory: {} KB ({} MB)", rss_kb, rss_kb / 1024);
        }
    }
}

#[derive(Debug)]
struct BenchmarkResult {
    users: usize,
    duration_secs: f64,
    messages_sent: u64,
    messages_received: u64,
    avg_latency_ms: f64,
    error_rate: f64,
    throughput: f64,
}

async fn run_benchmark(args: &Args) -> Result<BenchmarkResult> {
    let start_time = Instant::now();
    let duration = Duration::from_secs(args.duration);

    info!(
        users = args.users,
        duration = args.duration,
        rate = args.rate,
        "Starting benchmark"
    );

    // Setup
    let config = Config::default();
    let bus = Arc::new(MessageBus::new());
    let session_manager = SessionManager::new_memory();
    let agent = Arc::new(AgentLoop::new(config, session_manager, bus.clone()));

    // Register mock provider and echo tool
    agent.set_provider(Box::new(MockProvider)).await;
    agent.register_tool(Box::new(EchoTool)).await;

    let stats = Arc::new(Stats::new());
    let (stop_tx, stop_rx) = mpsc::channel(1);

    // Start agent
    let agent_clone = Arc::clone(&agent);
    let agent_handle: JoinHandle<Result<()>> =
        tokio::spawn(async move { agent_clone.start().await.map_err(|e| e.into()) });

    // Start response collector
    let stats_clone = Arc::clone(&stats);
    let bus_clone = Arc::clone(&bus);
    let collector_handle =
        tokio::spawn(async move { collect_responses(bus_clone, stats_clone, stop_rx).await });

    // Give agent time to start
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Print initial memory
    if args.memory_profile {
        println!("Initial memory:");
        print_memory_stats();
    }

    // Spawn user simulations
    let mut user_handles = Vec::new();
    let rate = args.rate;
    for user_id in 0..args.users {
        let bus_clone = Arc::clone(&bus);
        let stats_clone = Arc::clone(&stats);
        let handle = tokio::spawn(async move {
            simulate_user(user_id, bus_clone, rate, duration, stats_clone).await
        });
        user_handles.push(handle);
    }

    println!("\n🚀 Benchmark running...");
    println!("   Users: {}", args.users);
    println!("   Duration: {}s", args.duration);
    println!("   Rate: {} msg/user/s", args.rate);
    println!(
        "   Expected total: {} messages\n",
        args.users as u64 * args.duration * args.rate as u64
    );

    // Progress reporting
    let progress_handle = {
        let stats = Arc::clone(&stats);
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(Duration::from_secs(5));
            loop {
                interval.tick().await;
                let sent = stats.messages_sent();
                let received = stats.messages_received();
                let avg_latency = stats.avg_latency_ms();
                println!(
                    "  Progress: {} sent, {} received, {:.1}ms avg latency",
                    sent, received, avg_latency
                );
            }
        })
    };

    // Wait for all users to complete
    for handle in user_handles {
        let _ = handle.await;
    }

    // Give time for final messages to process
    tokio::time::sleep(Duration::from_secs(2)).await;

    // Stop collector and progress reporter
    let _ = stop_tx.send(()).await;
    let _ = collector_handle.await;
    progress_handle.abort();

    // Stop agent
    agent.stop();
    let _ = agent_handle.await;

    // Final memory stats
    if args.memory_profile {
        println!("\nFinal memory:");
        print_memory_stats();
    }

    let elapsed = start_time.elapsed();
    let result = BenchmarkResult {
        users: args.users,
        duration_secs: elapsed.as_secs_f64(),
        messages_sent: stats.messages_sent(),
        messages_received: stats.messages_received(),
        avg_latency_ms: stats.avg_latency_ms(),
        error_rate: stats.error_rate(),
        throughput: stats.messages_sent() as f64 / elapsed.as_secs_f64(),
    };

    Ok(result)
}

fn print_results(result: &BenchmarkResult) {
    println!("\n{}", "=".repeat(60));
    println!("📊 BENCHMARK RESULTS");
    println!("{}", "=".repeat(60));
    println!("  Concurrent Users:     {}", result.users);
    println!("  Duration:             {:.2}s", result.duration_secs);
    println!("  Messages Sent:        {}", result.messages_sent);
    println!("  Messages Received:    {}", result.messages_received);
    println!("  Throughput:           {:.1} msg/s", result.throughput);
    println!("  Avg Latency:          {:.2}ms", result.avg_latency_ms);
    println!("  Error Rate:           {:.2}%", result.error_rate);
    println!(
        "  msgs/user/s:          {:.2}",
        result.throughput / result.users as f64
    );
    println!("{}", "=".repeat(60));
}

fn print_results_json(result: &BenchmarkResult) {
    let json = serde_json::json!({
        "users": result.users,
        "duration_secs": result.duration_secs,
        "messages_sent": result.messages_sent,
        "messages_received": result.messages_received,
        "throughput": result.throughput,
        "avg_latency_ms": result.avg_latency_ms,
        "error_rate": result.error_rate,
    });
    println!("{}", serde_json::to_string_pretty(&json).unwrap());
}

#[tokio::main]
async fn main() -> Result<()> {
    tracing_subscriber::fmt().with_env_filter("warn").init();

    let args = Args::parse();

    let result = run_benchmark(&args).await?;

    match args.format.as_str() {
        "json" => print_results_json(&result),
        _ => print_results(&result),
    }

    Ok(())
}