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
Prev Previous commit
Next Next commit
Improve next-build-test
  • Loading branch information
sokra committed Jun 15, 2024
commit 71fb32d529ec05b866e605b36670d0b23ede0bbb
1 change: 0 additions & 1 deletion packages/next-swc/crates/next-build-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ next-api = { workspace = true }
serde_json = { workspace = true }
anyhow = { workspace = true }
tokio = { workspace = true, features = ["full"] }
turbo-tasks-malloc = { workspace = true, default-features = false }
turbopack-binding = { workspace = true, features = [
"__turbo_tasks",
"__turbo_tasks_memory",
Expand Down
81 changes: 41 additions & 40 deletions packages/next-swc/crates/next-build-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
#![feature(min_specialization)]
#![feature(arbitrary_self_types)]

use std::str::FromStr;
use std::{str::FromStr, time::Instant};

use anyhow::{Context, Result};
use futures_util::{StreamExt, TryStreamExt};
use next_api::{
project::{ProjectContainer, ProjectOptions},
route::{Endpoint, Route},
};
use turbo_tasks::{RcStr, TransientInstance, TurboTasks, Vc};
use turbopack_binding::turbo::tasks_memory::MemoryBackend;

pub async fn main_inner(
tt: &TurboTasks<MemoryBackend>,
Expand All @@ -20,14 +22,11 @@ pub async fn main_inner(
) -> Result<()> {
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())
})?;
let path = std::env::current_dir()?.join("project_options.json");
let mut file = std::fs::File::open(&path)
.with_context(|| format!("loading file at {}", path.display()))?;

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

if matches!(strat, Strategy::Development) {
options.dev = true;
Expand Down Expand Up @@ -59,13 +58,13 @@ pub async fn main_inner(
.routes
.clone()
.into_iter()
.filter(move |(name, _)| files.iter().any(|f| f == name)),
.filter(move |(name, _)| files.iter().any(|f| f.as_str() == name.as_str())),
) as Box<dyn Iterator<Item = _> + Send + Sync>
} else {
Box::new(shuffle(entrypoints.routes.clone().into_iter()))
};

let count = render_routes(tt, 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) {
Expand Down Expand Up @@ -123,11 +122,11 @@ 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)>,
routes: impl Iterator<Item = (RcStr, Route)>,
strategy: Strategy,
factor: usize,
limit: usize,
) -> usize {
) -> Result<usize> {
tracing::info!(
"rendering routes with {} parallel and strat {}",
factor,
Expand All @@ -139,32 +138,35 @@ pub async fn render_routes(
tracing::info!("{name}...");
let start = Instant::now();

tt.run_once(async move {
Ok(match route {
Route::Page {
html_endpoint,
data_endpoint: _,
} => {
html_endpoint.write_to_disk().await?;
}
Route::PageApi { endpoint } => {
endpoint.write_to_disk().await?;
}
Route::AppPage(routes) => {
for route in routes {
route.html_endpoint.write_to_disk().await?;
tt.run_once({
let name = name.clone();
async move {
Ok(match route {
Route::Page {
html_endpoint,
data_endpoint: _,
} => {
html_endpoint.write_to_disk().await?;
}
}
Route::AppRoute {
original_name: _,
endpoint,
} => {
endpoint.write_to_disk().await?;
}
Route::Conflict => {
tracing::info!("WARN: conflict {}", name);
}
})
Route::PageApi { endpoint } => {
endpoint.write_to_disk().await?;
}
Route::AppPage(routes) => {
for route in routes {
route.html_endpoint.write_to_disk().await?;
}
}
Route::AppRoute {
original_name: _,
endpoint,
} => {
endpoint.write_to_disk().await?;
}
Route::Conflict => {
tracing::info!("WARN: conflict {}", name);
}
})
}
})
.await?;

Expand All @@ -175,10 +177,9 @@ pub async fn render_routes(
.take(limit)
.buffer_unordered(factor)
.try_collect::<Vec<_>>()
.await
.unwrap();
.await?;

stream.len()
Ok(stream.len())
}

async fn hmr(tt: &TurboTasks<MemoryBackend>, project: Vc<ProjectContainer>) -> Result<()> {
Expand Down
21 changes: 11 additions & 10 deletions packages/next-swc/crates/next-build-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn main() {
.nth(5)
.map(|f| f.split(',').map(ToOwned::to_owned).collect());

if strat == Strategy::Sequential {
if matches!(strat, Strategy::Sequential | Strategy::Development) {
factor = 1;
}

Expand All @@ -70,6 +70,7 @@ fn main() {
.build()
.unwrap()
.block_on(async {
tracing::info!("start");
let tt = TurboTasks::new(MemoryBackend::new(usize::MAX));
let result = main_inner(&tt, strat, factor, limit, files).await;
let memory = TurboMalloc::memory_usage();
Expand All @@ -88,24 +89,24 @@ fn main() {
let canonical_path = std::fs::canonicalize(absolute_dir).unwrap();

let options = ProjectOptions {
build_id: "test".to_owned(),
build_id: "test".into(),
define_env: DefineEnv {
client: vec![],
edge: vec![],
nodejs: vec![],
},
dev: true,
encryption_key: "deadbeef".to_string(),
encryption_key: "deadbeef".into(),
env: vec![],
js_config: include_str!("../jsConfig.json").to_string(),
next_config: include_str!("../nextConfig.json").to_string(),
js_config: include_str!("../jsConfig.json").into(),
next_config: include_str!("../nextConfig.json").into(),
preview_props: next_api::project::DraftModeOptions {
preview_mode_encryption_key: "deadbeef".to_string(),
preview_mode_id: "test".to_string(),
preview_mode_signing_key: "deadbeef".to_string(),
preview_mode_encryption_key: "deadbeef".into(),
preview_mode_id: "test".into(),
preview_mode_signing_key: "deadbeef".into(),
},
project_path: canonical_path.to_string_lossy().to_string(),
root_path: "/".to_string(),
project_path: canonical_path.to_string_lossy().into(),
root_path: "/".into(),
watch: false,
};

Expand Down