ringkernel 0.4.2

GPU-native persistent actor model framework - Rust port of DotCompute Ring Kernel
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
//! Hybrid Logical Clock Benchmarks
//!
//! Validates README claims about HLC:
//! - Causal ordering across distributed GPU kernels
//! - Total ordering of timestamps
//! - Bounded drift from real time
//!
//! These benchmarks measure HLC performance critical for message ordering.

use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use std::sync::Arc;
use std::thread;
use std::time::Instant;

use ringkernel_core::hlc::{HlcClock, HlcState, HlcTimestamp};

/// Benchmark timestamp creation
fn bench_timestamp_creation(c: &mut Criterion) {
    let mut group = c.benchmark_group("hlc/timestamp");

    group.bench_function("now", |b| {
        b.iter(|| {
            let ts = HlcTimestamp::now(1);
            black_box(ts);
        });
    });

    group.bench_function("new_const", |b| {
        b.iter(|| {
            let ts = HlcTimestamp::new(12345678901234, 42, 1);
            black_box(ts);
        });
    });

    group.bench_function("zero", |b| {
        b.iter(|| {
            let ts = HlcTimestamp::zero();
            black_box(ts);
        });
    });

    group.finish();
}

/// Benchmark clock operations
fn bench_clock_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("hlc/clock");

    group.bench_function("create", |b| {
        b.iter(|| {
            let clock = HlcClock::new(1);
            black_box(clock);
        });
    });

    group.bench_function("tick", |b| {
        let clock = HlcClock::new(1);

        b.iter(|| {
            let ts = clock.tick();
            black_box(ts);
        });
    });

    group.bench_function("now_read", |b| {
        let clock = HlcClock::new(1);

        b.iter(|| {
            let ts = clock.now();
            black_box(ts);
        });
    });

    // Update from received message
    group.bench_function("update", |b| {
        let clock = HlcClock::new(1);
        let received = HlcTimestamp::now(2);

        b.iter(|| {
            let result = clock.update(&received);
            let _ = black_box(result);
        });
    });

    group.finish();
}

/// Benchmark timestamp comparison (ordering)
fn bench_timestamp_ordering(c: &mut Criterion) {
    let mut group = c.benchmark_group("hlc/ordering");

    group.bench_function("compare_timestamps", |b| {
        let ts1 = HlcTimestamp::new(100, 0, 1);
        let ts2 = HlcTimestamp::new(100, 1, 1);

        b.iter(|| {
            black_box(ts1 < ts2);
            black_box(ts1 > ts2);
            black_box(ts1 == ts2);
        });
    });

    group.bench_function("sort_timestamps", |b| {
        let mut timestamps: Vec<HlcTimestamp> = (0..100)
            .map(|i| HlcTimestamp::new(1000 + (i % 10) as u64, i as u64, (i % 3) as u64))
            .collect();

        b.iter(|| {
            timestamps.sort();
            black_box(&timestamps);
        });
    });

    group.bench_function("max_timestamp", |b| {
        let clock = HlcClock::new(1);
        let timestamps: Vec<HlcTimestamp> = (0..100).map(|_| clock.tick()).collect();

        b.iter(|| {
            let max = timestamps.iter().max();
            black_box(max);
        });
    });

    group.finish();
}

/// Benchmark pack/unpack operations
fn bench_pack_unpack(c: &mut Criterion) {
    let mut group = c.benchmark_group("hlc/pack_unpack");

    group.bench_function("pack", |b| {
        let ts = HlcTimestamp::now(1);

        b.iter(|| {
            let packed = ts.pack();
            black_box(packed);
        });
    });

    group.bench_function("unpack", |b| {
        let ts = HlcTimestamp::now(1);
        let packed = ts.pack();

        b.iter(|| {
            let unpacked = HlcTimestamp::unpack(packed);
            black_box(unpacked);
        });
    });

    group.bench_function("roundtrip", |b| {
        let ts = HlcTimestamp::now(1);

        b.iter(|| {
            let packed = ts.pack();
            let unpacked = HlcTimestamp::unpack(packed);
            black_box(unpacked);
        });
    });

    group.finish();
}

/// Benchmark concurrent HLC operations
fn bench_concurrent_hlc(c: &mut Criterion) {
    let mut group = c.benchmark_group("hlc/concurrent");

    group.sample_size(50);
    group.measurement_time(std::time::Duration::from_secs(10));

    // Single clock, multiple tick calls
    group.bench_function("single_clock_contention", |b| {
        let clock = Arc::new(HlcClock::new(1));

        b.iter_custom(|iters| {
            let handles: Vec<_> = (0..4)
                .map(|_| {
                    let clock = Arc::clone(&clock);
                    let per_thread = iters / 4;
                    thread::spawn(move || {
                        for _ in 0..per_thread {
                            let ts = clock.tick();
                            black_box(ts);
                        }
                    })
                })
                .collect();

            let start = Instant::now();
            for handle in handles {
                handle.join().unwrap();
            }
            start.elapsed()
        });
    });

    // Multiple clocks simulating distributed nodes
    for num_nodes in [2, 4, 8].iter() {
        group.bench_with_input(
            BenchmarkId::new("distributed_nodes", num_nodes),
            num_nodes,
            |b, &nodes| {
                let clocks: Vec<Arc<HlcClock>> = (0..nodes)
                    .map(|i| Arc::new(HlcClock::new(i as u64)))
                    .collect();

                b.iter(|| {
                    // Each node generates a timestamp
                    let timestamps: Vec<HlcTimestamp> =
                        clocks.iter().map(|clock| clock.tick()).collect();

                    // Simulate message exchange - each node updates from others
                    for (i, clock) in clocks.iter().enumerate() {
                        for (j, ts) in timestamps.iter().enumerate() {
                            if i != j {
                                let _ = clock.update(ts);
                            }
                        }
                    }

                    black_box(&timestamps);
                });
            },
        );
    }

    group.finish();
}

/// Benchmark causal ordering properties
fn bench_causality(c: &mut Criterion) {
    let mut group = c.benchmark_group("hlc/causality");

    // Verify causality is maintained
    group.bench_function("causal_chain", |b| {
        let clock1 = HlcClock::new(1);
        let clock2 = HlcClock::new(2);

        b.iter(|| {
            // Event A on node 1
            let ts_a = clock1.tick();

            // Message sent to node 2
            let ts_b = clock2.update(&ts_a).unwrap();

            // Verify causality
            assert!(ts_a < ts_b, "Causality must be preserved");

            black_box((ts_a, ts_b));
        });
    });

    // Verify happens-before relationship
    group.bench_function("happens_before_chain", |b| {
        let clocks: Vec<HlcClock> = (0..5).map(|i| HlcClock::new(i as u64)).collect();

        b.iter(|| {
            let mut prev_ts = clocks[0].tick();

            for clock in clocks.iter().skip(1) {
                let new_ts = clock.update(&prev_ts).unwrap();
                assert!(prev_ts < new_ts, "Happens-before must be maintained");
                prev_ts = new_ts;
            }

            black_box(prev_ts);
        });
    });

    group.finish();
}

/// Benchmark HLC state operations (GPU-side compact state)
fn bench_hlc_state(c: &mut Criterion) {
    let mut group = c.benchmark_group("hlc/state");

    group.bench_function("create", |b| {
        b.iter(|| {
            let state = HlcState::new(12345678901234, 42);
            black_box(state);
        });
    });

    group.bench_function("to_timestamp", |b| {
        let state = HlcState::new(12345678901234, 42);

        b.iter(|| {
            let ts = state.to_timestamp(1);
            black_box(ts);
        });
    });

    group.bench_function("from_timestamp", |b| {
        let ts = HlcTimestamp::now(1);

        b.iter(|| {
            let state = HlcState::from_timestamp(&ts);
            black_box(state);
        });
    });

    group.bench_function("roundtrip", |b| {
        let ts = HlcTimestamp::now(1);

        b.iter(|| {
            let state = HlcState::from_timestamp(&ts);
            let restored = state.to_timestamp(ts.node_id);
            assert_eq!(ts.physical, restored.physical);
            assert_eq!(ts.logical, restored.logical);
            black_box(restored);
        });
    });

    group.finish();
}

/// Benchmark throughput of HLC tick operations
fn bench_hlc_throughput(c: &mut Criterion) {
    let mut group = c.benchmark_group("hlc/throughput");

    for batch_size in [100, 1000, 10000].iter() {
        group.throughput(Throughput::Elements(*batch_size as u64));

        group.bench_with_input(
            BenchmarkId::new("tick_batch", batch_size),
            batch_size,
            |b, &size| {
                let clock = HlcClock::new(1);

                b.iter(|| {
                    for _ in 0..size {
                        let ts = clock.tick();
                        black_box(ts);
                    }
                });
            },
        );
    }

    group.finish();
}

/// Validation benchmark for HLC claims
fn bench_hlc_validation(c: &mut Criterion) {
    let mut group = c.benchmark_group("hlc_validation");

    // Total ordering must be maintained
    group.bench_function("total_ordering_guarantee", |b| {
        let clock = HlcClock::new(1);

        b.iter(|| {
            let ts1 = clock.tick();
            let ts2 = clock.tick();
            let ts3 = clock.tick();

            // Strict ordering must hold
            assert!(ts1 < ts2, "ts1 < ts2 must hold");
            assert!(ts2 < ts3, "ts2 < ts3 must hold");
            assert!(ts1 < ts3, "ts1 < ts3 must hold (transitivity)");

            black_box((ts1, ts2, ts3));
        });
    });

    // Causality across nodes must be preserved
    group.bench_function("causality_guarantee", |b| {
        b.iter(|| {
            let clock_a = HlcClock::new(1);
            let clock_b = HlcClock::new(2);

            // A sends message to B
            let ts_a = clock_a.tick();
            let ts_b = clock_b.update(&ts_a).unwrap();

            // Causality: ts_a -> ts_b means ts_a < ts_b
            assert!(ts_a < ts_b, "Causality must be preserved");

            // B sends message back to A
            let ts_a2 = clock_a.update(&ts_b).unwrap();

            // Full causal chain
            assert!(ts_b < ts_a2, "Return message must be causally after");
            assert!(ts_a < ts_a2, "Original event must be before final event");

            black_box((ts_a, ts_b, ts_a2));
        });
    });

    // Bounded drift (HLC should stay close to wall clock)
    group.bench_function("bounded_drift", |b| {
        let clock = HlcClock::new(1);

        b.iter(|| {
            let wall_before = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_micros() as u64;

            let ts = clock.tick();

            let wall_after = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_micros() as u64;

            // HLC physical should be within wall clock bounds (with some tolerance)
            assert!(
                ts.physical >= wall_before.saturating_sub(1000),
                "HLC should not be too far in the past"
            );
            assert!(
                ts.physical <= wall_after + 1000,
                "HLC should not be too far in the future"
            );

            black_box(ts);
        });
    });

    group.finish();
}

criterion_group!(
    benches,
    bench_timestamp_creation,
    bench_clock_operations,
    bench_timestamp_ordering,
    bench_pack_unpack,
    bench_concurrent_hlc,
    bench_causality,
    bench_hlc_state,
    bench_hlc_throughput,
    bench_hlc_validation,
);
criterion_main!(benches);