Skip to content

Commit

Permalink
add criterion benches
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed Apr 11, 2024
1 parent 56975cf commit 79d818c
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/next-swc/crates/next-build-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ turbopack-binding = { workspace = true, features = ["__turbo_tasks_build"] }

[dev-dependencies]
criterion = { version = "0.5", features = ["async_tokio"] }

[[bench]]
name = "bench"
harness = false
101 changes: 101 additions & 0 deletions packages/next-swc/crates/next-build-test/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use std::time::Duration;

use anyhow::Context;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use next_api::project::{ProjectContainer, ProjectOptions};
use turbo_tasks::TurboTasks;
use turbo_tasks_malloc::TurboMalloc;
use turbopack_binding::turbo::tasks_memory::MemoryBackend;

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

pub fn criterion_benchmark(c: &mut Criterion) {
next_build_test::register();

let mut file = std::fs::File::open("project_options.json")
.with_context(|| {
let path = std::env::current_dir()
.unwrap()
.join("project_options.json");
format!("loading file at {}", path.display())
})
.unwrap();

let data: ProjectOptions = serde_json::from_reader(&mut file).unwrap();

let options = ProjectOptions { ..data };

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

c.bench_function("entrypoints", |b| {
b.to_async(&rt).iter(|| {
let options = options.clone();
async move {
let tt = TurboTasks::new(MemoryBackend::new(usize::MAX));
_ = tt
.run_once(async move {
let project = ProjectContainer::new(options.to_owned());
_ = black_box(project.entrypoints().await);
Ok(())
})
.await;
}
});
});

// decide the pages to benchmark ahead of time
const SELECTED_PAGES: [&str; 10] = [
"/legal/event-code-of-conduct",
"/docs/functions/og-image-generation/og-image-api",
"/docs/image-optimization/managing-image-optimization-costs",
"/new/import/card",
"/try/share/[slug]",
"/app-future/[lang]/[teamSlug]/~/account/invoices-new",
"/docs/workflow-collaboration/conformance/rules/NEXTJS_MISSING_REACT_STRICT_MODE",
"/app-future/[lang]/[teamSlug]/~/ai/models/[modelName]/getting-started",
"/app-future/[lang]/[teamSlug]/~/usage/[plan]/[planIteration]",
"/docs/workflow-collaboration/conformance/rules/REQUIRE_CARET_DEPENDENCIES",
];

c.bench_function("10 pages", |b| {
b.to_async(&rt).iter(|| {
let options = options.clone();
async move {
let tt = TurboTasks::new(MemoryBackend::new(usize::MAX));
let result = tt
.run_once(async move {
let project = ProjectContainer::new(options.to_owned());
let entrypoints = project.entrypoints().await.unwrap();
let routes = entrypoints
.routes
.clone()
.into_iter()
.filter(|route| SELECTED_PAGES.contains(&route.0.as_str()));

next_build_test::render_routes(
routes,
next_build_test::Strategy::Concurrent,
10,
10,
)
.await;

Ok(())
})
.await;

black_box(result).unwrap();
}
});
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

0 comments on commit 79d818c

Please sign in to comment.