heddle-cli 0.15.0

An AI-native version control system
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
// SPDX-License-Identifier: Apache-2.0

use std::{
    env,
    path::Path,
    time::{Duration, Instant},
};

use serde_json::Value;

use super::{
    fixture::{PerfFixture, base_command},
    profile::sample_from_trace,
};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum NegativeControl {
    None,
    Latency,
    FullScan,
    SubtreeSkip,
    EagerPackIndex,
    DuplicateOpen,
}

impl NegativeControl {
    pub(super) fn from_env() -> Self {
        match env::var("HEDDLE_PERF_NEGATIVE_CONTROL").as_deref() {
            Ok("latency") => Self::Latency,
            Ok("full-scan") => Self::FullScan,
            Ok("subtree-skip") => Self::SubtreeSkip,
            Ok("eager-pack-index") => Self::EagerPackIndex,
            Ok("duplicate-open") => Self::DuplicateOpen,
            Ok(value) => panic!("unknown HEDDLE_PERF_NEGATIVE_CONTROL `{value}`"),
            Err(_) => Self::None,
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum CaseKind {
    Version,
    Help,
    StatusClean,
    StatusDirty,
    CaptureOne,
    DiffOne,
    LogBounded,
    ThreadListBounded,
}

impl CaseKind {
    pub(super) fn name(self) -> &'static str {
        match self {
            Self::Version => "version",
            Self::Help => "help",
            Self::StatusClean => "status_clean",
            Self::StatusDirty => "status_one_dirty",
            Self::CaptureOne => "capture_one",
            Self::DiffOne => "diff_one",
            Self::LogBounded => "log_bounded",
            Self::ThreadListBounded => "thread_list_bounded",
        }
    }

    fn args(self) -> &'static [&'static str] {
        match self {
            Self::Version => &["--version"],
            Self::Help => &["help"],
            Self::StatusClean | Self::StatusDirty => &["--output", "json", "status"],
            Self::CaptureOne => &["--output", "json", "capture", "-m", "perf sample"],
            Self::DiffOne => &["--output", "json", "diff"],
            Self::LogBounded => &["--output", "json", "log", "--limit", "20"],
            Self::ThreadListBounded => &["--output", "json", "thread", "list"],
        }
    }

    pub(super) fn expected_repo_opens(self) -> u64 {
        match self {
            Self::Version | Self::Help => 0,
            Self::StatusClean | Self::StatusDirty => 1,
            Self::CaptureOne => 2,
            Self::DiffOne | Self::LogBounded | Self::ThreadListBounded => 1,
        }
    }
}

#[derive(Clone, Debug, Default)]
pub(super) struct Sample {
    pub wall_ms: f64,
    pub profile_total_ms: f64,
    pub startup_ms: f64,
    pub warm_repository_ms: f64,
    pub repository_open_ms: f64,
    pub current_state_ms: f64,
    pub verification_ms: f64,
    pub worktree_status_ms: f64,
    pub worktree_index_load_ms: f64,
    pub worktree_compare_ms: f64,
    pub worktree_index_save_ms: f64,
    pub thread_summary_ms: f64,
    pub snapshot_ms: f64,
    pub snapshot_tree_walk_ms: f64,
    pub snapshot_blob_prep_ms: f64,
    pub snapshot_blob_write_ms: f64,
    pub snapshot_tree_write_ms: f64,
    pub snapshot_state_ref_oplog_ms: f64,
    pub snapshot_atomic_execute_ms: f64,
    pub snapshot_ref_publish_ms: f64,
    pub snapshot_state_create_ms: f64,
    pub snapshot_captured_path_count_ms: f64,
    pub snapshot_post_verification_ms: f64,
    pub snapshot_thread_metadata_ms: f64,
    pub snapshot_preflight_ms: f64,
    pub snapshot_attribution_ms: f64,
    pub snapshot_execute_save_ms: f64,
    pub snapshot_previous_state_ms: f64,
    pub snapshot_previous_state_head_ms: f64,
    pub snapshot_previous_state_cache_read_ms: f64,
    pub snapshot_previous_state_cache_decode_ms: f64,
    pub snapshot_previous_state_cache_validate_ms: f64,
    pub snapshot_previous_state_store_read_ms: f64,
    pub snapshot_signature_lookup_ms: f64,
    pub snapshot_output_build_ms: f64,
    pub monitor_ms: f64,
    pub rendering_ms: f64,
    pub network_ms: f64,
    pub counters: Counters,
}

#[derive(Clone, Debug, Default)]
pub(super) struct Counters {
    pub directories_scanned: u64,
    pub directories_skipped: u64,
    pub files_hashed: u64,
    pub monitor_changed_paths: u64,
    pub object_decodes: u64,
    pub ref_reads: u64,
    pub oplog_reads: u64,
    pub repository_opens: u64,
    pub network_client_initialized: bool,
    pub ancestors_visited: u64,
    pub history_objects_decoded: u64,
}

impl Sample {
    fn combine(mut self, other: Self) -> Self {
        self.wall_ms += other.wall_ms;
        self.profile_total_ms += other.profile_total_ms;
        self.startup_ms += other.startup_ms;
        self.warm_repository_ms += other.warm_repository_ms;
        self.repository_open_ms += other.repository_open_ms;
        self.current_state_ms += other.current_state_ms;
        self.verification_ms += other.verification_ms;
        self.worktree_status_ms += other.worktree_status_ms;
        self.worktree_index_load_ms += other.worktree_index_load_ms;
        self.worktree_compare_ms += other.worktree_compare_ms;
        self.worktree_index_save_ms += other.worktree_index_save_ms;
        self.thread_summary_ms += other.thread_summary_ms;
        self.snapshot_ms += other.snapshot_ms;
        self.snapshot_tree_walk_ms += other.snapshot_tree_walk_ms;
        self.snapshot_blob_prep_ms += other.snapshot_blob_prep_ms;
        self.snapshot_blob_write_ms += other.snapshot_blob_write_ms;
        self.snapshot_tree_write_ms += other.snapshot_tree_write_ms;
        self.snapshot_state_ref_oplog_ms += other.snapshot_state_ref_oplog_ms;
        self.snapshot_atomic_execute_ms += other.snapshot_atomic_execute_ms;
        self.snapshot_ref_publish_ms += other.snapshot_ref_publish_ms;
        self.snapshot_state_create_ms += other.snapshot_state_create_ms;
        self.snapshot_captured_path_count_ms += other.snapshot_captured_path_count_ms;
        self.snapshot_post_verification_ms += other.snapshot_post_verification_ms;
        self.snapshot_thread_metadata_ms += other.snapshot_thread_metadata_ms;
        self.snapshot_preflight_ms += other.snapshot_preflight_ms;
        self.snapshot_attribution_ms += other.snapshot_attribution_ms;
        self.snapshot_execute_save_ms += other.snapshot_execute_save_ms;
        self.snapshot_previous_state_ms += other.snapshot_previous_state_ms;
        self.snapshot_previous_state_head_ms += other.snapshot_previous_state_head_ms;
        self.snapshot_previous_state_cache_read_ms += other.snapshot_previous_state_cache_read_ms;
        self.snapshot_previous_state_cache_decode_ms +=
            other.snapshot_previous_state_cache_decode_ms;
        self.snapshot_previous_state_cache_validate_ms +=
            other.snapshot_previous_state_cache_validate_ms;
        self.snapshot_previous_state_store_read_ms += other.snapshot_previous_state_store_read_ms;
        self.snapshot_signature_lookup_ms += other.snapshot_signature_lookup_ms;
        self.snapshot_output_build_ms += other.snapshot_output_build_ms;
        self.monitor_ms += other.monitor_ms;
        self.rendering_ms += other.rendering_ms;
        self.network_ms += other.network_ms;
        self.counters.add(&other.counters);
        self
    }
}

impl Counters {
    fn add(&mut self, other: &Self) {
        self.directories_scanned += other.directories_scanned;
        self.directories_skipped += other.directories_skipped;
        self.files_hashed += other.files_hashed;
        self.monitor_changed_paths += other.monitor_changed_paths;
        self.object_decodes += other.object_decodes;
        self.ref_reads += other.ref_reads;
        self.oplog_reads += other.oplog_reads;
        self.repository_opens += other.repository_opens;
        self.network_client_initialized |= other.network_client_initialized;
        self.ancestors_visited += other.ancestors_visited;
        self.history_objects_decoded += other.history_objects_decoded;
    }
}

pub(super) struct CaseResult {
    pub kind: CaseKind,
    pub path_count: usize,
    pub samples: Vec<Sample>,
}

impl CaseResult {
    pub(super) fn metric(&self, select: impl Fn(&Sample) -> f64) -> Stats {
        Stats::from_values(self.samples.iter().map(select).collect())
    }

    pub(super) fn counter(&self, select: impl Fn(&Counters) -> u64) -> Stats {
        Stats::from_values(
            self.samples
                .iter()
                .map(|sample| select(&sample.counters) as f64)
                .collect(),
        )
    }

    pub(super) fn print(&self) {
        let wall = self.metric(|sample| sample.wall_ms);
        println!(
            "RESULT case={} mode={} paths={} samples={} wall_ms={} total_ms={} startup_ms={} warm_repo_ms={} repo_open_ms={} current_state_ms={} verification_ms={} worktree_status_ms={} worktree_index_load_ms={} worktree_compare_ms={} worktree_index_save_ms={} thread_summary_ms={} snapshot_ms={} snapshot_tree_walk_ms={} snapshot_blob_prep_ms={} snapshot_blob_write_ms={} snapshot_tree_write_ms={} snapshot_state_ref_oplog_ms={} snapshot_atomic_execute_ms={} snapshot_ref_publish_ms={} snapshot_state_create_ms={} snapshot_captured_path_count_ms={} snapshot_post_verification_ms={} snapshot_thread_metadata_ms={} snapshot_preflight_ms={} snapshot_attribution_ms={} snapshot_execute_save_ms={} snapshot_previous_state_ms={} snapshot_previous_state_head_ms={} snapshot_previous_state_cache_read_ms={} snapshot_previous_state_cache_decode_ms={} snapshot_previous_state_cache_validate_ms={} snapshot_previous_state_store_read_ms={} snapshot_signature_lookup_ms={} snapshot_output_build_ms={} monitor_ms={} render_ms={} network_ms={}",
            self.kind.name(),
            if self.path_count == 0 {
                "cold_process"
            } else {
                "warm_repo"
            },
            self.path_count,
            self.samples.len(),
            wall,
            self.metric(|sample| sample.profile_total_ms),
            self.metric(|sample| sample.startup_ms),
            self.metric(|sample| sample.warm_repository_ms),
            self.metric(|sample| sample.repository_open_ms),
            self.metric(|sample| sample.current_state_ms),
            self.metric(|sample| sample.verification_ms),
            self.metric(|sample| sample.worktree_status_ms),
            self.metric(|sample| sample.worktree_index_load_ms),
            self.metric(|sample| sample.worktree_compare_ms),
            self.metric(|sample| sample.worktree_index_save_ms),
            self.metric(|sample| sample.thread_summary_ms),
            self.metric(|sample| sample.snapshot_ms),
            self.metric(|sample| sample.snapshot_tree_walk_ms),
            self.metric(|sample| sample.snapshot_blob_prep_ms),
            self.metric(|sample| sample.snapshot_blob_write_ms),
            self.metric(|sample| sample.snapshot_tree_write_ms),
            self.metric(|sample| sample.snapshot_state_ref_oplog_ms),
            self.metric(|sample| sample.snapshot_atomic_execute_ms),
            self.metric(|sample| sample.snapshot_ref_publish_ms),
            self.metric(|sample| sample.snapshot_state_create_ms),
            self.metric(|sample| sample.snapshot_captured_path_count_ms),
            self.metric(|sample| sample.snapshot_post_verification_ms),
            self.metric(|sample| sample.snapshot_thread_metadata_ms),
            self.metric(|sample| sample.snapshot_preflight_ms),
            self.metric(|sample| sample.snapshot_attribution_ms),
            self.metric(|sample| sample.snapshot_execute_save_ms),
            self.metric(|sample| sample.snapshot_previous_state_ms),
            self.metric(|sample| sample.snapshot_previous_state_head_ms),
            self.metric(|sample| sample.snapshot_previous_state_cache_read_ms),
            self.metric(|sample| sample.snapshot_previous_state_cache_decode_ms),
            self.metric(|sample| sample.snapshot_previous_state_cache_validate_ms),
            self.metric(|sample| sample.snapshot_previous_state_store_read_ms),
            self.metric(|sample| sample.snapshot_signature_lookup_ms),
            self.metric(|sample| sample.snapshot_output_build_ms),
            self.metric(|sample| sample.monitor_ms),
            self.metric(|sample| sample.rendering_ms),
            self.metric(|sample| sample.network_ms),
        );
        println!(
            "COUNTERS case={} paths={} dirs_scanned={} dirs_skipped={} files_hashed={} monitor_paths={} object_decodes={} ref_reads={} oplog_reads={} repo_opens={} network_initialized={} ancestors_visited={} history_objects_decoded={}",
            self.kind.name(),
            self.path_count,
            self.counter(|value| value.directories_scanned),
            self.counter(|value| value.directories_skipped),
            self.counter(|value| value.files_hashed),
            self.counter(|value| value.monitor_changed_paths),
            self.counter(|value| value.object_decodes),
            self.counter(|value| value.ref_reads),
            self.counter(|value| value.oplog_reads),
            self.counter(|value| value.repository_opens),
            self.samples
                .iter()
                .any(|sample| sample.counters.network_client_initialized),
            self.counter(|value| value.ancestors_visited),
            self.counter(|value| value.history_objects_decoded),
        );
    }
}

#[derive(Clone, Copy, Debug)]
pub(super) struct Stats {
    pub p50: f64,
    pub p95: f64,
    pub p99: f64,
    pub max: f64,
}

impl Stats {
    fn from_values(mut values: Vec<f64>) -> Self {
        values.sort_by(f64::total_cmp);
        Self {
            p50: percentile(&values, 0.50),
            p95: percentile(&values, 0.95),
            p99: percentile(&values, 0.99),
            max: *values.last().expect("performance case has samples"),
        }
    }
}

impl std::fmt::Display for Stats {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            formatter,
            "p50={:.3},p95={:.3},p99={:.3},max={:.3}",
            self.p50, self.p95, self.p99, self.max
        )
    }
}

pub(super) fn measure_case(
    binary: &Path,
    mut fixture: Option<&mut PerfFixture>,
    kind: CaseKind,
    sample_count: usize,
    negative: NegativeControl,
) -> CaseResult {
    let path_count = fixture.as_ref().map_or(0, |value| value.path_count);
    let cwd = fixture
        .as_ref()
        .map_or_else(std::env::temp_dir, |value| value.root().to_path_buf());
    let mut samples = Vec::with_capacity(sample_count);
    for index in 0..sample_count {
        if let Some(value) = fixture.as_deref_mut() {
            if kind == CaseKind::CaptureOne {
                value.prepare_capture_sample();
            }
            if negative == NegativeControl::FullScan {
                value.prepare_full_scan_sample();
            }
        }
        let mut sample = run_profiled(binary, kind.args(), &cwd, negative);
        if negative == NegativeControl::DuplicateOpen {
            sample = sample.combine(run_profiled(binary, kind.args(), &cwd, negative));
        }
        assert!(
            sample.wall_ms.is_finite(),
            "sample {index} should be finite"
        );
        samples.push(sample);
    }
    CaseResult {
        kind,
        path_count,
        samples,
    }
}

fn run_profiled(binary: &Path, args: &[&str], cwd: &Path, negative: NegativeControl) -> Sample {
    let start = Instant::now();
    if negative == NegativeControl::Latency {
        std::thread::sleep(Duration::from_millis(50));
    }
    let mut command = base_command(binary, cwd);
    command.env("HEDDLE_PROFILE", "jsonl");
    if negative == NegativeControl::SubtreeSkip {
        command.env("HEDDLE_PERF_DISABLE_SUBTREE_SKIP", "1");
    }
    if negative == NegativeControl::EagerPackIndex {
        command.env("HEDDLE_PERF_FORCE_EAGER_PACK_INDEX", "1");
    }
    let output = command.args(args).output().expect("run profiled command");
    let wall_ms = start.elapsed().as_secs_f64() * 1_000.0;
    assert!(
        output.status.success(),
        "timed {args:?} failed; stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    let stderr = String::from_utf8(output.stderr).expect("profile stderr is utf8");
    let trace: Value = stderr
        .lines()
        .find_map(|line| serde_json::from_str(line).ok())
        .unwrap_or_else(|| panic!("profile trace missing from stderr: {stderr}"));
    sample_from_trace(wall_ms, &trace)
}

fn percentile(values: &[f64], quantile: f64) -> f64 {
    let index = ((values.len() as f64 * quantile).ceil() as usize)
        .saturating_sub(1)
        .min(values.len() - 1);
    values[index]
}