Skip to content

Commit 279082e

Browse files
committed
Improve the benchmark and address comments
1 parent 835750f commit 279082e

File tree

1 file changed

+61
-11
lines changed
  • turbopack/crates/turbo-tasks-backend/benches

1 file changed

+61
-11
lines changed

turbopack/crates/turbo-tasks-backend/benches/overhead.rs

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn overhead(c: &mut Criterion) {
2626
register();
2727

2828
let mut group = c.benchmark_group("task_overhead");
29-
group.sample_size(50);
29+
group.sample_size(100);
3030
// Test durations between 10us and 1ms
3131
for micros in [1, 10, 50, 100, 500, 1000] {
3232
let duration = Duration::from_micros(micros);
@@ -35,28 +35,76 @@ pub fn overhead(c: &mut Criterion) {
3535
b.iter(|| busy_task(black_box(d)))
3636
});
3737

38-
group.bench_with_input(BenchmarkId::new("turbo", micros), &duration, |b, &d| {
39-
run_turbo::<false>(b, d);
40-
});
38+
group.bench_with_input(
39+
BenchmarkId::new("turbo-uncached", micros),
40+
&duration,
41+
|b, &d| {
42+
run_turbo::<Uncached>(b, d);
43+
},
44+
);
4145

4246
group.bench_with_input(
43-
BenchmarkId::new("turbo-cached", micros),
47+
BenchmarkId::new("turbo-cached-same", micros),
4448
&duration,
4549
|b, &d| {
46-
run_turbo::<true>(b, d);
50+
run_turbo::<CachedSame>(b, d);
51+
},
52+
);
53+
54+
group.bench_with_input(
55+
BenchmarkId::new("turbo-cached-different", micros),
56+
&duration,
57+
|b, &d| {
58+
run_turbo::<CachedDifferent>(b, d);
4759
},
4860
);
4961
}
5062
group.finish();
5163
}
5264

53-
fn run_turbo<const CACHED: bool>(b: &mut criterion::Bencher<'_>, d: Duration) {
65+
trait TurboMode {
66+
fn key(index: u64) -> u64;
67+
fn is_cached() -> bool;
68+
}
69+
struct Uncached;
70+
impl TurboMode for Uncached {
71+
fn key(index: u64) -> u64 {
72+
index
73+
}
74+
75+
fn is_cached() -> bool {
76+
false
77+
}
78+
}
79+
struct CachedSame;
80+
impl TurboMode for CachedSame {
81+
fn key(_index: u64) -> u64 {
82+
0
83+
}
84+
85+
fn is_cached() -> bool {
86+
true
87+
}
88+
}
89+
struct CachedDifferent;
90+
impl TurboMode for CachedDifferent {
91+
fn key(index: u64) -> u64 {
92+
index
93+
}
94+
95+
fn is_cached() -> bool {
96+
true
97+
}
98+
}
99+
fn run_turbo<Mode: TurboMode>(b: &mut criterion::Bencher<'_>, d: Duration) {
54100
let rt = tokio::runtime::Builder::new_multi_thread()
55101
.enable_all()
56102
.build()
57103
.unwrap();
58104

59105
b.to_async(rt).iter_custom(|iters| {
106+
// It is important to create the tt instance here to ensure the cache is not shared across
107+
// iterations.
60108
let tt = TurboTasks::new(TurboTasksBackend::new(
61109
BackendOptions {
62110
storage_mode: None,
@@ -67,13 +115,15 @@ fn run_turbo<const CACHED: bool>(b: &mut criterion::Bencher<'_>, d: Duration) {
67115

68116
async move {
69117
tt.run_once(async move {
70-
// If cached run once outside the loop to ensure the task is cached.
71-
if CACHED {
72-
busy_turbo(0, black_box(d)).await?;
118+
// If cached run once outside the loop to ensure the tasks are cached.
119+
if Mode::is_cached() {
120+
for i in 0..iters {
121+
black_box(busy_turbo(i, black_box(d)).await?);
122+
}
73123
}
74124
let start = Instant::now();
75125
for i in 0..iters {
76-
black_box(busy_turbo(if CACHED { 0 } else { i }, black_box(d)).await?);
126+
black_box(busy_turbo(Mode::key(i), black_box(d)).await?);
77127
}
78128
Ok(start.elapsed())
79129
})

0 commit comments

Comments
 (0)