sql-splitter 1.16.1

High-performance CLI tool for splitting large SQL dump files into individual table files
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
//! Feedback controller for the adaptive I/O profiles.
//!
//! The controller is a pure state machine: `(state, epoch measurement) →
//! (state, actions)`. It never touches a clock or a device itself — epochs
//! are *byte-based* (every N input bytes) and durations arrive as data, which
//! is what makes every decision reproducible in tests with a mock clock.
//! See `docs/features/ADAPTIVE_IO_PROFILES.md` for the full design.

use super::profile::ProfileKind;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

/// Monotonic time source for epoch measurements.
///
/// Returns time elapsed since an arbitrary per-clock origin. The production
/// implementation wraps [`std::time::Instant`]; tests use [`MockClock`] so
/// controller behavior is a pure function of the scripted inputs.
pub trait Clock: Send + Sync {
    fn now(&self) -> Duration;
}

/// Wall-clock [`Clock`] backed by [`std::time::Instant`].
#[derive(Debug)]
pub struct RealClock {
    origin: std::time::Instant,
}

impl RealClock {
    pub fn new() -> Self {
        Self {
            origin: std::time::Instant::now(),
        }
    }
}

impl Default for RealClock {
    fn default() -> Self {
        Self::new()
    }
}

impl Clock for RealClock {
    fn now(&self) -> Duration {
        self.origin.elapsed()
    }
}

/// Deterministic [`Clock`] for tests: every `now()` call advances time by a
/// fixed step, so a pipeline that samples the clock once per epoch boundary
/// sees identical epoch durations on every machine, every run.
#[derive(Debug)]
pub struct MockClock {
    nanos: AtomicU64,
    step_nanos: u64,
}

impl MockClock {
    /// A clock that advances `step` per `now()` call.
    pub fn stepping(step: Duration) -> Self {
        Self {
            nanos: AtomicU64::new(0),
            step_nanos: step.as_nanos() as u64,
        }
    }
}

impl Clock for MockClock {
    fn now(&self) -> Duration {
        let t = self.nanos.fetch_add(self.step_nanos, Ordering::Relaxed);
        Duration::from_nanos(t + self.step_nanos)
    }
}

/// What the pipeline observed during one byte-based epoch.
#[derive(Debug, Clone, Copy)]
pub struct EpochMeasurement {
    /// Bytes acked by the writer threads during the epoch.
    pub bytes: u64,
    /// Wall time the epoch spanned.
    pub duration: Duration,
    /// Time the producer spent blocked on full writer channels in the epoch.
    pub send_stall: Duration,
}

impl EpochMeasurement {
    /// Writer throughput in MB/s (MiB, matching the measurement notes).
    ///
    /// A zero-duration epoch is unmeasurable, not infinitely fast (two epoch
    /// boundaries can land on the same clock tick); it reports `0.0` and
    /// [`Controller::on_epoch`] discards the epoch entirely.
    pub fn throughput_mbps(&self) -> f64 {
        let secs = self.duration.as_secs_f64();
        if secs <= 0.0 {
            return 0.0;
        }
        self.bytes as f64 / (1024.0 * 1024.0) / secs
    }

    /// Fraction of the epoch the producer sat blocked on channel sends.
    pub fn stall_fraction(&self) -> f64 {
        let secs = self.duration.as_secs_f64();
        if secs <= 0.0 {
            return 0.0;
        }
        self.send_stall.as_secs_f64() / secs
    }
}

/// What the splitter should do after an epoch boundary.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EpochDecision {
    /// `Some(kind)` when the controller changed state: swap the profile
    /// values and emit the transition log line.
    pub transition: Option<ProfileKind>,
    /// Desired writer-thread count. The pool only ever grows toward this
    /// (shrinking would reassign table ownership and break output ordering).
    pub target_writers: usize,
    /// Observed throughput for this epoch, for the transition log line.
    pub throughput_mbps: f64,
}

/// `FAST → SLOW_SEEK` fires below this throughput (with high stall).
const FAST_DOWN_MBPS: f64 = 150.0;
/// `SLOW_SEEK → SLOW_OPS` fires below this throughput (with high stall).
const SLOW_SEEK_DOWN_MBPS: f64 = 15.0;
/// Downgrades additionally require the producer blocked on channel sends
/// this fraction of the epoch — low throughput with near-zero stall means
/// the *input* is the bottleneck and the write profile must not react.
const STALL_DOWN_FRACTION: f64 = 0.30;
/// Consecutive qualifying epochs required to downgrade.
const DOWN_EPOCHS: u32 = 2;
/// Consecutive qualifying epochs required to upgrade (asymmetric hysteresis:
/// a wrong downgrade on NVMe costs ~15%; a wrong non-downgrade on an HDD
/// costs 2.5×).
const UP_EPOCHS: u32 = 3;
/// Upgrades require throughput above `factor × downgrade threshold`.
const UP_FACTOR: f64 = 2.0;

/// The adaptive-profile state machine, evaluated at epoch boundaries.
#[derive(Debug)]
pub struct Controller {
    state: ProfileKind,
    /// Writer count to grow to once the device is proven fast.
    fast_writers: usize,
    /// The first epoch is discarded as page-cache warmup.
    warmup_done: bool,
    /// Consecutive epochs meeting the current state's downgrade condition.
    down_streak: u32,
    /// Consecutive epochs meeting the current state's upgrade condition.
    up_streak: u32,
    /// Set once a measured epoch shows FAST-class throughput; gates the
    /// deferred writer spawn so slow media is never thrashed at all.
    proven_fast: bool,
    /// Output is compressed: throughput is measured in *compressed* bytes
    /// beneath the encoders, so the raw-media thresholds are meaningless and
    /// a CPU-bound encoder stalls the producer exactly like a slow device.
    /// The state is frozen at the probe-chosen opening profile, and the SSD
    /// opening requests its full writer count immediately (compression wants
    /// cores; a lone encoder could never "prove" 150 MB/s compressed).
    compressed_output: bool,
}

impl Controller {
    /// `opening` is the probe-chosen starting state; `fast_writers` is the
    /// FAST profile's writer count (the growth ceiling in auto mode).
    pub fn new(opening: ProfileKind, fast_writers: usize) -> Self {
        Self {
            state: opening,
            fast_writers: fast_writers.max(1),
            warmup_done: false,
            down_streak: 0,
            up_streak: 0,
            proven_fast: false,
            compressed_output: false,
        }
    }

    /// Freeze the controller for compressed-output runs: no transitions (the
    /// epoch signals cannot distinguish a CPU-bound encoder from a slow
    /// device) and immediate writer growth when the opening state is SSD.
    pub fn for_compressed_output(mut self) -> Self {
        self.compressed_output = true;
        self
    }

    /// Current state.
    pub fn state(&self) -> ProfileKind {
        self.state
    }

    fn decision(&self, transition: Option<ProfileKind>, mbps: f64) -> EpochDecision {
        EpochDecision {
            transition,
            target_writers: if self.state == ProfileKind::Ssd
                && (self.proven_fast || self.compressed_output)
            {
                self.fast_writers
            } else {
                1
            },
            throughput_mbps: mbps,
        }
    }

    /// Evaluate one epoch boundary. Pure: no clocks, no I/O.
    pub fn on_epoch(&mut self, m: &EpochMeasurement) -> EpochDecision {
        let mbps = m.throughput_mbps();

        // A zero-duration epoch is unmeasurable, not infinitely fast: two
        // boundary evaluations can land on the same clock tick (coarse or
        // injected clocks). Discard it entirely — updating `proven_fast` or
        // either streak from a non-measurement would fake throughput or
        // reset legitimate hysteresis.
        if m.duration.is_zero() {
            return self.decision(None, mbps);
        }

        // First epoch is page-cache warmup: measure nothing from it.
        if !self.warmup_done {
            self.warmup_done = true;
            return self.decision(None, mbps);
        }

        // Compressed output: the measurement is compressed MB/s from a
        // CPU-bound encoder, meaningless against the raw-media thresholds
        // (and its stall signature mimics a slow device). State stays frozen
        // at the probe-chosen opening profile.
        if self.compressed_output {
            return self.decision(None, mbps);
        }

        let stalled = m.stall_fraction() > STALL_DOWN_FRACTION;
        let mut transition = None;

        match self.state {
            ProfileKind::Ssd => {
                if mbps < FAST_DOWN_MBPS && stalled {
                    self.down_streak += 1;
                    if self.down_streak >= DOWN_EPOCHS {
                        transition = self.enter(ProfileKind::SlowSeek);
                    }
                } else {
                    self.down_streak = 0;
                    if mbps >= FAST_DOWN_MBPS {
                        self.proven_fast = true;
                    }
                }
            }
            ProfileKind::SlowSeek => {
                if mbps < SLOW_SEEK_DOWN_MBPS && stalled {
                    self.down_streak += 1;
                    self.up_streak = 0;
                    if self.down_streak >= DOWN_EPOCHS {
                        transition = self.enter(ProfileKind::SlowOps);
                    }
                } else if mbps > UP_FACTOR * FAST_DOWN_MBPS {
                    self.down_streak = 0;
                    self.up_streak += 1;
                    if self.up_streak >= UP_EPOCHS {
                        transition = self.enter(ProfileKind::Ssd);
                        // The upgrade evidence (3 epochs > 300 MB/s) is
                        // exactly what "proven fast" means.
                        self.proven_fast = true;
                    }
                } else {
                    self.down_streak = 0;
                    self.up_streak = 0;
                }
            }
            ProfileKind::SlowOps => {
                if mbps > UP_FACTOR * SLOW_SEEK_DOWN_MBPS {
                    self.up_streak += 1;
                    if self.up_streak >= UP_EPOCHS {
                        transition = self.enter(ProfileKind::SlowSeek);
                    }
                } else {
                    self.up_streak = 0;
                }
            }
        }

        self.decision(transition, mbps)
    }

    /// Move to `next`, resetting both hysteresis streaks.
    fn enter(&mut self, next: ProfileKind) -> Option<ProfileKind> {
        self.state = next;
        self.down_streak = 0;
        self.up_streak = 0;
        Some(next)
    }
}

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

    /// Build a measurement from MB/s and a stall fraction, using the mock
    /// clock's fixed step as the epoch duration — the same shape the real
    /// pipeline produces.
    fn epoch(mbps: f64, stall_fraction: f64) -> EpochMeasurement {
        let duration = Duration::from_millis(100);
        EpochMeasurement {
            bytes: (mbps * 1024.0 * 1024.0 * duration.as_secs_f64()) as u64,
            duration,
            send_stall: duration.mul_f64(stall_fraction),
        }
    }

    /// Run a scripted sequence, returning the transition trace as
    /// `(epoch_index_1_based, new_state)`.
    fn trace(
        controller: &mut Controller,
        epochs: &[EpochMeasurement],
    ) -> Vec<(usize, ProfileKind)> {
        epochs
            .iter()
            .enumerate()
            .filter_map(|(i, m)| controller.on_epoch(m).transition.map(|k| (i + 1, k)))
            .collect()
    }

    #[test]
    fn steady_fast_stays_fast() {
        let mut c = Controller::new(ProfileKind::Ssd, 4);
        let epochs: Vec<_> = std::iter::repeat_n(epoch(500.0, 0.05), 50).collect();
        assert_eq!(trace(&mut c, &epochs), vec![]);
        assert_eq!(c.state(), ProfileKind::Ssd);
    }

    #[test]
    fn degradation_transitions_exactly_at_epoch_three() {
        // 500, 30, 28 MB/s with high stall: epoch 1 is warmup, epochs 2 and 3
        // qualify, so the FAST→SLOW_SEEK transition lands exactly at epoch 3.
        let mut c = Controller::new(ProfileKind::Ssd, 4);
        let epochs = [epoch(500.0, 0.8), epoch(30.0, 0.8), epoch(28.0, 0.8)];
        assert_eq!(trace(&mut c, &epochs), vec![(3, ProfileKind::SlowSeek)]);
    }

    #[test]
    fn oscillation_around_threshold_never_flaps() {
        // 100/200 MB/s alternating straddles the 150 MB/s downgrade line but
        // never sustains it for 2 consecutive epochs — hysteresis holds.
        let mut c = Controller::new(ProfileKind::Ssd, 4);
        let epochs: Vec<_> = (0..40)
            .map(|i| epoch(if i % 2 == 0 { 100.0 } else { 200.0 }, 0.8))
            .collect();
        assert_eq!(trace(&mut c, &epochs), vec![]);
        assert_eq!(c.state(), ProfileKind::Ssd);
    }

    /// Regression for the 2026-07-16 field bug: `bytes_acked` used to count
    /// bytes landing in the writer's RAM coalescing buffer, so a slow device
    /// reported ~600 MB/s "throughput" alongside heavy producer stall. Per
    /// the design doc, a FAST downgrade requires *both* low throughput and
    /// high stall — high measured throughput must veto the downgrade no
    /// matter how stalled the producer is.
    #[test]
    fn high_throughput_high_stall_never_downgrades_from_fast() {
        let mut c = Controller::new(ProfileKind::Ssd, 4);
        let epochs: Vec<_> = std::iter::repeat_n(epoch(612.0, 0.9), 20).collect();
        assert_eq!(trace(&mut c, &epochs), vec![]);
        assert_eq!(c.state(), ProfileKind::Ssd);

        // Same for SLOW_SEEK → SLOW_OPS: 40 MB/s + heavy stall is above the
        // 15 MB/s line, so it must hold (and being above 2×15 for 3 epochs it
        // legitimately upgrades instead — anything but a downgrade).
        let mut c = Controller::new(ProfileKind::SlowSeek, 4);
        let epochs: Vec<_> = std::iter::repeat_n(epoch(40.0, 0.9), 20).collect();
        for (_, kind) in trace(&mut c, &epochs) {
            assert_ne!(kind, ProfileKind::SlowOps, "downgrade despite 40 MB/s");
        }
    }

    /// The value in the transition log line must be the exact value the
    /// state machine compared against its thresholds: a downgrade decision
    /// can therefore never carry a throughput above the downgrade line.
    #[test]
    fn logged_throughput_is_the_compared_throughput() {
        let mut c = Controller::new(ProfileKind::Ssd, 4);
        let script = [
            epoch(500.0, 0.1), // warmup
            epoch(30.0, 0.8),
            epoch(28.0, 0.8), // → SLOW_SEEK
        ];
        for m in &script {
            let d = c.on_epoch(m);
            assert_eq!(
                d.throughput_mbps,
                m.throughput_mbps(),
                "decision must carry the measured value it was computed from"
            );
            if d.transition == Some(ProfileKind::SlowSeek) {
                assert!(
                    d.throughput_mbps < FAST_DOWN_MBPS,
                    "FAST downgrade fired at {} MB/s, above the {} MB/s line",
                    d.throughput_mbps,
                    FAST_DOWN_MBPS
                );
            }
        }
        assert_eq!(c.state(), ProfileKind::SlowSeek);
    }

    #[test]
    fn input_bound_low_throughput_zero_stall_never_transitions() {
        // 5 MB/s but the producer never blocks on sends: the input (slow
        // decompression, other device) is the bottleneck, not the output.
        let mut c = Controller::new(ProfileKind::Ssd, 4);
        let epochs: Vec<_> = std::iter::repeat_n(epoch(5.0, 0.0), 20).collect();
        assert_eq!(trace(&mut c, &epochs), vec![]);
        assert_eq!(c.state(), ProfileKind::Ssd);

        // Same holds for the SLOW_SEEK → SLOW_OPS downgrade.
        let mut c = Controller::new(ProfileKind::SlowSeek, 4);
        let epochs: Vec<_> = std::iter::repeat_n(epoch(5.0, 0.0), 20).collect();
        assert_eq!(trace(&mut c, &epochs), vec![]);
        assert_eq!(c.state(), ProfileKind::SlowSeek);
    }

    #[test]
    fn double_downgrade_fast_to_slow_seek_to_slow_ops() {
        let mut c = Controller::new(ProfileKind::Ssd, 4);
        let epochs = [
            epoch(400.0, 0.1), // warmup (discarded)
            epoch(30.0, 0.8),
            epoch(30.0, 0.8), // → SLOW_SEEK
            epoch(8.0, 0.8),
            epoch(8.0, 0.8), // → SLOW_OPS
        ];
        assert_eq!(
            trace(&mut c, &epochs),
            vec![(3, ProfileKind::SlowSeek), (5, ProfileKind::SlowOps)]
        );
    }

    #[test]
    fn recovery_upgrade_after_three_qualifying_epochs() {
        // SLOW_SEEK → FAST needs 3 consecutive epochs above 2× the 150 MB/s
        // downgrade threshold; two are not enough.
        let mut c = Controller::new(ProfileKind::SlowSeek, 4);
        let epochs = [
            epoch(50.0, 0.1), // warmup
            epoch(400.0, 0.0),
            epoch(400.0, 0.0),
            epoch(250.0, 0.0), // streak broken (below 300)
            epoch(400.0, 0.0),
            epoch(400.0, 0.0),
            epoch(400.0, 0.0), // third consecutive → FAST
        ];
        assert_eq!(trace(&mut c, &epochs), vec![(7, ProfileKind::Ssd)]);

        // SLOW_OPS recovers to SLOW_SEEK above 2× 15 MB/s.
        let mut c = Controller::new(ProfileKind::SlowOps, 4);
        let epochs = [
            epoch(5.0, 0.8), // warmup
            epoch(40.0, 0.0),
            epoch(40.0, 0.0),
            epoch(40.0, 0.0),
        ];
        assert_eq!(trace(&mut c, &epochs), vec![(4, ProfileKind::SlowSeek)]);
    }

    #[test]
    fn writer_growth_waits_for_proven_fast() {
        let mut c = Controller::new(ProfileKind::Ssd, 4);
        // Warmup epoch: not proof, stay at 1 writer.
        assert_eq!(c.on_epoch(&epoch(500.0, 0.0)).target_writers, 1);
        // First measured fast epoch proves the device: grow to FAST's count.
        assert_eq!(c.on_epoch(&epoch(500.0, 0.0)).target_writers, 4);
        // Input-bound epochs are not proof either way; the flag is sticky.
        assert_eq!(c.on_epoch(&epoch(5.0, 0.0)).target_writers, 4);
    }

    #[test]
    fn slow_opening_never_requests_growth() {
        let mut c = Controller::new(ProfileKind::SlowSeek, 8);
        for _ in 0..10 {
            assert_eq!(c.on_epoch(&epoch(30.0, 0.5)).target_writers, 1);
        }
    }

    #[test]
    fn upgrade_to_fast_requests_growth() {
        let mut c = Controller::new(ProfileKind::SlowSeek, 4);
        c.on_epoch(&epoch(50.0, 0.1)); // warmup
        c.on_epoch(&epoch(400.0, 0.0));
        c.on_epoch(&epoch(400.0, 0.0));
        let d = c.on_epoch(&epoch(400.0, 0.0));
        assert_eq!(d.transition, Some(ProfileKind::Ssd));
        assert_eq!(d.target_writers, 4);
    }

    /// A zero-duration epoch used to report `f64::INFINITY` MB/s, instantly
    /// setting `proven_fast` (spurious writer growth on a slow device) or
    /// counting toward an upgrade streak. It must be discarded instead.
    #[test]
    fn zero_duration_epoch_is_discarded_not_infinite() {
        let zero = EpochMeasurement {
            bytes: 64 * 1024 * 1024,
            duration: Duration::ZERO,
            send_stall: Duration::ZERO,
        };
        assert_eq!(zero.throughput_mbps(), 0.0, "unmeasurable, not infinite");

        // SSD opening: a zero-duration epoch must never prove the device
        // fast — the deferred writer spawn stays deferred.
        let mut c = Controller::new(ProfileKind::Ssd, 4);
        c.on_epoch(&epoch(500.0, 0.0)); // warmup
        assert_eq!(
            c.on_epoch(&zero).target_writers,
            1,
            "zero-duration epoch proved the device fast"
        );

        // SLOW_SEEK: zero-duration epochs must not count toward an upgrade.
        let mut c = Controller::new(ProfileKind::SlowSeek, 4);
        c.on_epoch(&epoch(30.0, 0.1)); // warmup
        for _ in 0..10 {
            assert_eq!(c.on_epoch(&zero).transition, None);
        }
        assert_eq!(c.state(), ProfileKind::SlowSeek);
    }

    /// The companion hazard: a degenerate epoch must also not *reset* an
    /// in-progress downgrade streak (that would block legitimate downgrades
    /// on genuinely slow media).
    #[test]
    fn zero_duration_epoch_does_not_reset_downgrade_streak() {
        let zero = EpochMeasurement {
            bytes: 0,
            duration: Duration::ZERO,
            send_stall: Duration::ZERO,
        };
        let mut c = Controller::new(ProfileKind::Ssd, 4);
        c.on_epoch(&epoch(500.0, 0.1)); // warmup
        assert_eq!(c.on_epoch(&epoch(30.0, 0.8)).transition, None); // streak 1
        assert_eq!(c.on_epoch(&zero).transition, None); // degenerate boundary
        assert_eq!(
            c.on_epoch(&epoch(28.0, 0.8)).transition,
            Some(ProfileKind::SlowSeek),
            "degenerate epoch reset the downgrade streak"
        );
    }

    /// Compressed output: throughput is compressed MB/s from a CPU-bound
    /// encoder. A lone gzip writer emits ~20-30 MB/s compressed while the
    /// producer stalls on full channels — exactly the old downgrade
    /// signature. The frozen controller must neither downgrade nor wait for
    /// an unreachable 150 MB/s "proof" before growing writers.
    #[test]
    fn compressed_output_grows_immediately_and_never_downgrades() {
        let mut c = Controller::new(ProfileKind::Ssd, 8).for_compressed_output();
        // Even the warmup decision requests the full compression writer count.
        assert_eq!(c.on_epoch(&epoch(25.0, 0.9)).target_writers, 8);
        for _ in 0..20 {
            let d = c.on_epoch(&epoch(25.0, 0.9));
            assert_eq!(d.transition, None, "downgraded on CPU-bound compression");
            assert_eq!(d.target_writers, 8);
        }
        assert_eq!(c.state(), ProfileKind::Ssd);
    }

    /// A slow probe verdict still holds under compressed output: one writer,
    /// and no upgrade no matter what the (meaningless) numbers say.
    #[test]
    fn compressed_output_on_slow_opening_stays_single_writer() {
        let mut c = Controller::new(ProfileKind::SlowSeek, 8).for_compressed_output();
        for _ in 0..10 {
            let d = c.on_epoch(&epoch(400.0, 0.0));
            assert_eq!(d.transition, None);
            assert_eq!(d.target_writers, 1);
        }
        assert_eq!(c.state(), ProfileKind::SlowSeek);
    }

    #[test]
    fn mock_clock_steps_deterministically() {
        let clock = MockClock::stepping(Duration::from_millis(5));
        assert_eq!(clock.now(), Duration::from_millis(5));
        assert_eq!(clock.now(), Duration::from_millis(10));
        assert_eq!(clock.now(), Duration::from_millis(15));
    }

    #[test]
    fn real_clock_is_monotonic() {
        let clock = RealClock::new();
        let a = clock.now();
        let b = clock.now();
        assert!(b >= a);
    }
}