piano 0.15.0

Automatic instrumentation-based profiler for Rust. Measures self-time, call counts, and heap allocations per function.
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
//! Consolidated async integration tests.
//!
//! Tests in this module cover:
//! - async allocation tracking (alloc_count / alloc_bytes in NDJSON output)
//! - async main with return type (`async fn main() -> Result<...>`)
//! - nested `tokio::select!` self-time accuracy
//! - async self-time accuracy across thread migrations
//! - basic async tokio pipeline (instrument, build, run, verify output)
//!
//! All tests share a single pre-compiled tokio seed via `common::tokio_seed()`
//! so each `piano build` only recompiles the user crate (~0.7s vs ~3.5s).

mod common;

use std::fs;
use std::path::Path;
use std::process::Command;

// ---------------------------------------------------------------------------
// Project scaffolding helpers
// ---------------------------------------------------------------------------

fn create_async_alloc_project(dir: &Path) {
    fs::create_dir_all(dir.join("src")).unwrap();

    fs::write(
        dir.join("Cargo.toml"),
        r#"[package]
name = "async-alloc-test"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "async-alloc-test"
path = "src/main.rs"

[dependencies]
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] }
"#,
    )
    .unwrap();

    // The program allocates in an async function with .await points.
    // Vec::with_capacity forces a real heap allocation.
    // Uses a sync wrapper that calls the async function via block_on,
    // ensuring the depth-0 guard boundary flushes the per-thread buffer
    // to the file sink so NDJSON output is produced.
    fs::write(
        dir.join("src").join("main.rs"),
        r#"
async fn allocating_work() -> Vec<u8> {
    let mut data = Vec::with_capacity(1024);
    data.extend_from_slice(&[1u8; 512]);
    tokio::task::yield_now().await;
    data.extend_from_slice(&[2u8; 512]);
    data
}

fn wrapper() {
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();
    let result = rt.block_on(allocating_work());
    println!("len: {}", result.len());
}

fn main() {
    wrapper();
}
"#,
    )
    .unwrap();
}

fn create_async_main_return_type_project(dir: &Path) {
    fs::create_dir_all(dir.join("src")).unwrap();

    fs::write(
        dir.join("Cargo.toml"),
        r#"[package]
name = "async-main-result"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "async-main-result"
path = "src/main.rs"

[dependencies]
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] }
"#,
    )
    .unwrap();

    fs::write(
        dir.join("src").join("main.rs"),
        r#"use std::io;

async fn do_work() -> u64 {
    let mut sum = 0u64;
    for i in 0..1000 {
        sum += i;
    }
    sum
}

#[tokio::main]
async fn main() -> Result<(), io::Error> {
    let result = do_work().await;
    println!("result: {result}");
    Ok(())
}
"#,
    )
    .unwrap();
}

fn create_nested_select_project(dir: &Path) {
    fs::create_dir_all(dir.join("src")).unwrap();

    fs::write(
        dir.join("Cargo.toml"),
        r#"[package]
name = "nested-select-test"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "nested-select-test"
path = "src/main.rs"

[dependencies]
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] }
"#,
    )
    .unwrap();

    // The program has a parent that uses select! over two children.
    // Each child sleeps for 50ms. The parent does no computation.
    // select! cancellation must not inflate the parent's self_ns.
    // PianoFuture tracks per-poll wall time, so self_ns should be ~0.
    fs::write(
        dir.join("src").join("main.rs"),
        r#"use tokio::time::{sleep, Duration};

async fn child_a() -> &'static str {
    sleep(Duration::from_millis(50)).await;
    "a"
}

async fn child_b() -> &'static str {
    sleep(Duration::from_millis(60)).await;
    "b"
}

async fn parent_select() -> &'static str {
    tokio::select! {
        result = child_a() => result,
        result = child_b() => result,
    }
}

#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
async fn main() {
    // Run select multiple times to accumulate timing data.
    for _ in 0..5 {
        let result = parent_select().await;
        println!("winner: {result}");
    }
}
"#,
    )
    .unwrap();
}

fn create_async_self_time_project(dir: &Path) {
    fs::create_dir_all(dir.join("src")).unwrap();

    fs::write(
        dir.join("Cargo.toml"),
        r#"[package]
name = "async-selftime"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "async-selftime"
path = "src/main.rs"

[dependencies]
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] }
"#,
    )
    .unwrap();

    // The program has a clear parent/child hierarchy:
    // - parent_fn calls expensive_child twice via .await
    // - expensive_child is a leaf that does heavy computation
    // - parent_fn's self_time should be negligible compared to its wall time
    //
    // Multiple tasks + yield_now maximize migration likelihood on multi-thread rt.
    fs::write(
        dir.join("src").join("main.rs"),
        r#"use tokio::task;

async fn expensive_child() -> u64 {
    use std::hint::black_box;
    let mut sum = 0u64;
    for i in 0..2_000_000u64 {
        sum = sum.wrapping_add(black_box(i));
    }
    // Yield to give the scheduler a migration opportunity
    task::yield_now().await;
    let mut sum2 = 0u64;
    for i in 0..2_000_000u64 {
        sum2 = sum2.wrapping_add(black_box(i));
    }
    sum.wrapping_add(sum2)
}

async fn parent_fn() -> u64 {
    let a = expensive_child().await;
    let b = expensive_child().await;
    a.wrapping_add(b)
}

#[tokio::main(flavor = "multi_thread", worker_threads = 4)]
async fn main() {
    // Spawn several concurrent tasks to increase scheduling pressure
    let mut handles = Vec::new();
    for _ in 0..4 {
        handles.push(task::spawn(parent_fn()));
    }
    let mut total = 0u64;
    for h in handles {
        total = total.wrapping_add(h.await.unwrap());
    }
    println!("total: {total}");
}
"#,
    )
    .unwrap();
}

fn create_impl_future_project(dir: &Path) {
    fs::create_dir_all(dir.join("src")).unwrap();

    fs::write(
        dir.join("Cargo.toml"),
        r#"[package]
name = "impl-future-test"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "impl-future-test"
path = "src/main.rs"

[dependencies]
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] }
"#,
    )
    .unwrap();

    // foo() returns impl Future with 80ms sleep -- foo does the work.
    // bar() is async fn that just awaits foo() -- bar does no work.
    // If instrumentation is correct: foo.self_ns >> bar.self_ns.
    // If broken (old behavior): foo.self_ns ~= 0, bar.self_ns absorbs 80ms.
    fs::write(
        dir.join("src").join("main.rs"),
        r#"use std::future::Future;
use tokio::time::{sleep, Duration};

fn foo() -> impl Future<Output = ()> {
    async {
        sleep(Duration::from_millis(80)).await;
    }
}

async fn bar() {
    foo().await;
}

#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
async fn main() {
    for _ in 0..3 {
        bar().await;
    }
}
"#,
    )
    .unwrap();
}

/// Create a small tokio project with async functions.
fn create_async_project(dir: &Path) {
    fs::create_dir_all(dir.join("src")).unwrap();

    fs::write(
        dir.join("Cargo.toml"),
        r#"[package]
name = "async-test"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "async-test"
path = "src/main.rs"

[dependencies]
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] }
"#,
    )
    .unwrap();

    fs::write(
        dir.join("src").join("main.rs"),
        r#"async fn compute(x: u64) -> u64 {
    let mut sum = 0u64;
    for i in 0..x {
        sum += i;
    }
    sum
}

async fn orchestrate() -> u64 {
    let a = compute(1000).await;
    let b = compute(2000).await;
    a + b
}

#[tokio::main]
async fn main() {
    let result = orchestrate().await;
    println!("result: {result}");
}
"#,
    )
    .unwrap();
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[test]
fn async_alloc_tracking_pipeline() {
    let tmp = tempfile::tempdir().unwrap();
    let project_dir = tmp.path().join("async-alloc-test");
    create_async_alloc_project(&project_dir);
    common::prepopulate_deps(&project_dir, common::tokio_seed());

    let piano_bin = env!("CARGO_BIN_EXE_piano");
    let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
    let runtime_path = manifest_dir.join("piano-runtime");

    // Build with piano -- instrument allocating_work, wrapper, and main.
    // wrapper is the sync depth-0 boundary that triggers frame flush.
    let output = Command::new(piano_bin)
        .args([
            "build",
            "--fn",
            "allocating_work",
            "--fn",
            "wrapper",
            "--fn",
            "main",
            "--project",
        ])
        .arg(&project_dir)
        .arg("--runtime-path")
        .arg(&runtime_path)
        .output()
        .expect("failed to run piano build");

    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(
        output.status.success(),
        "piano build failed:\nstderr: {stderr}\nstdout: {stdout}"
    );

    let binary_path = stdout.trim();
    assert!(
        Path::new(binary_path).exists(),
        "built binary should exist at: {binary_path}"
    );

    // Run the instrumented binary.
    let runs_dir = tmp.path().join("runs");
    fs::create_dir_all(&runs_dir).unwrap();

    let run_output = Command::new(binary_path)
        .env("PIANO_RUNS_DIR", &runs_dir)
        .output()
        .expect("failed to run instrumented binary");

    assert!(
        run_output.status.success(),
        "instrumented binary failed:\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&run_output.stdout),
        String::from_utf8_lossy(&run_output.stderr)
    );

    // Program should produce correct output.
    let program_stdout = String::from_utf8_lossy(&run_output.stdout);
    assert!(
        program_stdout.contains("len: 1024"),
        "program should produce correct output, got: {program_stdout}"
    );

    // Find run output file.
    let run_file = common::largest_ndjson_file(&runs_dir);
    let content = fs::read_to_string(&run_file).unwrap();

    // NDJSON format:
    //   Header: {"type":"header","names":{"0":"allocating_work",...}}
    //   Measurement: {"span_id":N,"parent_span_id":N,"name_id":N,...,"alloc_count":N,"alloc_bytes":N}
    //   Trailer: {"type":"trailer","names":{"0":"allocating_work",...}}

    let stats = common::aggregate_ndjson(&content);

    let alloc_stats = stats
        .get("allocating_work")
        .expect("allocating_work should appear in output");

    assert!(
        alloc_stats.alloc_count > 0,
        "allocating_work should have non-zero alloc_count in NDJSON output.\nContent:\n{content}"
    );
}

#[test]
fn async_main_with_return_type() {
    let tmp = tempfile::tempdir().unwrap();
    let project_dir = tmp.path().join("async-main-result");
    create_async_main_return_type_project(&project_dir);
    common::prepopulate_deps(&project_dir, common::tokio_seed());

    let piano_bin = env!("CARGO_BIN_EXE_piano");
    let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
    let runtime_path = manifest_dir.join("piano-runtime");

    let build = Command::new(piano_bin)
        .args(["build", "--fn", "do_work", "--fn", "main", "--project"])
        .arg(&project_dir)
        .arg("--runtime-path")
        .arg(&runtime_path)
        .output()
        .expect("failed to run piano build");

    let stderr = String::from_utf8_lossy(&build.stderr);
    let stdout = String::from_utf8_lossy(&build.stdout);
    assert!(
        build.status.success(),
        "piano build failed:\nstderr: {stderr}\nstdout: {stdout}"
    );

    let binary_path = stdout.trim();
    assert!(
        Path::new(binary_path).exists(),
        "built binary should exist at: {binary_path}"
    );

    let runs_dir = tmp.path().join("runs");
    fs::create_dir_all(&runs_dir).unwrap();

    let run = Command::new(binary_path)
        .env("PIANO_RUNS_DIR", &runs_dir)
        .output()
        .expect("failed to run instrumented binary");

    assert!(
        run.status.success(),
        "instrumented binary should exit cleanly:\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&run.stdout),
        String::from_utf8_lossy(&run.stderr),
    );

    let program_stdout = String::from_utf8_lossy(&run.stdout);
    assert!(
        program_stdout.contains("result: 499500"),
        "program should produce correct output, got: {program_stdout}"
    );

    let run_file = common::largest_ndjson_file(&runs_dir);
    let content = fs::read_to_string(&run_file).unwrap();
    let stats = common::aggregate_ndjson(&content);

    assert!(
        stats.contains_key("do_work"),
        "output should contain do_work. Got:\n{content}"
    );
}

#[test]
fn async_nested_select_self_time() {
    let tmp = tempfile::tempdir().unwrap();
    let project_dir = tmp.path().join("nested-select-test");
    create_nested_select_project(&project_dir);
    common::prepopulate_deps(&project_dir, common::tokio_seed());

    let piano_bin = env!("CARGO_BIN_EXE_piano");
    let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
    let runtime_path = manifest_dir.join("piano-runtime");

    let build = Command::new(piano_bin)
        .args([
            "build",
            "--fn",
            "child_a",
            "--fn",
            "child_b",
            "--fn",
            "parent_select",
            "--fn",
            "main",
            "--project",
        ])
        .arg(&project_dir)
        .arg("--runtime-path")
        .arg(&runtime_path)
        .output()
        .expect("failed to run piano build");

    let stderr = String::from_utf8_lossy(&build.stderr);
    let stdout = String::from_utf8_lossy(&build.stdout);
    assert!(
        build.status.success(),
        "piano build failed:\nstderr: {stderr}\nstdout: {stdout}"
    );

    let binary_path = stdout.trim();
    assert!(
        Path::new(binary_path).exists(),
        "built binary should exist at: {binary_path}"
    );

    let runs_dir = tmp.path().join("runs");
    fs::create_dir_all(&runs_dir).unwrap();

    let run = Command::new(binary_path)
        .env("PIANO_RUNS_DIR", &runs_dir)
        .output()
        .expect("failed to run instrumented binary");

    assert!(
        run.status.success(),
        "instrumented binary failed:\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&run.stdout),
        String::from_utf8_lossy(&run.stderr),
    );

    let run_file = common::largest_ndjson_file(&runs_dir);
    let content = fs::read_to_string(&run_file).unwrap();
    let stats = common::aggregate_ndjson(&content);

    // All instrumented functions should appear.
    assert!(
        stats.contains_key("parent_select"),
        "output should contain parent_select. Got:\n{content}"
    );
    assert!(
        stats.contains_key("child_a"),
        "output should contain child_a. Got:\n{content}"
    );

    // parent_select does zero computation -- its body is just a select! macro.
    // Its self_ns should be much smaller than child_a's self_ns (~50ms of sleep).
    let parent = stats.get("parent_select").unwrap();
    let child = stats.get("child_a").unwrap();
    assert!(
        child.self_ns > 0,
        "child_a should have non-zero self_ns (it sleeps 50ms)"
    );
    assert!(
        parent.self_ns < child.self_ns,
        "parent_select.self_ns ({}) must be < child_a.self_ns ({}) -- \
         parent does no computation, select! overhead should be negligible",
        parent.self_ns,
        child.self_ns,
    );
}

#[test]
fn async_self_time_accuracy() {
    let tmp = tempfile::tempdir().unwrap();
    let project_dir = tmp.path().join("async-selftime");
    create_async_self_time_project(&project_dir);
    common::prepopulate_deps(&project_dir, common::tokio_seed());

    let piano_bin = env!("CARGO_BIN_EXE_piano");
    let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
    let runtime_path = manifest_dir.join("piano-runtime");

    // Build: instrument parent_fn and expensive_child.
    let build = Command::new(piano_bin)
        .args([
            "build",
            "--fn",
            "parent_fn",
            "--fn",
            "expensive_child",
            "--fn",
            "main",
            "--project",
        ])
        .arg(&project_dir)
        .arg("--runtime-path")
        .arg(&runtime_path)
        .output()
        .expect("failed to run piano build");

    let stderr = String::from_utf8_lossy(&build.stderr);
    let stdout = String::from_utf8_lossy(&build.stdout);
    assert!(
        build.status.success(),
        "piano build failed:\nstderr: {stderr}\nstdout: {stdout}"
    );

    let binary_path = stdout.trim();
    assert!(
        Path::new(binary_path).exists(),
        "built binary should exist at: {binary_path}"
    );

    // Run the instrumented binary.
    let runs_dir = tmp.path().join("runs");
    fs::create_dir_all(&runs_dir).unwrap();

    let run = Command::new(binary_path)
        .env("PIANO_RUNS_DIR", &runs_dir)
        .output()
        .expect("failed to run instrumented binary");

    let run_stderr = String::from_utf8_lossy(&run.stderr);
    assert!(
        run.status.success(),
        "instrumented binary failed:\nstdout: {}\nstderr: {run_stderr}",
        String::from_utf8_lossy(&run.stdout),
    );

    // Parse NDJSON output (always written by shutdown).
    let run_file = common::largest_ndjson_file(&runs_dir);
    let content = fs::read_to_string(&run_file).unwrap();
    let stats = common::aggregate_ndjson(&content);

    // Verify functions appear in the output.
    assert!(
        stats.contains_key("parent_fn"),
        "output should contain parent_fn. Got:\n{content}"
    );
    assert!(
        stats.contains_key("expensive_child"),
        "output should contain expensive_child. Got:\n{content}"
    );

    // Structural assertion derived from the program, not an arbitrary threshold.
    //
    // By construction:
    //   parent_fn does ZERO computation (body is just two .await calls)
    //   expensive_child does 4M wrapping_add iterations (black_box prevents folding)
    //
    // Therefore: parent_fn.self ~ 0  and  expensive_child.self >> 0
    // So: parent_fn.self_ns < expensive_child.self_ns  (always true if accounting works)
    //
    // PianoFuture carries the call stack inside each future's state machine,
    // so parent-child subtraction is correct regardless of thread migration.
    let child_self = stats.get("expensive_child").unwrap().self_ns;
    assert!(
        child_self > 0,
        "expensive_child should have non-zero self_ns (it does real work)"
    );

    let parent_stats = stats
        .get("parent_fn")
        .expect("parent_fn should appear in output");
    assert!(
        parent_stats.self_ns < child_self,
        "parent_fn.self_ns ({}) must be < expensive_child.self_ns \
         ({child_self}) -- parent does no computation, child does all of it",
        parent_stats.self_ns,
    );

    // PianoFuture carries real function names; no "<migrated>" bucket.
    assert!(
        !stats.contains_key("<migrated>"),
        "should not have a <migrated> bucket"
    );
}

#[test]
fn async_tokio_pipeline() {
    let tmp = tempfile::tempdir().unwrap();
    let project_dir = tmp.path().join("async-test");
    create_async_project(&project_dir);
    common::prepopulate_deps(&project_dir, common::tokio_seed());

    let piano_bin = env!("CARGO_BIN_EXE_piano");
    let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
    let runtime_path = manifest_dir.join("piano-runtime");

    // Run piano build on the async project -- instrument all three functions.
    let output = Command::new(piano_bin)
        .args([
            "build",
            "--fn",
            "compute",
            "--fn",
            "orchestrate",
            "--fn",
            "main",
            "--project",
        ])
        .arg(&project_dir)
        .arg("--runtime-path")
        .arg(&runtime_path)
        .output()
        .expect("failed to run piano build");

    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(
        output.status.success(),
        "piano build failed:\nstderr: {stderr}\nstdout: {stdout}"
    );

    // Async functions must be instrumented, not skipped.
    // Verify no "skipped" + "async" message appears in stderr.
    let stderr_lower = stderr.to_lowercase();
    assert!(
        !(stderr_lower.contains("skipped") && stderr_lower.contains("async")),
        "stderr should not contain async-skip warnings, got:\n{stderr}"
    );

    let binary_path = stdout.trim();
    assert!(
        Path::new(binary_path).exists(),
        "built binary should exist at: {binary_path}"
    );

    // Run the instrumented binary.
    let runs_dir = tmp.path().join("runs");
    fs::create_dir_all(&runs_dir).unwrap();

    let run_output = Command::new(binary_path)
        .env("PIANO_RUNS_DIR", &runs_dir)
        .output()
        .expect("failed to run instrumented binary");

    assert!(
        run_output.status.success(),
        "instrumented binary panicked or failed:\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&run_output.stdout),
        String::from_utf8_lossy(&run_output.stderr)
    );

    // The program should still produce the correct result.
    let program_stdout = String::from_utf8_lossy(&run_output.stdout);
    assert!(
        program_stdout.contains("result: 2498500"),
        "program should produce correct output, got: {program_stdout}"
    );

    // Read the output and verify async function names appear.
    let run_file = common::largest_ndjson_file(&runs_dir);
    let content = fs::read_to_string(&run_file).unwrap();
    assert!(
        content.contains("compute"),
        "output should contain instrumented async function 'compute'. Got:\n{content}"
    );
    assert!(
        content.contains("orchestrate"),
        "output should contain instrumented async function 'orchestrate'. Got:\n{content}"
    );
}

#[test]
fn impl_future_self_time_attribution() {
    let tmp = tempfile::tempdir().unwrap();
    let project_dir = tmp.path().join("impl-future-test");
    create_impl_future_project(&project_dir);
    common::prepopulate_deps(&project_dir, common::tokio_seed());

    let piano_bin = env!("CARGO_BIN_EXE_piano");
    let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
    let runtime_path = manifest_dir.join("piano-runtime");

    let build = Command::new(piano_bin)
        .args([
            "build",
            "--fn",
            "foo",
            "--fn",
            "bar",
            "--fn",
            "main",
            "--project",
        ])
        .arg(&project_dir)
        .arg("--runtime-path")
        .arg(&runtime_path)
        .output()
        .expect("failed to run piano build");

    let stderr = String::from_utf8_lossy(&build.stderr);
    let stdout = String::from_utf8_lossy(&build.stdout);
    assert!(
        build.status.success(),
        "piano build failed:\nstderr: {stderr}\nstdout: {stdout}"
    );

    let binary_path = stdout.trim();
    assert!(
        Path::new(binary_path).exists(),
        "built binary should exist at: {binary_path}"
    );

    let runs_dir = tmp.path().join("runs");
    fs::create_dir_all(&runs_dir).unwrap();

    let run = Command::new(binary_path)
        .env("PIANO_RUNS_DIR", &runs_dir)
        .output()
        .expect("failed to run instrumented binary");

    assert!(
        run.status.success(),
        "instrumented binary failed:\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&run.stdout),
        String::from_utf8_lossy(&run.stderr),
    );

    let run_file = common::largest_ndjson_file(&runs_dir);
    let content = fs::read_to_string(&run_file).unwrap();
    let stats = common::aggregate_ndjson(&content);

    assert!(
        stats.contains_key("foo"),
        "output should contain foo. Got:\n{content}"
    );
    assert!(
        stats.contains_key("bar"),
        "output should contain bar. Got:\n{content}"
    );

    let foo = stats.get("foo").unwrap();
    let bar = stats.get("bar").unwrap();

    // foo does the work (80ms sleep x3 calls). bar does nothing.
    // foo.self_ns must be greater than bar.self_ns.
    assert!(
        foo.self_ns > bar.self_ns,
        "foo.self_ns ({}) must be > bar.self_ns ({}) -- \
         foo does the 80ms sleep, bar just awaits foo",
        foo.self_ns,
        bar.self_ns,
    );
}