File size: 8,147 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this

use std::{future::Future, time::Duration};

use anyhow::Result;
use turbo_tasks::{TransientInstance, Vc};
use turbo_tasks_testing::{Registration, register, run};

static REGISTRATION: Registration = register!();

const COUNT1: u32 = 100;
const COUNT2: u32 = 2000;

async fn run_test<F, T>(
    f: impl Fn() -> F + Sync + Send + Clone + 'static,
    limit: Duration,
) -> anyhow::Result<()>
where
    F: Future<Output = anyhow::Result<T>> + Sync + Send + 'static,
{
    // The first call will actually execute everything.
    let start = std::time::Instant::now();
    f().await?;
    println!("Initial call took {:?}", start.elapsed());

    let mut warmup_calls = Vec::new();

    for _ in 0..10 {
        let start = std::time::Instant::now();
        f().await?;
        let warmup_call = start.elapsed();
        println!("Subsequent call took {warmup_call:?}");
        warmup_calls.push(warmup_call);
    }

    // Susbsequent calls should be very fast.
    let start = std::time::Instant::now();
    for _ in 0..COUNT1 {
        f().await?;
    }
    let subsequent = start.elapsed();
    println!(
        "First {} subsequent calls took {:?} ({:?} per call)",
        COUNT1,
        subsequent,
        subsequent / COUNT1
    );

    let start = std::time::Instant::now();
    for _ in 0..COUNT1 {
        f().await?;
    }
    let subsequent2 = start.elapsed();
    println!(
        "Another {} subsequent calls took {:?} ({:?} per call)",
        COUNT1,
        subsequent2,
        subsequent2 / COUNT1
    );

    let start = std::time::Instant::now();
    for _ in 0..COUNT1 {
        f().await?;
    }
    let subsequent3 = start.elapsed();
    println!(
        "Another {} subsequent calls took {:?} ({:?} per call)",
        COUNT1,
        subsequent3,
        subsequent3 / COUNT1
    );

    if subsequent2 * 2 > subsequent * 3 || subsequent3 * 2 > subsequent * 3 {
        // Performance regresses with more calls
        // Check if this fixes itself eventually
        for i in 0.. {
            let start = std::time::Instant::now();
            for _ in 0..COUNT1 {
                f().await?;
            }
            let subsequent4 = start.elapsed();
            println!(
                "Another {} subsequent calls took {:?} ({:?} per call)",
                COUNT1,
                subsequent4,
                subsequent4 / COUNT1
            );
            if subsequent4 * 2 < subsequent * 3 {
                break;
            }
            if i >= 20 {
                panic!("Performance regressed with more calls");
            }
        }
    }

    let start = std::time::Instant::now();
    f().await?;
    let final_call = start.elapsed();
    println!("Final call took {final_call:?}");

    let target = subsequent / COUNT1;

    for (i, warmup_call) in warmup_calls.into_iter().enumerate() {
        assert!(
            warmup_call < target * 10,
            "Warmup call {} should be less than {:?}",
            i,
            target * 10
        );
    }

    assert!(
        subsequent < limit * COUNT1,
        "Each call should be less than {limit:?}"
    );

    assert!(
        subsequent2 < limit * COUNT1,
        "Each call should be less than {limit:?}"
    );

    assert!(
        subsequent3 < limit * COUNT1,
        "Each call should be less than {limit:?}"
    );

    anyhow::Ok(())
}

fn check_skip() -> bool {
    if matches!(
        std::env::var("TURBOPACK_TEST_PERFORMANCE").ok().as_deref(),
        None | Some("") | Some("no") | Some("false")
    ) {
        println!("Skipping test, pass `TURBOPACK_TEST_PERFORMANCE=yes` to run it");
        return true;
    }

    false
}

#[tokio::test]
async fn many_calls_to_many_children() {
    if check_skip() {
        return;
    }
    run(&REGISTRATION, || {
        run_test(
            || calls_many_children(TransientInstance::new(()), None).strongly_consistent(),
            Duration::from_micros(100),
        )
    })
    .await
    .unwrap();
}

#[tokio::test]
async fn many_calls_to_uncached_many_children() {
    if check_skip() {
        return;
    }
    run(&REGISTRATION, || {
        run_test(
            || {
                calls_many_children(TransientInstance::new(()), Some(TransientInstance::new(())))
                    .strongly_consistent()
            },
            Duration::from_micros(100) * COUNT2,
        )
    })
    .await
    .unwrap();
}

fn run_big_graph_test(counts: Vec<u32>) -> impl Future<Output = Result<()>> + Send + 'static {
    println!(
        "Graph {:?} = {} tasks",
        counts,
        (1..=counts.len())
            .map(|i| counts.iter().take(i).product::<u32>())
            .sum::<u32>()
    );
    run_test(
        move || calls_big_graph(counts.clone(), TransientInstance::new(())).strongly_consistent(),
        Duration::from_micros(100),
    )
}

#[tokio::test]
async fn many_calls_to_big_graph_1() {
    if check_skip() {
        return;
    }
    run(&REGISTRATION, || run_big_graph_test(vec![5, 8, 10, 15, 20]))
        .await
        .unwrap();
}

#[tokio::test]
async fn many_calls_to_big_graph_2() {
    if check_skip() {
        return;
    }
    run(&REGISTRATION, || {
        run_big_graph_test(vec![2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
    })
    .await
    .unwrap();
}

#[tokio::test]
async fn many_calls_to_big_graph_3() {
    if check_skip() {
        return;
    }
    run(&REGISTRATION, || run_big_graph_test(vec![1000, 3, 3, 3, 3]))
        .await
        .unwrap();
}

#[tokio::test]
async fn many_calls_to_big_graph_4() {
    if check_skip() {
        return;
    }
    run(&REGISTRATION, || run_big_graph_test(vec![3, 3, 3, 3, 1000]))
        .await
        .unwrap();
}

#[tokio::test]
async fn many_calls_to_big_graph_5() {
    if check_skip() {
        return;
    }
    run(&REGISTRATION, || {
        run_big_graph_test(vec![10, 10, 10, 10, 10])
    })
    .await
    .unwrap();
}

#[tokio::test]
async fn many_calls_to_big_graph_6() {
    if check_skip() {
        return;
    }
    run(&REGISTRATION, || {
        run_big_graph_test(vec![2, 2, 2, 1000, 2, 2, 2])
    })
    .await
    .unwrap();
}

#[tokio::test]
async fn many_calls_to_big_graph_7() {
    if check_skip() {
        return;
    }
    run(&REGISTRATION, || {
        run_big_graph_test(vec![
            1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 3, 2, 1, 1, 1, 1, 5, 1, 1, 1, 200, 2, 1,
            1, 1, 1, 1, 1, 1, 1, 1,
        ])
    })
    .await
    .unwrap();
}

#[tokio::test]
async fn many_calls_to_big_graph_8() {
    if check_skip() {
        return;
    }
    run(&REGISTRATION, || {
        run_big_graph_test(vec![200, 2, 2, 2, 2, 200])
    })
    .await
    .unwrap();
}

#[tokio::test]
async fn many_calls_to_big_graph_9() {
    if check_skip() {
        return;
    }
    run(&REGISTRATION, || {
        run_big_graph_test(vec![10000, 1, 1, 2, 1, 1, 2, 2, 1, 1, 1, 1])
    })
    .await
    .unwrap();
}

#[turbo_tasks::value]
struct Value {
    value: u32,
}

#[turbo_tasks::function]
fn calls_many_children(i: TransientInstance<()>, j: Option<TransientInstance<()>>) -> Vc<()> {
    let _ = i;
    let _ = many_children(j);
    Vc::cell(())
}

#[turbo_tasks::function]
fn many_children(_j: Option<TransientInstance<()>>) -> Vc<()> {
    for i in 0..COUNT2 {
        let _ = many_children_inner(i);
    }
    Vc::cell(())
}

#[turbo_tasks::function]
fn many_children_inner(_i: u32) -> Vc<()> {
    Vc::cell(())
}

#[turbo_tasks::function]
fn calls_big_graph(mut counts: Vec<u32>, i: TransientInstance<()>) -> Vc<()> {
    let _ = i;
    counts.reverse();
    let _ = big_graph(counts, vec![]);
    Vc::cell(())
}

#[turbo_tasks::function]
fn big_graph(mut counts: Vec<u32>, keys: Vec<u32>) -> Vc<()> {
    let Some(count) = counts.pop() else {
        return Vc::cell(());
    };
    for i in 0..count {
        let new_keys = keys.iter().copied().chain(std::iter::once(i)).collect();
        let _ = big_graph(counts.clone(), new_keys);
    }
    Vc::cell(())
}