Commit d6d16414 authored by Benjamin Woodruff's avatar Benjamin Woodruff

Update next.js callsite for benchmarking

parent 00565bcc
......@@ -5,7 +5,7 @@ use std::{
fs::OpenOptions,
io::{self, BufRead, Write},
path::PathBuf,
sync::{Arc, Mutex},
sync::{Arc, LazyLock, Mutex},
time::Instant,
};
......@@ -14,22 +14,16 @@ use either::Either;
use napi::{JsFunction, threadsafe_function::ThreadsafeFunction};
use once_cell::sync::Lazy;
use owo_colors::OwoColorize;
use serde::Serialize;
use terminal_hyperlink::Hyperlink;
use turbo_tasks::{
TurboTasks, TurboTasksApi,
backend::TurboTasksExecutionError,
message_queue::{CompilationEvent, Severity},
};
use turbo_tasks::{StaticOrArc, TurboTasks, backend::TurboTasksExecutionError};
use turbo_tasks_backend::{
BackendOptions, DefaultBackingStorage, GitVersionInfo, NoopBackingStorage, StartupCacheState,
TurboTasksBackend, db_invalidation::invalidation_reasons, default_backing_storage,
BackendOptions, DefaultBackingStorage, NoopBackingStorage, TurboTasksBackend,
noop_backing_storage,
};
use turbopack_core::error::PrettyPrintError;
pub type NextTurboTasks =
Arc<TurboTasks<TurboTasksBackend<Either<DefaultBackingStorage, NoopBackingStorage>>>>;
StaticOrArc<TurboTasks<TurboTasksBackend<Either<DefaultBackingStorage, NoopBackingStorage>>>>;
/// A value often wrapped in [`napi::bindgen_prelude::External`] that retains the [TurboTasks]
/// instance used by Next.js, and [various napi helpers that are passed to us from
......@@ -180,84 +174,30 @@ impl NapiNextTurbopackCallbacks {
}
pub fn create_turbo_tasks(
output_path: PathBuf,
persistent_caching: bool,
_output_path: PathBuf,
_persistent_caching: bool,
_memory_limit: usize,
dependency_tracking: bool,
is_ci: bool,
is_short_session: bool,
_is_ci: bool,
_is_short_session: bool,
) -> Result<NextTurboTasks> {
Ok(if persistent_caching {
let version_info = GitVersionInfo {
describe: env!("VERGEN_GIT_DESCRIBE"),
dirty: option_env!("CI").is_none_or(|value| value.is_empty())
&& env!("VERGEN_GIT_DIRTY") == "true",
};
let (backing_storage, cache_state) = default_backing_storage(
&output_path.join("cache/turbopack"),
&version_info,
is_ci,
is_short_session,
)?;
let tt = TurboTasks::new(TurboTasksBackend::new(
BackendOptions {
storage_mode: Some(if std::env::var("TURBO_ENGINE_READ_ONLY").is_ok() {
turbo_tasks_backend::StorageMode::ReadOnly
} else {
turbo_tasks_backend::StorageMode::ReadWrite
}),
dependency_tracking,
num_workers: Some(tokio::runtime::Handle::current().metrics().num_workers()),
..Default::default()
},
Either::Left(backing_storage),
));
if let StartupCacheState::Invalidated { reason_code } = cache_state {
tt.send_compilation_event(Arc::new(StartupCacheInvalidationEvent { reason_code }));
}
tt
static TURBO_TASKS_STATIC: LazyLock<bool> =
LazyLock::new(|| std::env::var("NEXT_TURBO_TASKS_STATIC").unwrap() == "1");
let backend = TurboTasksBackend::new(
BackendOptions {
storage_mode: None,
dependency_tracking,
..Default::default()
},
Either::Right(noop_backing_storage()),
);
if *TURBO_TASKS_STATIC {
Ok(StaticOrArc::Static(TurboTasks::new_static(backend)))
} else {
TurboTasks::new(TurboTasksBackend::new(
BackendOptions {
storage_mode: None,
dependency_tracking,
..Default::default()
},
Either::Right(noop_backing_storage()),
))
})
}
#[derive(Serialize)]
struct StartupCacheInvalidationEvent {
reason_code: Option<String>,
}
impl CompilationEvent for StartupCacheInvalidationEvent {
fn type_name(&self) -> &'static str {
"StartupCacheInvalidationEvent"
}
fn severity(&self) -> Severity {
Severity::Warning
}
fn message(&self) -> String {
let reason_msg = match self.reason_code.as_deref() {
Some(invalidation_reasons::PANIC) => {
" because we previously detected an internal error in Turbopack"
}
Some(invalidation_reasons::USER_REQUEST) => " as the result of a user request",
_ => "", // ignore unknown reasons
};
format!(
"Turbopack's filesystem cache has been deleted{reason_msg}. Builds or page loads may \
be slower as a result."
)
}
fn to_json(&self) -> String {
serde_json::to_string(self).unwrap()
let tt = TurboTasks::new(backend);
// leak it for apples-to-apples benchmark comparision
std::mem::forget(tt.clone());
Ok(StaticOrArc::Shared(tt))
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment