harn-cli 0.10.53

CLI for the Harn programming language — run, test, REPL, format, and lint
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
use std::fs;
use std::path::Path;
use std::sync::{Condvar, Mutex};
use std::time::Instant;

use super::display_path;
use crate::cli::{BenchPortableArgs, PortableEntryKindArg, ProfileArgs};
use crate::commands::portable_source::PortableSourceInput;
use harn_kernel::{
    benchmark_terminal_digest, ArtifactLimits, BenchmarkBuildProfile, BenchmarkEntryKind,
    BenchmarkProvenance, BenchmarkStatistics, BenchmarkTarget, CompileMeasurements, DataValue,
    DispatchMeasurements, EntryKind, Execution, GrantSet, PortableBenchmarkReceipt,
    ProgramArtifact, PORTABLE_BENCHMARK_SCHEMA_VERSION, PORTABLE_MAX_COMPILE_ITERATIONS,
    PORTABLE_MAX_DISPATCH_ITERATIONS, PORTABLE_MAX_WORKERS,
};

#[cfg(test)]
const SCHEMA_PATH: &str = "spec/schemas/portable-kernel-benchmark.v1.schema.json";

#[derive(Debug)]
struct StartState {
    ready: usize,
    released: bool,
    cancelled: bool,
}

pub(super) fn run(args: BenchPortableArgs, profile: &ProfileArgs) -> Result<(), String> {
    if profile.text || profile.json_path.is_some() {
        return Err(
            "`harn bench portable` does not support --profile, --profile-json, HARN_PROFILE, or HARN_PROFILE_JSON; use its versioned receipt or profile a full VM benchmark"
                .to_string(),
        );
    }
    let receipt = collect(&args)?;
    let json = serde_json::to_string_pretty(&receipt)
        .map_err(|error| format!("serialize benchmark receipt: {error}"))?;
    if let Some(path) = args.output.as_deref() {
        write(path, &json)?;
    }
    if args.json {
        println!("{json}");
    } else {
        println!(
            "Portable kernel: {}::{} ({} bytes, {} workers, {} dispatches)",
            receipt.source,
            receipt.entry,
            receipt.artifact_bytes,
            receipt.workers,
            receipt.iterations
        );
        println!(
            "First compile: {:.3} ms | repeated compile p50/p95: {:.3}/{:.3} ms | decode: {:.3}/{:.3} ms",
            receipt.compile.first_ms,
            receipt.compile.repeated.p50_ms,
            receipt.compile.repeated.p95_ms,
            receipt.decode.as_ref().expect("native decode samples").p50_ms,
            receipt.decode.as_ref().expect("native decode samples").p95_ms,
        );
        println!(
            "First dispatch: {:.3} ms | repeated dispatch p50/p95: {:.3}/{:.3} ms | batch: {:.3} ms ({:.1} dispatches/s)",
            receipt.dispatch.first_ms,
            receipt.dispatch.repeated.p50_ms,
            receipt.dispatch.repeated.p95_ms,
            receipt.dispatch.batch_wall_ms,
            receipt.dispatch.throughput_per_second,
        );
        if let Some(path) = args.output {
            println!("Receipt JSON: {}", path.display());
        }
    }
    Ok(())
}

fn collect(args: &BenchPortableArgs) -> Result<PortableBenchmarkReceipt, String> {
    if args.iterations == 0 || args.compile_iterations == 0 {
        return Err("portable benchmark iteration counts must be at least one".to_string());
    }
    if args.threads == 0 {
        return Err("portable benchmark thread count must be at least one".to_string());
    }
    if args.threads > PORTABLE_MAX_WORKERS {
        return Err(format!(
            "portable benchmark worker count must not exceed {PORTABLE_MAX_WORKERS}"
        ));
    }
    if args.iterations > PORTABLE_MAX_DISPATCH_ITERATIONS {
        return Err(format!(
            "portable benchmark dispatch iterations must not exceed {PORTABLE_MAX_DISPATCH_ITERATIONS}"
        ));
    }
    if args.compile_iterations > PORTABLE_MAX_COMPILE_ITERATIONS {
        return Err(format!(
            "portable benchmark compile iterations must not exceed {PORTABLE_MAX_COMPILE_ITERATIONS}"
        ));
    }

    let source = PortableSourceInput::load(&args.source)?;
    let input_json = read(&args.input, "input")?;
    let input = DataValue::from_json(
        serde_json::from_str(&input_json)
            .map_err(|error| format!("invalid JSON in {}: {error}", args.input.display()))?,
    )
    .map_err(|error| format!("{}: {}", error.code, error.message))?;
    let entry_kind = match args.entry_kind {
        PortableEntryKindArg::Function => EntryKind::Function,
        PortableEntryKindArg::Pipeline => EntryKind::Pipeline,
    };

    let first_compile_started = Instant::now();
    let program = source.compile(&args.entry, entry_kind.clone())?;
    let first_compile_ms = elapsed_ms(first_compile_started);

    let mut compile_samples = Vec::with_capacity(args.compile_iterations);
    for _ in 0..args.compile_iterations {
        let started = Instant::now();
        let repeated = source.compile(&args.entry, entry_kind.clone())?;
        compile_samples.push(elapsed_ms(started));
        if repeated.bytes() != program.bytes() {
            return Err(
                "portable compiler emitted different artifact bytes for identical input"
                    .to_string(),
            );
        }
    }

    let mut decode_samples = Vec::with_capacity(args.compile_iterations);
    for _ in 0..args.compile_iterations {
        let started = Instant::now();
        ProgramArtifact::decode(program.bytes(), ArtifactLimits::default())
            .map_err(|error| format!("{}: {}", error.code, error.message))?;
        decode_samples.push(elapsed_ms(started));
    }

    let first_started = Instant::now();
    let first = harn_kernel::start(&program, input.clone(), &GrantSet::pure());
    let first_dispatch_ms = elapsed_ms(first_started);
    let expected = completed_value(first)?;

    let workers = args.threads.min(args.iterations);
    let (dispatch_samples, batch_wall_ms) =
        measure_dispatch_batch(&program, &input, &expected, args.iterations, workers)?;
    if batch_wall_ms == 0.0 {
        return Err("benchmark clock did not advance for the dispatch batch".to_string());
    }
    let throughput_per_second = args.iterations as f64 * 1_000.0 / batch_wall_ms;

    let receipt = PortableBenchmarkReceipt {
        schema_version: PORTABLE_BENCHMARK_SCHEMA_VERSION.to_string(),
        target: BenchmarkTarget::Native,
        source: display_path(&args.source),
        entry: args.entry.clone(),
        entry_kind: match args.entry_kind {
            PortableEntryKindArg::Function => BenchmarkEntryKind::Function,
            PortableEntryKindArg::Pipeline => BenchmarkEntryKind::Pipeline,
        },
        artifact_bytes: program.bytes().len(),
        artifact_digest: program.digest_hex(),
        iterations: args.iterations,
        workers,
        provenance: provenance(),
        initialization_ms: None,
        compile: CompileMeasurements {
            first_ms: first_compile_ms,
            repeated: summarize(compile_samples)?,
        },
        decode: Some(summarize(decode_samples)?),
        dispatch: DispatchMeasurements {
            first_ms: first_dispatch_ms,
            repeated: summarize(dispatch_samples)?,
            batch_wall_ms,
            throughput_per_second,
        },
        terminal_digest: benchmark_terminal_digest(&expected),
    };
    receipt.validate()?;
    Ok(receipt)
}

fn measure_dispatch_batch(
    program: &ProgramArtifact,
    input: &DataValue,
    expected: &DataValue,
    iterations: usize,
    workers: usize,
) -> Result<(Vec<f64>, f64), String> {
    let gate = (
        Mutex::new(StartState {
            ready: 0,
            released: false,
            cancelled: false,
        }),
        Condvar::new(),
    );

    std::thread::scope(|scope| {
        let mut handles = Vec::with_capacity(workers);
        for worker in 0..workers {
            let count = iterations / workers + usize::from(worker < iterations % workers);
            let gate = &gate;
            let handle = std::thread::Builder::new()
                .name(format!("harn-portable-bench-{worker}"))
                .spawn_scoped(scope, move || {
                    let (state_lock, start_signal) = gate;
                    let mut state = state_lock.lock().unwrap_or_else(|error| error.into_inner());
                    state.ready += 1;
                    // Workers and the coordinator share this condition
                    // variable, so wake every waiter when readiness changes.
                    start_signal.notify_all();
                    while !state.released {
                        state = start_signal
                            .wait(state)
                            .unwrap_or_else(|error| error.into_inner());
                    }
                    if state.cancelled {
                        return Err("worker startup cancelled".to_string());
                    }
                    drop(state);

                    let mut local_samples = Vec::with_capacity(count);
                    for _ in 0..count {
                        let started = Instant::now();
                        let execution =
                            harn_kernel::start(program, input.clone(), &GrantSet::pure());
                        let elapsed = elapsed_ms(started);
                        match completed_value(execution) {
                            Ok(value) if value == *expected => local_samples.push(elapsed),
                            Ok(_) => {
                                return Err("terminal value changed".to_string());
                            }
                            Err(error) => return Err(error),
                        }
                    }
                    Ok(local_samples)
                });
            match handle {
                Ok(handle) => handles.push(handle),
                Err(error) => {
                    let (state_lock, start_signal) = &gate;
                    let mut state = state_lock.lock().unwrap_or_else(|error| error.into_inner());
                    state.released = true;
                    state.cancelled = true;
                    start_signal.notify_all();
                    drop(state);
                    for handle in handles {
                        let _ = handle.join();
                    }
                    return Err(format!(
                        "failed to create portable benchmark worker {worker}: {error}"
                    ));
                }
            }
        }

        let (state_lock, start_signal) = &gate;
        let mut state = state_lock.lock().unwrap_or_else(|error| error.into_inner());
        while state.ready < workers {
            state = start_signal
                .wait(state)
                .unwrap_or_else(|error| error.into_inner());
        }
        let batch_started = Instant::now();
        state.released = true;
        start_signal.notify_all();
        drop(state);

        let mut samples = Vec::with_capacity(iterations);
        for handle in handles {
            match handle.join() {
                Ok(Ok(worker_samples)) => samples.extend(worker_samples),
                Ok(Err(error)) => {
                    return Err(format!("portable dispatch was not deterministic: {error}"));
                }
                Err(_) => return Err("portable benchmark worker panicked".to_string()),
            }
        }
        let batch_wall_ms = elapsed_ms(batch_started);
        if samples.len() != iterations {
            return Err("portable benchmark did not record every dispatch".to_string());
        }
        Ok((samples, batch_wall_ms))
    })
}

fn provenance() -> BenchmarkProvenance {
    BenchmarkProvenance::current(
        env!("CARGO_PKG_VERSION"),
        if cfg!(debug_assertions) {
            BenchmarkBuildProfile::Debug
        } else {
            BenchmarkBuildProfile::Release
        },
        std::env::consts::OS,
        std::env::consts::ARCH,
    )
}

fn completed_value(execution: Execution) -> Result<DataValue, String> {
    match execution {
        Execution::Completed { value } => Ok(value),
        Execution::Suspended { request, .. } => Err(format!(
            "execution suspended for {}.{}",
            request.capability, request.operation
        )),
        Execution::Failed { diagnostic } => {
            Err(format!("{}: {}", diagnostic.code, diagnostic.message))
        }
    }
}

fn elapsed_ms(started: Instant) -> f64 {
    started.elapsed().as_secs_f64() * 1_000.0
}

fn summarize(samples: Vec<f64>) -> Result<BenchmarkStatistics, String> {
    BenchmarkStatistics::from_samples(samples).map_err(|error| format!("{}: {error}", error.code()))
}

fn read(path: &Path, kind: &str) -> Result<String, String> {
    fs::read_to_string(path).map_err(|error| format!("read {kind} {}: {error}", path.display()))
}

fn write(path: &Path, json: &str) -> Result<(), String> {
    if let Some(parent) = path
        .parent()
        .filter(|parent| !parent.as_os_str().is_empty())
    {
        fs::create_dir_all(parent)
            .map_err(|error| format!("create {}: {error}", parent.display()))?;
    }
    fs::write(path, format!("{json}\n"))
        .map_err(|error| format!("write {}: {error}", path.display()))
}

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

    fn object_keys(value: &serde_json::Value) -> std::collections::BTreeSet<&str> {
        value
            .as_object()
            .expect("schema/value object")
            .keys()
            .map(String::as_str)
            .collect()
    }

    fn benchmark_args(dir: &Path, threads: usize) -> BenchPortableArgs {
        let source = dir.join("reducer.harn");
        let input = dir.join("event.json");
        fs::write(
            &source,
            "fn reduce(input) { return {count: input.count + 1} }",
        )
        .unwrap();
        fs::write(&input, r#"{"count": 41}"#).unwrap();
        BenchPortableArgs {
            source,
            entry: "reduce".to_string(),
            entry_kind: PortableEntryKindArg::Function,
            input,
            iterations: 8,
            threads,
            compile_iterations: 2,
            json: false,
            output: None,
        }
    }

    #[test]
    fn receipt_identity_is_stable_across_thread_counts() {
        let dir = tempfile::tempdir().unwrap();
        let serial = collect(&benchmark_args(dir.path(), 1)).unwrap();
        let parallel = collect(&benchmark_args(dir.path(), 4)).unwrap();

        assert_eq!(serial.schema_version, PORTABLE_BENCHMARK_SCHEMA_VERSION);
        assert_eq!(serial.artifact_digest, parallel.artifact_digest);
        assert_eq!(serial.terminal_digest, parallel.terminal_digest);
        assert_eq!(serial.compile.repeated.iterations, 2);
        assert_eq!(parallel.compile.repeated.iterations, 2);
        assert_eq!(serial.dispatch.repeated.iterations, 8);
        assert_eq!(parallel.dispatch.repeated.iterations, 8);
        assert_eq!(serial.workers, 1);
        assert_eq!(parallel.workers, 4);
        assert!(parallel.dispatch.batch_wall_ms > 0.0);
        assert!(parallel.dispatch.throughput_per_second > 0.0);
        assert_eq!(
            parallel.provenance.artifact_format_version,
            harn_kernel::ARTIFACT_VERSION
        );
        assert_eq!(parallel.provenance.semantic_abi_fingerprint.len(), 64);
        assert_eq!(parallel.provenance.opcode_abi_fingerprint.len(), 64);

        let repo_root = Path::new(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .and_then(Path::parent)
            .unwrap();
        let schema: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(repo_root.join(SCHEMA_PATH)).unwrap())
                .unwrap();
        jsonschema::draft202012::meta::validate(&schema).unwrap();
        let validator = jsonschema::draft202012::new(&schema).unwrap();
        let instance = serde_json::to_value(&parallel).unwrap();
        validator.validate(&instance).unwrap();
        assert_eq!(object_keys(&instance), object_keys(&schema["properties"]));
        assert_eq!(
            object_keys(&instance["provenance"]),
            object_keys(&schema["$defs"]["provenance"]["properties"])
        );
        assert_eq!(
            object_keys(&instance["compile"]),
            object_keys(&schema["$defs"]["compileMeasurements"]["properties"])
        );
        assert_eq!(
            object_keys(&instance["dispatch"]),
            object_keys(&schema["$defs"]["dispatchMeasurements"]["properties"])
        );
        assert_eq!(
            object_keys(&instance["dispatch"]["repeated"]),
            object_keys(&schema["$defs"]["statistics"]["properties"])
        );

        let mut browser_instance = instance.clone();
        browser_instance["target"] = serde_json::json!("browser");
        browser_instance["initializationMs"] = serde_json::json!(1.0);
        browser_instance["decode"] = serde_json::Value::Null;
        validator.validate(&browser_instance).unwrap();

        let mut unexpected_field = instance.clone();
        unexpected_field
            .as_object_mut()
            .unwrap()
            .insert("undocumented".to_string(), serde_json::Value::Bool(true));
        assert!(!validator.is_valid(&unexpected_field));

        let mut too_many_workers = instance.clone();
        too_many_workers["workers"] = serde_json::json!(PORTABLE_MAX_WORKERS + 1);
        assert!(!validator.is_valid(&too_many_workers));

        let mut too_many_dispatches = instance.clone();
        too_many_dispatches["iterations"] = serde_json::json!(PORTABLE_MAX_DISPATCH_ITERATIONS + 1);
        assert!(!validator.is_valid(&too_many_dispatches));

        let mut too_many_compiles = instance;
        too_many_compiles["compile"]["repeated"]["iterations"] =
            serde_json::json!(PORTABLE_MAX_COMPILE_ITERATIONS + 1);
        assert!(!validator.is_valid(&too_many_compiles));
    }

    #[test]
    fn rejects_unbounded_thread_requests_before_reading_inputs() {
        let dir = tempfile::tempdir().unwrap();
        let args = benchmark_args(dir.path(), PORTABLE_MAX_WORKERS + 1);

        assert_eq!(
            collect(&args).unwrap_err(),
            "portable benchmark worker count must not exceed 256"
        );
    }

    #[test]
    fn rejects_full_vm_profile_outputs_explicitly() {
        let dir = tempfile::tempdir().unwrap();
        let text_profile = ProfileArgs {
            text: true,
            json_path: None,
        };

        assert!(run(benchmark_args(dir.path(), 1), &text_profile)
            .unwrap_err()
            .contains("does not support --profile"));

        let json_profile = ProfileArgs {
            text: false,
            json_path: Some(dir.path().join("profile.json")),
        };
        assert!(run(benchmark_args(dir.path(), 1), &json_profile)
            .unwrap_err()
            .contains("does not support --profile"));
    }
}