Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Turbopack] improve memory measurement suite #66748

Merged
merged 7 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
improve next-build-test
  • Loading branch information
sokra committed Jun 15, 2024
commit 7daf12e32de32dc33d9a9498f4675392b83626d7
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"packages/next-swc/crates/napi",
"packages/next-swc/crates/wasm",
"packages/next-swc/crates/next-api",
"packages/next-swc/crates/next-build-test",
"packages/next-swc/crates/next-build",
"packages/next-swc/crates/next-core",
"packages/next-swc/crates/next-custom-transforms",
Expand Down
13 changes: 12 additions & 1 deletion packages/next-swc/crates/next-build-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ license = "MPL-2.0"
edition = "2021"
autobenches = false

# [[bin]]
# name = "next-build-test"
# path = "src/main.rs"
# bench = false

[lints]
workspace = true

[dependencies]
next-core = { workspace = true, features = ["rustls-tls"] }
next-api = { workspace = true }
serde_json = { workspace = true }
anyhow = { workspace = true }
Expand Down Expand Up @@ -39,6 +43,13 @@ tracing = "0.1"
tracing-subscriber = "0.3"
futures-util = "0.3.30"

# Enable specific tls features per-target.
[target.'cfg(all(target_os = "windows", target_arch = "aarch64"))'.dependencies]
next-core = { workspace = true, features = ["native-tls"] }

[target.'cfg(not(any(all(target_os = "windows", target_arch = "aarch64"), target_arch="wasm32")))'.dependencies]
next-core = { workspace = true, features = ["rustls-tls"] }

[build-dependencies]
turbopack-binding = { workspace = true, features = ["__turbo_tasks_build"] }

Expand Down
85 changes: 69 additions & 16 deletions packages/next-swc/crates/next-build-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use next_api::{
};

pub async fn main_inner(
tt: &TurboTasks<MemoryBackend>,
strat: Strategy,
factor: usize,
limit: usize,
Expand All @@ -27,10 +28,23 @@ pub async fn main_inner(
})?;

let options: ProjectOptions = serde_json::from_reader(&mut file).unwrap();
let project = ProjectContainer::new(options);

if matches!(strat, Strategy::Development) {
options.dev = true;
options.watch = true;
} else {
options.dev = false;
options.watch = false;
}

let project = tt
.run_once(async { Ok(ProjectContainer::new(options)) })
.await?;

tracing::info!("collecting endpoints");
let entrypoints = project.entrypoints().await?;
let entrypoints = tt
.run_once(async move { Ok(project.entrypoints().await?) })
.await?;

let routes = if let Some(files) = files {
tracing::info!("builing only the files:");
Expand All @@ -51,9 +65,13 @@ pub async fn main_inner(
Box::new(shuffle(entrypoints.routes.clone().into_iter()))
};

let count = render_routes(routes, strat, factor, limit).await;
let count = render_routes(tt, routes, strat, factor, limit).await;
tracing::info!("rendered {} pages", count);

if matches!(strat, Strategy::Development) {
hmr(tt, project).await?;
}

Ok(())
}

Expand All @@ -67,6 +85,7 @@ pub enum Strategy {
Sequential,
Concurrent,
Parallel,
Development,
}

impl std::fmt::Display for Strategy {
Expand All @@ -75,6 +94,7 @@ impl std::fmt::Display for Strategy {
Strategy::Sequential => write!(f, "sequential"),
Strategy::Concurrent => write!(f, "concurrent"),
Strategy::Parallel => write!(f, "parallel"),
Strategy::Development => write!(f, "development"),
}
}
}
Expand All @@ -87,6 +107,7 @@ impl FromStr for Strategy {
"sequential" => Ok(Strategy::Sequential),
"concurrent" => Ok(Strategy::Concurrent),
"parallel" => Ok(Strategy::Parallel),
"development" => Ok(Strategy::Development),
_ => Err(anyhow::anyhow!("invalid strategy")),
}
}
Expand All @@ -101,6 +122,7 @@ pub fn shuffle<'a, T: 'a>(items: impl Iterator<Item = T>) -> impl Iterator<Item
}

pub async fn render_routes(
tt: &TurboTasks<MemoryBackend>,
routes: impl Iterator<Item = (String, Route)>,
strategy: Strategy,
factor: usize,
Expand All @@ -113,11 +135,12 @@ pub async fn render_routes(
);

let stream = tokio_stream::iter(routes)
.map(move |(name, route)| {
let fut = async move {
tracing::info!("{name}");
.map(move |(name, route)| async move {
tracing::info!("{name}...");
let start = Instant::now();

match route {
tt.run_once(async move {
Ok(match route {
Route::Page {
html_endpoint,
data_endpoint: _,
Expand All @@ -141,17 +164,13 @@ pub async fn render_routes(
Route::Conflict => {
tracing::info!("WARN: conflict {}", name);
}
}
})
})
.await?;

Ok::<_, anyhow::Error>(())
};
tracing::info!("{name} {:?}", start.elapsed());

async move {
match strategy {
Strategy::Parallel => tokio::task::spawn(fut).await.unwrap(),
_ => fut.await,
}
}
Ok::<_, anyhow::Error>(())
})
.take(limit)
.buffer_unordered(factor)
Expand All @@ -161,3 +180,37 @@ pub async fn render_routes(

stream.len()
}

async fn hmr(tt: &TurboTasks<MemoryBackend>, project: Vc<ProjectContainer>) -> Result<()> {
tracing::info!("HMR...");
let session = TransientInstance::new(());
let idents = tt
.run_once(async move { Ok(project.hmr_identifiers().await?) })
.await?;
let start = Instant::now();
for ident in idents {
let session = session.clone();
let start = Instant::now();
let task = tt.spawn_root_task(move || {
let session = session.clone();
async move {
let project = project.project();
project
.hmr_update(
ident.clone(),
project.hmr_version_state(ident.clone(), session),
)
.await?;
Ok(Vc::<()>::cell(()))
}
});
tt.wait_task_completion(task, true).await?;
let e = start.elapsed();
if e.as_millis() > 10 {
tracing::info!("HMR: {:?} {:?}", ident, e);
}
}
tracing::info!("HMR {:?}", start.elapsed());

Ok(())
}
15 changes: 11 additions & 4 deletions packages/next-swc/crates/next-build-test/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::{convert::Infallible, str::FromStr};
use std::{convert::Infallible, str::FromStr, time::Instant};

use next_api::project::{DefineEnv, ProjectOptions};
use next_build_test::{main_inner, Strategy};
use turbo_tasks::TurboTasks;
use turbo_tasks_malloc::TurboMalloc;
use turbopack_binding::turbo::tasks_memory::MemoryBackend;

#[global_allocator]
static ALLOC: TurboMalloc = TurboMalloc;

enum Cmd {
Run,
Generate,
Expand Down Expand Up @@ -68,9 +71,13 @@ fn main() {
.unwrap()
.block_on(async {
let tt = TurboTasks::new(MemoryBackend::new(usize::MAX));
let x = tt.run_once(main_inner(strat, factor, limit, files)).await;
tracing::debug!("done");
x
let result = main_inner(&tt, strat, factor, limit, files).await;
let memory = TurboMalloc::memory_usage();
tracing::info!("memory usage: {} MiB", memory / 1024 / 1024);
let start = Instant::now();
drop(tt);
tracing::info!("drop {:?}", start.elapsed());
result
})
.unwrap();
}
Expand Down