koru-lambda-core 1.2.0

A minimal axiomatic system for distributed computation
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use koru_lambda_core::{
    Canonicalizable, Distinction, DistinctionEngine, LocalCausalAgent, NetworkAgent,
    ParallelAction, ParallelBatchProcessor, ParallelSynthesizer, PeerIdentity, ProcessingStrategy,
    StructuralCompactor, TransactionAction, TransactionBatch,
};
use std::sync::Arc;

/// Helper: Propose and finalize batch using two-stage commitment protocol
fn propose_and_finalize_batch(
    agent: &mut NetworkAgent,
    batch: TransactionBatch,
    engine: &Arc<DistinctionEngine>,
) -> Result<Distinction, String> {
    let commitment = agent.propose_commitment(batch.clone(), engine)?;
    agent.finalize_batch(batch, commitment.commitment_hash, engine)
}

/// Benchmark: Core synthesis operations
///
/// Measures raw distinction synthesis throughput.
/// Target: 100,000+ synthesize ops/second
fn bench_core_synthesis(c: &mut Criterion) {
    let mut group = c.benchmark_group("core_synthesis");

    for size in [100, 1_000, 10_000].iter() {
        group.throughput(Throughput::Elements(*size as u64));
        group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| {
            let engine = Arc::new(DistinctionEngine::new());

            b.iter(|| {
                let d0 = engine.d0().clone();
                let d1 = engine.d1().clone();

                let mut current = engine.synthesize(&d0, &d1);

                for i in 0..size {
                    let byte = ((i % 256) as u8).to_canonical_structure(&engine);
                    current = engine.synthesize(&current, &byte);
                }

                black_box(current)
            });
        });
    }

    group.finish();
}

/// Benchmark: Transaction validation throughput
///
/// Measures batch validation performance.
/// Target: 10,000+ batches/second (100k+ tx/s with 10 tx/batch)
fn bench_transaction_validation(c: &mut Criterion) {
    let mut group = c.benchmark_group("transaction_validation");

    for batch_size in [10, 50, 100].iter() {
        group.throughput(Throughput::Elements(*batch_size as u64));
        group.bench_with_input(
            BenchmarkId::from_parameter(batch_size),
            batch_size,
            |b, &batch_size| {
                let engine = Arc::new(DistinctionEngine::new());
                let mut agent = NetworkAgent::new(&engine);

                // Bootstrap with validators
                for i in 0..5 {
                    let peer = PeerIdentity::new(format!("validator_{}", i), &engine);
                    agent.join_peer(peer, &engine);
                }

                b.iter(|| {
                    let transactions: Vec<TransactionAction> = (0..batch_size)
                        .map(|i| TransactionAction { nonce: i as u64, data: vec![i as u8] })
                        .collect();

                    let batch = TransactionBatch {
                        transactions,
                        previous_root: agent.consensus_state_root().to_string(),
                    };

                    let result = propose_and_finalize_batch(&mut agent, batch, &engine);
                    black_box(result)
                });
            },
        );
    }

    group.finish();
}

/// Benchmark: Leader election performance
///
/// Measures deterministic leader election speed.
/// Should be sub-microsecond (pure hash computation)
fn bench_leader_election(c: &mut Criterion) {
    let mut group = c.benchmark_group("leader_election");

    for num_validators in [5, 10, 20, 50].iter() {
        group.bench_with_input(
            BenchmarkId::from_parameter(num_validators),
            num_validators,
            |b, &num_validators| {
                let engine = Arc::new(DistinctionEngine::new());
                let mut agent = NetworkAgent::new(&engine);

                // Add validators
                for i in 0..num_validators {
                    let peer = PeerIdentity::new(format!("validator_{}", i), &engine);
                    agent.join_peer(peer, &engine);
                }

                b.iter(|| {
                    let leader = agent.get_current_leader();
                    black_box(leader)
                });
            },
        );
    }

    group.finish();
}

/// Benchmark: Structural compaction performance
///
/// Measures compaction throughput on large graphs.
/// Target: <100ms for 10,000 node graph
fn bench_compaction(c: &mut Criterion) {
    let mut group = c.benchmark_group("compaction");
    group.sample_size(10); // Fewer samples for expensive operation

    for graph_size in [1_000, 5_000, 10_000].iter() {
        group.bench_with_input(
            BenchmarkId::from_parameter(graph_size),
            graph_size,
            |b, &graph_size| {
                // Build graph once
                let engine = Arc::new(DistinctionEngine::new());
                let d0 = engine.d0().clone();
                let d1 = engine.d1().clone();

                let mut current = engine.synthesize(&d0, &d1);

                // Create large graph
                for i in 0..graph_size {
                    let byte = ((i % 256) as u8).to_canonical_structure(&engine);
                    current = engine.synthesize(&current, &byte);
                }

                b.iter(|| {
                    let mut compactor = StructuralCompactor::new(&engine);
                    compactor.set_hot_threshold(8);
                    let action = compactor.compact(&engine);
                    black_box(action)
                });
            },
        );
    }

    group.finish();
}

/// Benchmark: Multi-node consensus throughput
///
/// Measures end-to-end distributed consensus performance.
/// Target: 50,000+ tx/s across 5 nodes
fn bench_distributed_consensus(c: &mut Criterion) {
    let mut group = c.benchmark_group("distributed_consensus");
    group.sample_size(10);

    for num_nodes in [3, 5, 7].iter() {
        group.bench_with_input(
            BenchmarkId::from_parameter(num_nodes),
            num_nodes,
            |b, &num_nodes| {
                let engine = Arc::new(DistinctionEngine::new());

                // Setup nodes
                let mut nodes: Vec<NetworkAgent> =
                    (0..num_nodes).map(|_| NetworkAgent::new(&engine)).collect();

                // Bootstrap validators
                let validators: Vec<PeerIdentity> = (0..num_nodes)
                    .map(|i| PeerIdentity::new(format!("node_{}", i), &engine))
                    .collect();

                for node in nodes.iter_mut() {
                    for validator in validators.iter() {
                        node.join_peer(validator.clone(), &engine);
                    }
                }

                b.iter(|| {
                    // Process 10 batches of 10 tx each = 100 tx
                    let mut tx_count = 0;

                    for batch_idx in 0..10 {
                        let transactions: Vec<TransactionAction> = (0..10)
                            .map(|i| TransactionAction {
                                nonce: (batch_idx * 10 + i) as u64,
                                data: vec![batch_idx as u8, i as u8],
                            })
                            .collect();

                        let batch = TransactionBatch {
                            transactions: transactions.clone(),
                            previous_root: nodes[0].consensus_state_root().to_string(),
                        };

                        // All nodes process
                        for node in nodes.iter_mut() {
                            let _ = propose_and_finalize_batch(node, batch.clone(), &engine);
                        }

                        tx_count += transactions.len();

                        // Advance epoch
                        for node in nodes.iter_mut() {
                            node.advance_epoch(&engine);
                        }
                    }

                    black_box(tx_count)
                });
            },
        );
    }

    group.finish();
}

/// Benchmark: Byte canonicalization performance
///
/// Measures primitive data mapping throughput.
/// Target: 1M+ bytes/second
fn bench_byte_canonicalization(c: &mut Criterion) {
    let mut group = c.benchmark_group("byte_canonicalization");

    for size in [100, 1_000, 10_000].iter() {
        group.throughput(Throughput::Bytes(*size as u64));
        group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| {
            let engine = Arc::new(DistinctionEngine::new());
            let data: Vec<u8> = (0..size).map(|i| (i % 256) as u8).collect();

            b.iter(|| {
                for &byte in &data {
                    let d = byte.to_canonical_structure(&engine);
                    black_box(d);
                }
            });
        });
    }

    group.finish();
}

/// Benchmark: Network event synthesis
///
/// Measures network action processing speed.
/// Target: 100,000+ events/second
fn bench_network_events(c: &mut Criterion) {
    let mut group = c.benchmark_group("network_events");

    group.bench_function("peer_join", |b| {
        let engine = Arc::new(DistinctionEngine::new());
        let mut agent = NetworkAgent::new(&engine);

        b.iter(|| {
            let peer = PeerIdentity::new("test_peer".to_string(), &engine);
            agent.join_peer(peer, &engine);
            black_box(&agent);
        });
    });

    group.bench_function("epoch_advance", |b| {
        let engine = Arc::new(DistinctionEngine::new());
        let mut agent = NetworkAgent::new(&engine);

        // Add some validators
        for i in 0..5 {
            let peer = PeerIdentity::new(format!("validator_{}", i), &engine);
            agent.join_peer(peer, &engine);
        }

        b.iter(|| {
            agent.advance_epoch(&engine);
            black_box(&agent);
        });
    });

    group.finish();
}

/// Benchmark: Parallel batch processing
///
/// Measures ParallelBatchProcessor throughput.
/// Target: 100,000+ tx/s with batched operations
fn bench_parallel_batch_processing(c: &mut Criterion) {
    let mut group = c.benchmark_group("parallel_batch_processing");

    for num_batches in [10, 100, 1_000].iter() {
        group.throughput(Throughput::Elements(*num_batches as u64));
        group.bench_with_input(
            BenchmarkId::from_parameter(num_batches),
            num_batches,
            |b, &num_batches| {
                let engine = Arc::new(DistinctionEngine::new());
                let mut processor = ParallelBatchProcessor::new(&engine);

                b.iter(|| {
                    let mut current_root = processor.get_current_root().id().to_string();

                    for i in 0..num_batches {
                        let batch = TransactionBatch {
                            transactions: vec![TransactionAction {
                                nonce: i as u64,
                                data: vec![i as u8],
                            }],
                            previous_root: current_root.clone(),
                        };

                        let action = ParallelAction {
                            batches: vec![batch],
                            strategy: ProcessingStrategy::Sequential,
                        };

                        let new_root = processor.synthesize_action(action, &engine);
                        current_root = new_root.id().to_string();
                    }

                    black_box(processor.batches_processed())
                });
            },
        );
    }

    group.finish();
}

/// Benchmark: Parallel byte canonicalization
///
/// Compares single-threaded vs parallel byte canonicalization.
/// Target: 1M+ bytes/s with parallelism
fn bench_parallel_byte_canonicalization(c: &mut Criterion) {
    let mut group = c.benchmark_group("parallel_byte_canon");

    for size in [1_000, 10_000, 100_000].iter() {
        group.throughput(Throughput::Bytes(*size as u64));

        // Single-threaded baseline
        group.bench_with_input(BenchmarkId::new("sequential", size), size, |b, &size| {
            let engine = Arc::new(DistinctionEngine::new());
            let data: Vec<u8> = (0..size).map(|i| (i % 256) as u8).collect();

            b.iter(|| {
                let results: Vec<_> = data
                    .iter()
                    .map(|&byte| {
                        let d = byte.to_canonical_structure(&engine);
                        d.id().to_string()
                    })
                    .collect();

                black_box(results)
            });
        });

        // Parallel with Rayon
        group.bench_with_input(BenchmarkId::new("parallel", size), size, |b, &size| {
            let engine = Arc::new(DistinctionEngine::new());
            let synthesizer = ParallelSynthesizer::new(engine.clone());
            let data: Vec<u8> = (0..size).map(|i| (i % 256) as u8).collect();

            b.iter(|| {
                let results = synthesizer.canonicalize_bytes_parallel(data.clone());
                black_box(results)
            });
        });
    }

    group.finish();
}

/// Benchmark: Parallel synthesis operations
///
/// Measures parallel synthesis throughput using Rayon.
/// Target: Multi-core speedup over sequential operations
fn bench_parallel_synthesis(c: &mut Criterion) {
    let mut group = c.benchmark_group("parallel_synthesis");

    for num_ops in [100, 1_000, 10_000].iter() {
        group.throughput(Throughput::Elements(*num_ops as u64));

        group.bench_with_input(BenchmarkId::from_parameter(num_ops), num_ops, |b, &num_ops| {
            let engine = Arc::new(DistinctionEngine::new());
            let synthesizer = ParallelSynthesizer::new(engine.clone());

            // Create distinction ID pairs
            let d0_id = engine.d0().id().to_string();
            let d1_id = engine.d1().id().to_string();
            let pairs: Vec<(String, String)> =
                (0..num_ops).map(|_| (d0_id.clone(), d1_id.clone())).collect();

            b.iter(|| {
                let results = synthesizer.synthesize_parallel(pairs.clone());
                black_box(results)
            });
        });
    }

    group.finish();
}

/// Benchmark: Multi-batch parallel processing
///
/// Measures throughput when processing multiple batches per action.
/// Target: 100,000+ tx/s with optimal batching
fn bench_multi_batch_parallel(c: &mut Criterion) {
    let mut group = c.benchmark_group("multi_batch_parallel");
    group.sample_size(10);

    for batches_per_action in [10, 50, 100].iter() {
        group.throughput(Throughput::Elements(*batches_per_action as u64 * 10)); // 10 tx per batch

        group.bench_with_input(
            BenchmarkId::from_parameter(batches_per_action),
            batches_per_action,
            |b, &batches_per_action| {
                let engine = Arc::new(DistinctionEngine::new());
                let mut processor = ParallelBatchProcessor::new(&engine);

                b.iter(|| {
                    let initial_root = processor.get_current_root().id().to_string();
                    let current_root = initial_root.clone();

                    // Create batches
                    let batches: Vec<TransactionBatch> = (0..batches_per_action)
                        .map(|batch_idx| {
                            let transactions: Vec<TransactionAction> = (0..10)
                                .map(|tx_idx| TransactionAction {
                                    nonce: (batch_idx * 10 + tx_idx) as u64,
                                    data: vec![batch_idx as u8, tx_idx as u8],
                                })
                                .collect();

                            TransactionBatch {
                                transactions,
                                previous_root: if batch_idx == 0 {
                                    current_root.clone()
                                } else {
                                    String::new() // Will be updated
                                },
                            }
                        })
                        .collect();

                    let action =
                        ParallelAction { batches, strategy: ProcessingStrategy::Sequential };

                    let _new_root = processor.synthesize_action(action, &engine);

                    black_box(processor.batches_processed())
                });
            },
        );
    }

    group.finish();
}

criterion_group!(
    benches,
    bench_core_synthesis,
    bench_transaction_validation,
    bench_leader_election,
    bench_compaction,
    bench_distributed_consensus,
    bench_byte_canonicalization,
    bench_network_events,
    bench_parallel_batch_processing,
    bench_parallel_byte_canonicalization,
    bench_parallel_synthesis,
    bench_multi_batch_parallel,
);

criterion_main!(benches);