mielin-cli 0.1.0-rc.1

Command-line interface and control plane for MielinOS distributed agent mesh
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use mielin_cli::config::Config;
use mielin_cli::error::CliError;
use mielin_cli::{AgentInfo, NodeInfo, OperationResult};
use serde_json::Value;

/// Benchmark configuration loading and parsing
fn bench_config_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("config");

    group.bench_function("create_default_config", |b| {
        b.iter(|| {
            black_box(Config::default());
        });
    });

    group.bench_function("serialize_config_toml", |b| {
        let config = Config::default();
        b.iter(|| {
            black_box(toml::to_string(&config).unwrap());
        });
    });

    group.bench_function("deserialize_config_toml", |b| {
        let config_str = toml::to_string(&Config::default()).unwrap();
        b.iter(|| {
            black_box(toml::from_str::<Config>(&config_str).unwrap());
        });
    });

    group.bench_function("get_config_value", |b| {
        let config = Config::default();
        b.iter(|| {
            black_box(config.get("node.id"));
        });
    });

    group.bench_function("set_config_value", |b| {
        b.iter(|| {
            let mut config = Config::default();
            config.set("node.id", "bench-node").unwrap();
            black_box(());
        });
    });

    group.finish();
}

/// Benchmark output formatting for different formats
fn bench_output_formatting(c: &mut Criterion) {
    let mut group = c.benchmark_group("output_formatting");

    // Create sample data
    let node_info = NodeInfo {
        id: "node-001".to_string(),
        role: "core".to_string(),
        state: "Running".to_string(),
        address: "0.0.0.0:7000".to_string(),
        agents: 10,
        uptime: "1h".to_string(),
        version: "0.1.0".to_string(),
    };

    let nodes: Vec<NodeInfo> = (0..10)
        .map(|i| NodeInfo {
            id: format!("node-{:03}", i),
            role: if i % 3 == 0 {
                "core"
            } else if i % 3 == 1 {
                "relay"
            } else {
                "edge"
            }
            .to_string(),
            state: "Running".to_string(),
            address: format!("0.0.0.0:700{}", i),
            agents: i * 5,
            uptime: format!("{}h", i + 1),
            version: "0.1.0".to_string(),
        })
        .collect();

    group.bench_function("format_single_node_json", |b| {
        b.iter(|| {
            black_box(serde_json::to_string(&node_info).unwrap());
        });
    });

    group.bench_function("format_single_node_yaml", |b| {
        b.iter(|| {
            black_box(serde_yaml::to_string(&node_info).unwrap());
        });
    });

    group.bench_function("format_10_nodes_json", |b| {
        b.iter(|| {
            black_box(serde_json::to_string(&nodes).unwrap());
        });
    });

    group.bench_function("format_10_nodes_yaml", |b| {
        b.iter(|| {
            black_box(serde_yaml::to_string(&nodes).unwrap());
        });
    });

    // Benchmark pretty printing
    group.bench_function("format_10_nodes_json_pretty", |b| {
        b.iter(|| {
            black_box(serde_json::to_string_pretty(&nodes).unwrap());
        });
    });

    group.finish();
}

/// Benchmark agent operations
fn bench_agent_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("agent_operations");

    let agent_info = AgentInfo {
        id: "agent-001-xxxxxxxxxx".to_string(),
        state: "Running".to_string(),
        node: "node-001".to_string(),
        dna_hash: "abc123def456".to_string(),
        memory_mb: 128.5,
        uptime: "30m".to_string(),
    };

    let agents: Vec<AgentInfo> = (0..100)
        .map(|i| AgentInfo {
            id: format!("agent-{:03}-xxxxxxxxxx", i),
            state: if i % 5 == 0 { "Running" } else { "Paused" }.to_string(),
            node: format!("node-{:03}", i % 10),
            dna_hash: format!("hash{:010}", i),
            memory_mb: 64.0 + (i as f64) * 2.0,
            uptime: format!("{}m", i),
        })
        .collect();

    group.bench_function("serialize_agent_json", |b| {
        b.iter(|| {
            black_box(serde_json::to_string(&agent_info).unwrap());
        });
    });

    group.bench_function("serialize_100_agents_json", |b| {
        b.iter(|| {
            black_box(serde_json::to_string(&agents).unwrap());
        });
    });

    group.bench_function("filter_agents_by_state", |b| {
        b.iter(|| {
            let filtered: Vec<_> = agents.iter().filter(|a| a.state == "Running").collect();
            black_box(filtered);
        });
    });

    group.bench_function("filter_agents_by_node", |b| {
        b.iter(|| {
            let filtered: Vec<_> = agents.iter().filter(|a| a.node == "node-001").collect();
            black_box(filtered);
        });
    });

    group.finish();
}

/// Benchmark error handling
fn bench_error_handling(c: &mut Criterion) {
    let mut group = c.benchmark_group("error_handling");

    group.bench_function("create_cli_error", |b| {
        b.iter(|| {
            black_box(CliError::Connection("Connection failed".to_string()));
        });
    });

    group.bench_function("format_error_message", |b| {
        let error = CliError::Connection("Connection failed".to_string());
        b.iter(|| {
            black_box(format!("{}", error));
        });
    });

    group.bench_function("create_and_format_error", |b| {
        b.iter(|| {
            let error = CliError::Connection("Connection failed".to_string());
            black_box(format!("{}", error));
        });
    });

    group.finish();
}

/// Benchmark JSON parsing and manipulation
fn bench_json_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("json_operations");

    let small_json = r#"{"id": "node-001", "state": "Running"}"#;
    let large_json = serde_json::to_string(&vec![
        serde_json::json!({
            "id": format!("node-{:03}", 0),
            "role": "core",
            "state": "Running",
            "agents": (0..50).map(|i| format!("agent-{:03}", i)).collect::<Vec<_>>()
        });
        100
    ])
    .unwrap();

    group.bench_function("parse_small_json", |b| {
        b.iter(|| {
            black_box(serde_json::from_str::<Value>(small_json).unwrap());
        });
    });

    group.bench_function("parse_large_json", |b| {
        b.iter(|| {
            black_box(serde_json::from_str::<Value>(&large_json).unwrap());
        });
    });

    group.bench_function("stringify_small_json", |b| {
        let value = serde_json::from_str::<Value>(small_json).unwrap();
        b.iter(|| {
            black_box(serde_json::to_string(&value).unwrap());
        });
    });

    group.bench_function("stringify_large_json", |b| {
        let value = serde_json::from_str::<Value>(&large_json).unwrap();
        b.iter(|| {
            black_box(serde_json::to_string(&value).unwrap());
        });
    });

    group.bench_function("pretty_print_large_json", |b| {
        let value = serde_json::from_str::<Value>(&large_json).unwrap();
        b.iter(|| {
            black_box(serde_json::to_string_pretty(&value).unwrap());
        });
    });

    group.finish();
}

/// Benchmark table formatting
fn bench_table_formatting(c: &mut Criterion) {
    let mut group = c.benchmark_group("table_formatting");

    let nodes: Vec<NodeInfo> = (0..50)
        .map(|i| NodeInfo {
            id: format!("node-{:03}", i),
            role: if i % 3 == 0 {
                "core"
            } else if i % 3 == 1 {
                "relay"
            } else {
                "edge"
            }
            .to_string(),
            state: "Running".to_string(),
            address: format!("0.0.0.0:700{}", i),
            agents: i * 5,
            uptime: format!("{}h", i + 1),
            version: "0.1.0".to_string(),
        })
        .collect();

    group.bench_function("format_50_nodes_table", |b| {
        b.iter(|| {
            use tabled::{Table, Tabled};
            #[derive(Tabled)]
            struct NodeRow {
                id: String,
                role: String,
                state: String,
                address: String,
                agents: String,
            }

            let rows: Vec<NodeRow> = nodes
                .iter()
                .map(|n| NodeRow {
                    id: n.id.clone(),
                    role: n.role.clone(),
                    state: n.state.clone(),
                    address: n.address.clone(),
                    agents: n.agents.to_string(),
                })
                .collect();

            black_box(Table::new(rows).to_string());
        });
    });

    group.finish();
}

/// Benchmark operation result tracking
fn bench_operation_result(c: &mut Criterion) {
    let mut group = c.benchmark_group("operation_result");

    group.bench_function("create_operation_result", |b| {
        b.iter(|| {
            black_box(OperationResult {
                success: true,
                message: "Deploying agent".to_string(),
                id: Some("op-001".to_string()),
            });
        });
    });

    group.bench_function("serialize_operation_result", |b| {
        let result = OperationResult {
            success: true,
            message: "Deploying agent".to_string(),
            id: Some("op-001".to_string()),
        };
        b.iter(|| {
            black_box(serde_json::to_string(&result).unwrap());
        });
    });

    group.finish();
}

/// Benchmark plugin operations
fn bench_plugin_operations(c: &mut Criterion) {
    use mielin_cli::{PluginCommand, PluginManager, PluginMetadata};

    let mut group = c.benchmark_group("plugin");

    group.bench_function("create_plugin_manager", |b| {
        b.iter(|| {
            black_box(PluginManager::new().unwrap());
        });
    });

    group.bench_function("serialize_plugin_metadata", |b| {
        let metadata = PluginMetadata {
            name: "test-plugin".to_string(),
            version: "1.0.0".to_string(),
            description: "A test plugin".to_string(),
            author: "Test Author".to_string(),
            license: "MIT".to_string(),
            commands: vec![PluginCommand {
                name: "hello".to_string(),
                description: "Say hello".to_string(),
                aliases: vec!["hi".to_string()],
                arguments: vec![],
            }],
            dependencies: vec![],
            min_version: Some("0.1.0".to_string()),
        };

        b.iter(|| {
            black_box(toml::to_string(&metadata).unwrap());
        });
    });

    group.finish();
}

/// Benchmark script operations
fn bench_script_operations(c: &mut Criterion) {
    use mielin_cli::{ScriptEngine, ScriptMetadata};

    let mut group = c.benchmark_group("script");

    group.bench_function("create_script_engine", |b| {
        b.iter(|| {
            black_box(ScriptEngine::new().unwrap());
        });
    });

    group.bench_function("serialize_script_metadata", |b| {
        let metadata = ScriptMetadata {
            name: "test-script".to_string(),
            version: "1.0.0".to_string(),
            description: "A test script".to_string(),
            author: Some("Test Author".to_string()),
            required_version: Some("0.1.0".to_string()),
            tags: vec!["test".to_string(), "automation".to_string()],
        };

        b.iter(|| {
            black_box(serde_json::to_string(&metadata).unwrap());
        });
    });

    group.finish();
}

/// Benchmark remote management operations
fn bench_remote_operations(c: &mut Criterion) {
    use mielin_cli::{AuthMethod, ConnectionOptions, RemoteManager, RemoteNode};

    let mut group = c.benchmark_group("remote");

    group.bench_function("create_remote_manager", |b| {
        b.iter(|| {
            black_box(RemoteManager::new().unwrap());
        });
    });

    group.bench_function("serialize_remote_node", |b| {
        let node = RemoteNode {
            id: "node1".to_string(),
            name: "Test Node".to_string(),
            address: "localhost:8080".to_string(),
            auth: AuthMethod::None,
            options: ConnectionOptions::default(),
            tags: vec!["test".to_string(), "dev".to_string()],
            description: "A test node".to_string(),
        };

        b.iter(|| {
            black_box(toml::to_string(&node).unwrap());
        });
    });

    group.finish();
}

criterion_group!(
    benches,
    bench_config_operations,
    bench_output_formatting,
    bench_agent_operations,
    bench_error_handling,
    bench_json_operations,
    bench_table_formatting,
    bench_operation_result,
    bench_plugin_operations,
    bench_script_operations,
    bench_remote_operations,
);

criterion_main!(benches);