File size: 10,129 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 |
#![feature(future_join)]
#![feature(min_specialization)]
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
use std::{str::FromStr, time::Instant};
use anyhow::{Context, Result};
use futures_util::{StreamExt, TryStreamExt};
use next_api::{
project::{ProjectContainer, ProjectOptions},
route::{Endpoint, EndpointOutputPaths, Route, endpoint_write_to_disk},
};
use turbo_rcstr::RcStr;
use turbo_tasks::{ReadConsistency, ResolvedVc, TransientInstance, TurboTasks, Vc, get_effects};
use turbo_tasks_backend::{NoopBackingStorage, TurboTasksBackend};
use turbo_tasks_malloc::TurboMalloc;
pub async fn main_inner(
tt: &TurboTasks<TurboTasksBackend<NoopBackingStorage>>,
strategy: Strategy,
factor: usize,
limit: usize,
files: Option<Vec<String>>,
) -> Result<()> {
register();
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 mut options: ProjectOptions = serde_json::from_reader(&mut file)?;
if matches!(strategy, Strategy::Development { .. }) {
options.dev = true;
options.watch.enable = true;
} else {
options.dev = false;
options.watch.enable = false;
}
let project = tt
.run_once(async {
let project = ProjectContainer::new("next-build-test".into(), options.dev);
let project = project.to_resolved().await?;
project.initialize(options).await?;
Ok(project)
})
.await?;
tracing::info!("collecting endpoints");
let entrypoints = tt
.run_once(async move { project.entrypoints().await })
.await?;
let mut routes = if let Some(files) = files {
tracing::info!("building only the files:");
for file in &files {
tracing::info!(" {}", file);
}
// filter out the files that are not in the list
// we expect this to be small so linear search OK
Box::new(files.into_iter().filter_map(|f| {
entrypoints
.routes
.iter()
.find(|(name, _)| f.as_str() == name.as_str())
.map(|(name, route)| (name.clone(), route.clone()))
})) as Box<dyn Iterator<Item = _> + Send + Sync>
} else {
Box::new(entrypoints.routes.clone().into_iter())
};
if strategy.randomized() {
routes = Box::new(shuffle(routes))
}
let start = Instant::now();
let count = render_routes(tt, routes, strategy, factor, limit).await?;
tracing::info!("rendered {} pages in {:?}", count, start.elapsed());
if count == 0 {
tracing::info!("No pages found, these pages exist:");
for (route, _) in entrypoints.routes.iter() {
tracing::info!(" {}", route);
}
}
if matches!(strategy, Strategy::Development { .. }) {
hmr(tt, *project).await?;
}
Ok(())
}
pub fn register() {
next_api::register();
include!(concat!(env!("OUT_DIR"), "/register.rs"));
}
#[derive(PartialEq, Copy, Clone)]
pub enum Strategy {
Sequential { randomized: bool },
Concurrent,
Parallel { randomized: bool },
Development { randomized: bool },
}
impl std::fmt::Display for Strategy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Strategy::Sequential { randomized: false } => write!(f, "sequential"),
Strategy::Sequential { randomized: true } => write!(f, "sequential-randomized"),
Strategy::Concurrent => write!(f, "concurrent"),
Strategy::Parallel { randomized: false } => write!(f, "parallel"),
Strategy::Parallel { randomized: true } => write!(f, "parallel-randomized"),
Strategy::Development { randomized: false } => write!(f, "development"),
Strategy::Development { randomized: true } => write!(f, "development-randomized"),
}
}
}
impl FromStr for Strategy {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
match s {
"sequential" => Ok(Strategy::Sequential { randomized: false }),
"sequential-randomized" => Ok(Strategy::Sequential { randomized: true }),
"concurrent" => Ok(Strategy::Concurrent),
"parallel" => Ok(Strategy::Parallel { randomized: false }),
"parallel-randomized" => Ok(Strategy::Parallel { randomized: true }),
"development" => Ok(Strategy::Development { randomized: false }),
"development-randomized" => Ok(Strategy::Development { randomized: true }),
_ => Err(anyhow::anyhow!("invalid strategy")),
}
}
}
impl Strategy {
pub fn randomized(&self) -> bool {
match self {
Strategy::Sequential { randomized } => *randomized,
Strategy::Concurrent => false,
Strategy::Parallel { randomized } => *randomized,
Strategy::Development { randomized } => *randomized,
}
}
}
pub fn shuffle<'a, T: 'a>(items: impl Iterator<Item = T>) -> impl Iterator<Item = T> {
use rand::{SeedableRng, seq::SliceRandom};
let mut rng = rand::rngs::SmallRng::from_seed([0; 32]);
let mut input = items.collect::<Vec<_>>();
input.shuffle(&mut rng);
input.into_iter()
}
pub async fn render_routes(
tt: &TurboTasks<TurboTasksBackend<NoopBackingStorage>>,
routes: impl Iterator<Item = (RcStr, Route)>,
strategy: Strategy,
factor: usize,
limit: usize,
) -> Result<usize> {
tracing::info!(
"rendering routes with {} parallel and strategy {}",
factor,
strategy
);
let stream = tokio_stream::iter(routes)
.map(move |(name, route)| async move {
tracing::info!("{name}...");
let start = Instant::now();
let memory = TurboMalloc::memory_usage();
tt.run_once({
let name = name.clone();
async move {
match route {
Route::Page {
html_endpoint,
data_endpoint: _,
} => {
endpoint_write_to_disk_with_effects(*html_endpoint).await?;
}
Route::PageApi { endpoint } => {
endpoint_write_to_disk_with_effects(*endpoint).await?;
}
Route::AppPage(routes) => {
for route in routes {
endpoint_write_to_disk_with_effects(*route.html_endpoint).await?;
}
}
Route::AppRoute {
original_name: _,
endpoint,
} => {
endpoint_write_to_disk_with_effects(*endpoint).await?;
}
Route::Conflict => {
tracing::info!("WARN: conflict {}", name);
}
}
Ok(())
}
})
.await?;
let duration = start.elapsed();
let memory_after = TurboMalloc::memory_usage();
if matches!(strategy, Strategy::Sequential { .. }) {
if memory_after > memory {
tracing::info!(
"{name} {:?} {} MiB (memory usage increased by {} MiB)",
duration,
memory_after / 1024 / 1024,
(memory_after - memory) / 1024 / 1024
);
} else {
tracing::info!(
"{name} {:?} {} MiB (memory usage decreased by {} MiB)",
duration,
memory_after / 1024 / 1024,
(memory - memory_after) / 1024 / 1024
);
}
} else {
tracing::info!("{name} {:?} {} MiB", duration, memory_after / 1024 / 1024);
}
Ok::<_, anyhow::Error>(())
})
.take(limit)
.buffer_unordered(factor)
.try_collect::<Vec<_>>()
.await?;
Ok(stream.len())
}
#[turbo_tasks::function]
async fn endpoint_write_to_disk_with_effects(
endpoint: ResolvedVc<Box<dyn Endpoint>>,
) -> Result<Vc<EndpointOutputPaths>> {
let op = endpoint_write_to_disk_operation(endpoint);
let result = op.resolve_strongly_consistent().await?;
get_effects(op).await?.apply().await?;
Ok(*result)
}
#[turbo_tasks::function(operation)]
pub fn endpoint_write_to_disk_operation(
endpoint: ResolvedVc<Box<dyn Endpoint>>,
) -> Vc<EndpointOutputPaths> {
endpoint_write_to_disk(*endpoint)
}
async fn hmr(
tt: &TurboTasks<TurboTasksBackend<NoopBackingStorage>>,
project: Vc<ProjectContainer>,
) -> Result<()> {
tracing::info!("HMR...");
let session = TransientInstance::new(());
let idents = tt
.run_once(async move { project.hmr_identifiers().await })
.await?;
let start = Instant::now();
for ident in idents {
if !ident.ends_with(".js") {
continue;
}
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();
let state = project.hmr_version_state(ident.clone(), session);
project.hmr_update(ident.clone(), state).await?;
Ok(Vc::<()>::cell(()))
}
});
tt.wait_task_completion(task, ReadConsistency::Strong)
.await?;
let e = start.elapsed();
if e.as_millis() > 10 {
tracing::info!("HMR: {:?} {:?}", ident, e);
}
}
tracing::info!("HMR {:?}", start.elapsed());
Ok(())
}
|