use chromiumoxide::{
browser::Browser,
cache::{BasicCachePolicy, CacheStrategy},
handler::HandlerConfig,
init_default_cache_worker,
};
use futures_util::StreamExt;
use std::time::{Duration, Instant};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let cache_strat = CacheStrategy::Scraping;
let (browser, mut handler) = Browser::connect_with_config(
"http://localhost:9223",
HandlerConfig {
request_intercept: true,
cache_enabled: false,
..Default::default()
},
)
.await?;
init_default_cache_worker().await;
let handle = tokio::task::spawn(async move {
loop {
if let Some(Err(e)) = handler.next().await {
eprintln!("{:?}", e);
}
}
});
let page = browser.new_page("about:blank").await?;
let test_url = "https://spider.cloud/about";
page.spawn_cache_listener(test_url, None, Some(cache_strat), Some("true".into()), None)
.await?;
let start_first = Instant::now();
println!("Attempting first navigation");
page.goto(test_url).await?;
let html = page.wait_for_navigation().await?.content().await?;
let dur_first = start_first.elapsed();
println!(
"First (cold) navigation to {} took: {:?}",
test_url, dur_first
);
tokio::time::sleep(Duration::from_secs(5)).await;
let cache_policy = BasicCachePolicy::AllowStale;
let start_second = Instant::now();
println!("Attempting second navigation");
let html2 = page
.goto_with_cache_remote_intercept_enabled(
test_url,
None,
Some(cache_policy.clone()),
Some(cache_strat),
None,
None,
)
.await?
.wait_for_navigation()
.await?
.content()
.await?;
let dur_second = start_second.elapsed();
println!(
"Second (warm) navigation to {} took: {:?}",
test_url, dur_second
);
if dur_second.as_millis() > 0 {
let speedup = dur_first.as_secs_f64() / dur_second.as_secs_f64();
println!(
"Warm run speedup: {:.2}x (first: {:?}, second: {:?})",
speedup, dur_first, dur_second
);
} else {
println!(
"Second run duration was extremely small (<= 1ms); \
treat as effectively instant. First: {:?}",
dur_first
);
}
println!("Main size: {:?}", html.len());
println!("Cached size: {:?}", html2.len());
handle.await?;
Ok(())
}