File size: 3,197 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
#![cfg_attr(not(codspeed), allow(unused))]

#[global_allocator]
static ALLOC: turbo_tasks_malloc::TurboMalloc = turbo_tasks_malloc::TurboMalloc;

use std::{
    path::{Path, PathBuf},
    process::Command,
};

use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use turbopack_cli::{
    arguments::{BuildArguments, CommonArguments},
    register,
};

fn list_apps() -> (PathBuf, Vec<PathBuf>) {
    // We need to rely on `CARGO_MANIFEST_DIR` because we are running it via `cargo codspeed`

    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let apps_dir = Path::new(&manifest_dir)
        .join("../../benchmark-apps")
        .canonicalize()
        .expect("failed to canonicalize path");

    let mut apps = Vec::new();

    for entry in std::fs::read_dir(&apps_dir).unwrap() {
        let entry = entry.unwrap();
        let path = entry.path();
        if path.is_dir() {
            // Exclude node_modules
            if path.file_name().unwrap_or_default() == "node_modules" {
                continue;
            }

            apps.push(path);
        }
    }

    (apps_dir, apps)
}

fn bench_small_apps(c: &mut Criterion) {
    use turbo_tasks_malloc::TurboMalloc;

    register();

    let (apps_dir, apps) = list_apps();
    let mut g = c.benchmark_group("turbopack/build/apps");

    for app in apps {
        g.bench_function(
            BenchmarkId::new("build", app.file_name().unwrap().to_string_lossy()),
            |b| {
                let apps_dir = apps_dir.clone();
                let app = app.clone();

                b.iter(move || {
                    let mut rt = tokio::runtime::Builder::new_multi_thread();
                    rt.enable_all().on_thread_stop(|| {
                        TurboMalloc::thread_stop();
                    });
                    let rt = rt.build().unwrap();

                    let apps_dir = apps_dir.clone();
                    let app = app.clone();

                    let app_name = app.file_name().unwrap().to_string_lossy().to_string();

                    rt.block_on(async move {
                        turbopack_cli::build::build(&BuildArguments {
                            common: CommonArguments {
                                entries: Some(vec![format!("{app_name}/index.tsx")]),
                                dir: Some(app.clone()),
                                root: Some(apps_dir),
                                log_level: None,
                                show_all: false,
                                log_detail: false,
                                full_stats: false,
                                target: None,
                            },
                            no_sourcemap: false,
                            no_minify: false,
                            force_memory_cleanup: true,
                            no_scope_hoist: false,
                        })
                        .await
                    })
                    .unwrap();
                });
            },
        );
    }
}

criterion_group!(
  name = benches;
  config = Criterion::default().sample_size(10);
  targets = bench_small_apps
);
criterion_main!(benches);