From bf1cf2302aa1401562b412687602d364c96e25ed Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 10 Oct 2025 14:20:42 +0000 Subject: [PATCH] fix(harness): add harness debug feature (#1012) --- crates/harness/build.sh | 14 +++- crates/harness/executor/Cargo.toml | 9 ++- crates/harness/runner/Cargo.toml | 4 + crates/harness/runner/src/debug_prelude.rs | 17 +++++ crates/harness/runner/src/executor.rs | 86 +++++++++++++++++----- crates/harness/runner/src/lib.rs | 12 +++ 6 files changed, 122 insertions(+), 20 deletions(-) create mode 100644 crates/harness/runner/src/debug_prelude.rs diff --git a/crates/harness/build.sh b/crates/harness/build.sh index 7cbd48c6c..96e1cab48 100755 --- a/crates/harness/build.sh +++ b/crates/harness/build.sh @@ -3,7 +3,19 @@ # Ensure the script runs in the folder that contains this script cd "$(dirname "$0")" -cargo build --release --package tlsn-harness-runner --package tlsn-harness-executor --package tlsn-server-fixture --package tlsn-harness-plot +RUNNER_FEATURES="" +EXECUTOR_FEATURES="" + +if [ "$1" = "debug" ]; then + RUNNER_FEATURES="--features debug" + EXECUTOR_FEATURES="--no-default-features --features debug" +fi + +cargo build --release \ + --package tlsn-harness-runner $RUNNER_FEATURES \ + --package tlsn-harness-executor $EXECUTOR_FEATURES \ + --package tlsn-server-fixture \ + --package tlsn-harness-plot mkdir -p bin diff --git a/crates/harness/executor/Cargo.toml b/crates/harness/executor/Cargo.toml index 614c4a620..18b91a6ff 100644 --- a/crates/harness/executor/Cargo.toml +++ b/crates/harness/executor/Cargo.toml @@ -4,6 +4,12 @@ version = "0.1.0" edition = "2024" publish = false +[features] +# Disable tracing events as a workaround for issue 959. +default = ["tracing/release_max_level_off"] +# Used to debug the executor itself. +debug = [] + [lib] name = "harness_executor" crate-type = ["cdylib", "rlib"] @@ -28,8 +34,7 @@ tokio = { workspace = true, features = ["full"] } tokio-util = { workspace = true, features = ["compat"] } [target.'cfg(target_arch = "wasm32")'.dependencies] -# Disable tracing events as a workaround for issue 959. -tracing = { workspace = true, features = ["release_max_level_off"] } +tracing = { workspace = true } wasm-bindgen = { workspace = true } tlsn-wasm = { workspace = true } js-sys = { workspace = true } diff --git a/crates/harness/runner/Cargo.toml b/crates/harness/runner/Cargo.toml index 2dbd6b42a..508ce331b 100644 --- a/crates/harness/runner/Cargo.toml +++ b/crates/harness/runner/Cargo.toml @@ -7,6 +7,10 @@ publish = false [lib] name = "harness_runner" +[features] +# Used to debug the runner itself. +debug = [] + [dependencies] tlsn-harness-core = { workspace = true } tlsn-server-fixture = { workspace = true } diff --git a/crates/harness/runner/src/debug_prelude.rs b/crates/harness/runner/src/debug_prelude.rs new file mode 100644 index 000000000..d474fde77 --- /dev/null +++ b/crates/harness/runner/src/debug_prelude.rs @@ -0,0 +1,17 @@ +#![allow(unused_imports)] +pub use futures::FutureExt; + +pub use tracing::{debug, error}; + +pub use chromiumoxide::{ + Browser, Page, + cdp::{ + browser_protocol::{ + log::{EventEntryAdded, LogEntryLevel}, + network::{EnableParams, SetCacheDisabledParams}, + page::ReloadParams, + }, + js_protocol::runtime::EventExceptionThrown, + }, + handler::HandlerConfig, +}; diff --git a/crates/harness/runner/src/executor.rs b/crates/harness/runner/src/executor.rs index 826b0e1d6..87820ef61 100644 --- a/crates/harness/runner/src/executor.rs +++ b/crates/harness/runner/src/executor.rs @@ -21,6 +21,9 @@ use harness_core::{ use crate::{Target, network::Namespace, rpc::Rpc}; +#[cfg(feature = "debug")] +use crate::debug_prelude::*; + pub struct Executor { ns: Namespace, config: ExecutorConfig, @@ -66,20 +69,34 @@ impl Executor { Id::One => self.config.network().rpc_1, }; - let process = duct::cmd!( - "sudo", - "ip", - "netns", - "exec", - self.ns.name(), - "env", + let mut args = vec![ + "ip".into(), + "netns".into(), + "exec".into(), + self.ns.name().into(), + "env".into(), format!("CONFIG={}", serde_json::to_string(&self.config)?), - executor_path - ) - .stdout_capture() - .stderr_capture() - .unchecked() - .start()?; + ]; + + if cfg!(feature = "debug") { + let level = &std::env::var("RUST_LOG").unwrap_or("debug".to_string()); + args.push("env".into()); + args.push(format!("RUST_LOG={}", level)); + }; + + args.push(executor_path.to_str().expect("valid path").into()); + + let process = duct::cmd("sudo", args); + + let process = if !cfg!(feature = "debug") { + process + .stdout_capture() + .stderr_capture() + .unchecked() + .start()? + } else { + process.unchecked().start()? + }; let rpc = Rpc::new_native(rpc_addr).await?; @@ -119,10 +136,13 @@ impl Executor { "--no-sandbox", format!("--user-data-dir={tmp}"), format!("--allowed-ips=10.250.0.1"), - ) - .stderr_capture() - .stdout_capture() - .start()?; + ); + + let process = if !cfg!(feature = "debug") { + process.stderr_capture().stdout_capture().start()? + } else { + process.start()? + }; const TIMEOUT: usize = 10000; const DELAY: usize = 100; @@ -171,6 +191,38 @@ impl Executor { .new_page(&format!("http://{wasm_addr}:{wasm_port}/index.html")) .await?; + #[cfg(feature = "debug")] + tokio::spawn(register_listeners(page.clone()).await?); + + #[cfg(feature = "debug")] + async fn register_listeners(page: Page) -> Result> { + let mut logs = page.event_listener::().await?.fuse(); + let mut exceptions = + page.event_listener::().await?.fuse(); + + Ok(futures::future::join( + async move { + while let Some(event) = logs.next().await { + let entry = &event.entry; + match entry.level { + LogEntryLevel::Error => { + error!("{:?}", entry); + } + _ => { + debug!("{:?}: {}", entry.timestamp, entry.text); + } + } + } + }, + async move { + while let Some(event) = exceptions.next().await { + error!("{:?}", event); + } + }, + ) + .map(|_| ())) + } + page.execute(EnableParams::builder().build()).await?; page.execute(SetCacheDisabledParams { cache_disabled: true, diff --git a/crates/harness/runner/src/lib.rs b/crates/harness/runner/src/lib.rs index 10358c70d..88613c802 100644 --- a/crates/harness/runner/src/lib.rs +++ b/crates/harness/runner/src/lib.rs @@ -6,6 +6,9 @@ mod server_fixture; pub mod wasm_server; mod ws_proxy; +#[cfg(feature = "debug")] +mod debug_prelude; + use std::time::Duration; use anyhow::Result; @@ -24,6 +27,9 @@ use cli::{Cli, Command}; use executor::Executor; use server_fixture::ServerFixture; +#[cfg(feature = "debug")] +use crate::debug_prelude::*; + use crate::{cli::Route, network::Network, wasm_server::WasmServer, ws_proxy::WsProxy}; #[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)] @@ -113,6 +119,9 @@ impl Runner { } pub async fn main() -> Result<()> { + #[cfg(feature = "debug")] + tracing_subscriber::fmt::init(); + let cli = Cli::parse(); let mut runner = Runner::new(&cli)?; @@ -227,6 +236,9 @@ pub async fn main() -> Result<()> { // Wait for the network to stabilize tokio::time::sleep(Duration::from_millis(100)).await; + #[cfg(feature = "debug")] + debug!("Starting bench in group {:?}", config.group); + let (output, _) = tokio::try_join!( runner.exec_p.bench(BenchCmd { config: config.clone(),