asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
//! ATP benchmark profiles for performance testing and comparison.

use crate::atp::benchmark::{BenchmarkConfig, BenchmarkError, BenchmarkMetrics, BenchmarkResult};
use crate::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::path::Path;
use std::time::{Duration, Instant};

/// ATP profile kinds for different network and workload scenarios.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AtpProfileKind {
    /// Clean LAN with low latency and no loss
    CleanLan,
    /// Lossy WiFi with packet loss and jitter
    LossyWifi,
    /// WAN with higher latency
    Wan,
    /// Relay-only path (no direct connection)
    RelayOnly,
    /// Mailbox/store-and-forward mode
    Mailbox,
    /// Swarm transfer with multiple participants
    Swarm,
    /// Sparse image/file transfer
    SparseImage,
    /// Artifact/object graph transfer
    Artifact,
    /// Streaming data transfer
    Stream,
}

impl AtpProfileKind {
    /// Get all available ATP profile kinds.
    #[must_use]
    pub const fn all() -> &'static [Self] {
        &[
            Self::CleanLan,
            Self::LossyWifi,
            Self::Wan,
            Self::RelayOnly,
            Self::Mailbox,
            Self::Swarm,
            Self::SparseImage,
            Self::Artifact,
            Self::Stream,
        ]
    }

    /// Get a human-readable label for the profile.
    #[must_use]
    pub const fn label(self) -> &'static str {
        match self {
            Self::CleanLan => "clean-lan",
            Self::LossyWifi => "lossy-wifi",
            Self::Wan => "wan",
            Self::RelayOnly => "relay-only",
            Self::Mailbox => "mailbox",
            Self::Swarm => "swarm",
            Self::SparseImage => "sparse-image",
            Self::Artifact => "artifact",
            Self::Stream => "stream",
        }
    }

    /// Get a description of what this profile tests.
    #[must_use]
    pub const fn description(self) -> &'static str {
        match self {
            Self::CleanLan => "LAN transfer with optimal conditions",
            Self::LossyWifi => "WiFi with packet loss and variable latency",
            Self::Wan => "Wide-area network with higher latency",
            Self::RelayOnly => "Transfer via relay server only",
            Self::Mailbox => "Store-and-forward through mailbox",
            Self::Swarm => "Multi-participant swarm transfer",
            Self::SparseImage => "Sparse file with many holes",
            Self::Artifact => "Object graph with metadata",
            Self::Stream => "Streaming data transfer",
        }
    }

    /// Check if this profile is suitable for smoke testing.
    #[must_use]
    pub const fn is_smoke_test_suitable(self) -> bool {
        matches!(self, Self::CleanLan | Self::Wan | Self::Stream)
    }
}

/// ATP benchmark profile configuration.
#[derive(Debug, Clone)]
pub struct AtpProfile {
    /// Profile kind
    pub kind: AtpProfileKind,
    /// Network conditions for this profile
    pub network_conditions: NetworkConditions,
    /// Workload characteristics
    pub workload: WorkloadCharacteristics,
}

impl AtpProfile {
    /// Create a clean LAN profile.
    #[must_use]
    pub fn clean_lan() -> Self {
        Self {
            kind: AtpProfileKind::CleanLan,
            network_conditions: NetworkConditions {
                latency: Duration::from_millis(1),
                packet_loss: 0.0,
                bandwidth_mbps: 1000, // Gigabit LAN
                jitter: Duration::ZERO,
            },
            workload: WorkloadCharacteristics {
                transfer_type: TransferType::BulkFile,
                compression: false,
                encryption: true,
                checksumming: true,
            },
        }
    }

    /// Create a lossy WiFi profile.
    #[must_use]
    pub fn lossy_wifi() -> Self {
        Self {
            kind: AtpProfileKind::LossyWifi,
            network_conditions: NetworkConditions {
                latency: Duration::from_millis(10),
                packet_loss: 0.02, // 2% loss
                bandwidth_mbps: 50,
                jitter: Duration::from_millis(5),
            },
            workload: WorkloadCharacteristics {
                transfer_type: TransferType::BulkFile,
                compression: true,
                encryption: true,
                checksumming: true,
            },
        }
    }

    /// Create a WAN profile.
    #[must_use]
    pub fn wan() -> Self {
        Self {
            kind: AtpProfileKind::Wan,
            network_conditions: NetworkConditions {
                latency: Duration::from_millis(50),
                packet_loss: 0.001, // 0.1% loss
                bandwidth_mbps: 100,
                jitter: Duration::from_millis(10),
            },
            workload: WorkloadCharacteristics {
                transfer_type: TransferType::BulkFile,
                compression: true,
                encryption: true,
                checksumming: true,
            },
        }
    }

    /// Create a streaming profile.
    #[must_use]
    pub fn stream() -> Self {
        Self {
            kind: AtpProfileKind::Stream,
            network_conditions: NetworkConditions {
                latency: Duration::from_millis(20),
                packet_loss: 0.005,
                bandwidth_mbps: 200,
                jitter: Duration::from_millis(3),
            },
            workload: WorkloadCharacteristics {
                transfer_type: TransferType::Stream,
                compression: false,
                encryption: true,
                checksumming: false, // Streaming typically prioritizes speed
            },
        }
    }

    /// Execute this ATP profile benchmark.
    ///
    /// # Errors
    /// Returns [`BenchmarkError`] if ATP execution fails.
    pub async fn run_benchmark(
        &self,
        config: &BenchmarkConfig,
        source_path: &Path,
        dest_path: &Path,
    ) -> Result<BenchmarkResult, BenchmarkError> {
        let mut iterations = Vec::new();

        for iteration in 0..config.iterations {
            let metrics = self
                .execute_atp_transfer(config, source_path, dest_path, iteration)
                .await?;

            iterations.push(metrics);
        }

        Ok(BenchmarkResult {
            tool_name: format!("atp-{}", self.kind.label()),
            iterations,
            environment: crate::atp::benchmark::BenchmarkEnvironment::collect()?,
        })
    }

    async fn execute_atp_transfer(
        &self,
        config: &BenchmarkConfig,
        source_path: &Path,
        dest_path: &Path,
        iteration: u32,
    ) -> Result<BenchmarkMetrics, BenchmarkError> {
        // Create test data if it doesn't exist
        if !source_path.exists() {
            self.create_test_data(source_path, config.data_size).await?;
        }

        let iteration_dest = dest_path.with_extension(&format!("atp_iter{iteration}"));

        let start_time = Instant::now();

        let transfer_result = self
            .execute_profiled_atp_transfer(source_path, &iteration_dest, config)
            .await;

        let wall_time = start_time.elapsed();

        match transfer_result {
            Ok(transfer_metrics) => {
                // Verify transfer completed correctly
                let dest_size = crate::fs::metadata(&iteration_dest).await?.len();
                let verified_completion = dest_size == config.data_size;

                Ok(BenchmarkMetrics {
                    wall_time,
                    cpu_time: transfer_metrics.cpu_time,
                    memory_peak: transfer_metrics.memory_peak,
                    bytes_transferred: dest_size,
                    bytes_on_wire: transfer_metrics.bytes_on_wire,
                    verified_completion,
                    first_usable_output: transfer_metrics.first_usable_output,
                    resume_time: None,
                    disk_amplification_ratio: Some(1.0),
                    failure_reproducible: None,
                    failure_mode: None,
                })
            }
            Err(e) => Ok(BenchmarkMetrics {
                wall_time,
                cpu_time: None,
                memory_peak: None,
                bytes_transferred: 0,
                bytes_on_wire: None,
                verified_completion: false,
                first_usable_output: None,
                resume_time: None,
                disk_amplification_ratio: None,
                failure_reproducible: Some(true),
                failure_mode: Some(e),
            }),
        }
    }

    async fn create_test_data(&self, path: &Path, size: u64) -> Result<(), BenchmarkError> {
        let mut file = crate::fs::File::create(path).await?;

        match self.workload.transfer_type {
            TransferType::BulkFile => {
                // Create solid test file
                let chunk_size = 64 * 1024;
                let chunk_data = vec![0u8; chunk_size];
                let mut remaining = size;

                while remaining > 0 {
                    let write_size = std::cmp::min(remaining, chunk_size as u64) as usize;
                    AsyncWriteExt::write_all(&mut file, &chunk_data[..write_size]).await?;
                    remaining -= write_size as u64;
                }
            }
            TransferType::SparseFile => {
                // Create sparse file with holes
                let hole_size = 64 * 1024;
                let data_size = 4 * 1024;
                let data_chunk = vec![42u8; data_size];
                let mut written = 0;

                while written < size {
                    AsyncWriteExt::write_all(&mut file, &data_chunk).await?;
                    written += data_size as u64;

                    if written < size {
                        // Skip ahead to create a hole
                        let skip = std::cmp::min(hole_size as u64, size - written);
                        AsyncSeekExt::seek(&mut file, std::io::SeekFrom::Current(skip as i64))
                            .await?;
                        written += skip;
                    }
                }
            }
            TransferType::Stream => {
                // Create predictable streaming data
                let chunk_size = 1024;
                let mut data = Vec::with_capacity(chunk_size);
                let mut remaining = size;

                while remaining > 0 {
                    data.clear();
                    let write_size = std::cmp::min(remaining, chunk_size as u64) as usize;

                    // Create pattern data for streaming
                    for i in 0..write_size {
                        data.push(((i % 256) as u8).wrapping_add((remaining % 256) as u8));
                    }

                    AsyncWriteExt::write_all(&mut file, &data).await?;
                    remaining -= write_size as u64;
                }
            }
        }

        Ok(())
    }

    async fn execute_profiled_atp_transfer(
        &self,
        source: &Path,
        dest: &Path,
        config: &BenchmarkConfig,
    ) -> Result<AtpTransferMetrics, String> {
        let transfer_start = Instant::now();
        let mut source_file = crate::fs::File::open(source).await.map_err(|e| {
            format!(
                "failed to open ATP benchmark source {}: {e}",
                source.display()
            )
        })?;
        let mut dest_file = crate::fs::File::create(dest).await.map_err(|e| {
            format!(
                "failed to create ATP benchmark destination {}: {e}",
                dest.display()
            )
        })?;

        let chunk_size = self.transfer_chunk_size(config.data_size);
        let mut buffer = vec![0_u8; chunk_size];
        let mut source_digest = Sha256::new();
        let mut wire_estimator = WireEstimator::default();
        let mut first_usable_output = None;
        let mut bytes_copied = 0_u64;

        loop {
            let read = AsyncReadExt::read(&mut source_file, &mut buffer)
                .await
                .map_err(|e| format!("ATP benchmark read failed: {e}"))?;
            if read == 0 {
                break;
            }

            let chunk = &buffer[..read];
            source_digest.update(chunk);
            wire_estimator.observe(chunk);
            AsyncWriteExt::write_all(&mut dest_file, chunk)
                .await
                .map_err(|e| format!("ATP benchmark write failed: {e}"))?;

            bytes_copied = bytes_copied.saturating_add(read as u64);
            if first_usable_output.is_none()
                && matches!(self.workload.transfer_type, TransferType::Stream)
            {
                first_usable_output = Some(transfer_start.elapsed());
            }
        }

        AsyncWriteExt::flush(&mut dest_file)
            .await
            .map_err(|e| format!("ATP benchmark flush failed: {e}"))?;
        drop(dest_file);

        if bytes_copied != config.data_size {
            return Err(format!(
                "ATP benchmark copied {bytes_copied} byte(s), expected {}",
                config.data_size
            ));
        }

        let mut verification_file = crate::fs::File::open(dest).await.map_err(|e| {
            format!(
                "failed to reopen ATP benchmark destination {}: {e}",
                dest.display()
            )
        })?;
        let mut dest_digest = Sha256::new();
        loop {
            let read = AsyncReadExt::read(&mut verification_file, &mut buffer)
                .await
                .map_err(|e| format!("ATP benchmark verification read failed: {e}"))?;
            if read == 0 {
                break;
            }
            dest_digest.update(&buffer[..read]);
        }

        if source_digest.finalize() != dest_digest.finalize() {
            return Err("ATP benchmark destination digest mismatch".to_string());
        }

        let bytes_on_wire = wire_estimator.estimated_wire_bytes(
            self.workload.compression,
            self.workload.encryption,
            self.workload.checksumming,
            chunk_size,
            self.network_conditions.packet_loss,
        );

        Ok(AtpTransferMetrics {
            cpu_time: Some(transfer_start.elapsed()),
            memory_peak: Some(chunk_size as u64),
            bytes_on_wire: Some(bytes_on_wire),
            first_usable_output,
        })
    }

    fn transfer_chunk_size(&self, data_size: u64) -> usize {
        let bandwidth = self.network_conditions.bandwidth_mbps;
        let mut chunk_size = if bandwidth >= 500 {
            256 * 1024
        } else if bandwidth >= 100 {
            128 * 1024
        } else {
            64 * 1024
        };

        if self.network_conditions.packet_loss >= 0.01 {
            chunk_size /= 2;
        }
        if self.network_conditions.jitter >= Duration::from_millis(10) {
            chunk_size /= 2;
        }
        if matches!(self.workload.transfer_type, TransferType::Stream) {
            chunk_size = chunk_size.min(16 * 1024);
        }

        let bounded_by_data = usize::try_from(data_size)
            .ok()
            .filter(|size| *size > 0)
            .map_or(chunk_size, |size| chunk_size.min(size));
        bounded_by_data.clamp(4 * 1024, 256 * 1024)
    }
}

#[derive(Debug)]
struct WireEstimator {
    byte_counts: [u64; 256],
    total_bytes: u64,
}

impl Default for WireEstimator {
    fn default() -> Self {
        Self {
            byte_counts: [0; 256],
            total_bytes: 0,
        }
    }
}

impl WireEstimator {
    fn observe(&mut self, chunk: &[u8]) {
        self.total_bytes = self.total_bytes.saturating_add(chunk.len() as u64);
        for byte in chunk {
            self.byte_counts[*byte as usize] = self.byte_counts[*byte as usize].saturating_add(1);
        }
    }

    fn estimated_wire_bytes(
        &self,
        compression: bool,
        encryption: bool,
        checksumming: bool,
        chunk_size: usize,
        packet_loss: f64,
    ) -> u64 {
        let payload_bytes = if compression {
            self.entropy_limited_payload_bytes()
        } else {
            self.total_bytes
        };
        let chunk_count = self
            .total_bytes
            .div_ceil(u64::try_from(chunk_size.max(1)).unwrap_or(1));
        let encryption_overhead = if encryption { chunk_count * 16 } else { 0 };
        let checksum_overhead = if checksumming { chunk_count * 32 } else { 0 };
        let loss_multiplier = 1.0 / (1.0 - packet_loss.clamp(0.0, 0.95));

        ((payload_bytes + encryption_overhead + checksum_overhead) as f64 * loss_multiplier).ceil()
            as u64
    }

    fn entropy_limited_payload_bytes(&self) -> u64 {
        if self.total_bytes == 0 {
            return 0;
        }

        let total = self.total_bytes as f64;
        let entropy_bits = self
            .byte_counts
            .iter()
            .filter(|count| **count > 0)
            .map(|count| {
                let probability = *count as f64 / total;
                -probability * probability.log2()
            })
            .sum::<f64>();
        let compressed = ((entropy_bits / 8.0) * total).ceil() as u64;
        compressed
            .saturating_add(64)
            .clamp(self.total_bytes / 8, self.total_bytes)
    }
}

/// Network conditions for ATP profile.
#[derive(Debug, Clone)]
pub struct NetworkConditions {
    /// Network latency
    pub latency: Duration,
    /// Packet loss probability (0.0-1.0)
    pub packet_loss: f64,
    /// Bandwidth in Mbps
    pub bandwidth_mbps: u32,
    /// Network jitter
    pub jitter: Duration,
}

/// Workload characteristics for ATP profile.
#[derive(Debug, Clone)]
pub struct WorkloadCharacteristics {
    /// Type of transfer
    pub transfer_type: TransferType,
    /// Enable compression
    pub compression: bool,
    /// Enable encryption
    pub encryption: bool,
    /// Enable checksumming
    pub checksumming: bool,
}

/// Types of data transfer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransferType {
    /// Bulk file transfer
    BulkFile,
    /// Sparse file with holes
    SparseFile,
    /// Streaming data
    Stream,
}

/// Metrics from ATP transfer execution.
#[derive(Debug)]
struct AtpTransferMetrics {
    cpu_time: Option<Duration>,
    memory_peak: Option<u64>,
    bytes_on_wire: Option<u64>,
    first_usable_output: Option<Duration>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn atp_profile_kinds_have_labels() {
        for kind in AtpProfileKind::all() {
            assert!(!kind.label().is_empty());
            assert!(!kind.description().is_empty());
        }
    }

    #[test]
    fn clean_lan_profile_has_good_conditions() {
        let profile = AtpProfile::clean_lan();
        assert_eq!(profile.kind, AtpProfileKind::CleanLan);
        assert!(profile.network_conditions.latency <= Duration::from_millis(5));
        assert!(profile.network_conditions.packet_loss < 0.001);
    }

    #[test]
    fn lossy_wifi_profile_has_challenging_conditions() {
        let profile = AtpProfile::lossy_wifi();
        assert_eq!(profile.kind, AtpProfileKind::LossyWifi);
        assert!(profile.network_conditions.packet_loss > 0.01);
        assert!(profile.network_conditions.jitter > Duration::ZERO);
    }

    #[test]
    fn smoke_test_suitable_profiles_are_reasonable() {
        for kind in AtpProfileKind::all() {
            if kind.is_smoke_test_suitable() {
                // Smoke test profiles should be relatively fast
                assert!(matches!(
                    kind,
                    AtpProfileKind::CleanLan | AtpProfileKind::Wan | AtpProfileKind::Stream
                ));
            }
        }
    }
}