-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathcache_remote_skip_browser.rs
More file actions
67 lines (53 loc) · 2.45 KB
/
cache_remote_skip_browser.rs
File metadata and controls
67 lines (53 loc) · 2.45 KB
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
//! End-to-end remote cache demo (index_cache_server / hybrid_cache_server).
//!
//! 1) Start your remote cache server (default expected at http://127.0.0.1:8080)
//! 2) Set `HYBRID_CACHE_ENDPOINT` if different.
//! 3) Run:
//! cargo run --example cache_remote_skip_browser --features="spider/sync spider/chrome spider/chrome_remote_cache" -- https://example.com/
extern crate spider;
use spider::features::chrome_common::RequestInterceptConfiguration;
use spider::tokio;
use spider::website::Website;
async fn crawl_once(url: &str, skip_browser: bool) -> (std::time::Duration, Option<f64>, usize) {
let mut website: Website = Website::new(url)
.with_limit(1)
.with_caching(true)
.with_cache_skip_browser(skip_browser)
.with_chrome_intercept(RequestInterceptConfiguration::new(true))
.build()
.expect("build website");
let mut rx = website.subscribe(4);
let page_task = tokio::spawn(async move { rx.recv().await.ok() });
let started = tokio::time::Instant::now();
website.crawl_raw().await;
let elapsed = started.elapsed();
website.unsubscribe();
let page = page_task.await.ok().and_then(|p| p);
let bytes_transferred = page.as_ref().and_then(|p| p.bytes_transferred);
let html_len = page.map_or(0, |p| p.get_html_bytes_u8().len());
(elapsed, bytes_transferred, html_len)
}
#[tokio::main]
async fn main() {
env_logger::init();
let target = std::env::args()
.nth(1)
.unwrap_or_else(|| "https://example.com/".to_string());
let endpoint = std::env::var("HYBRID_CACHE_ENDPOINT")
.unwrap_or_else(|_| "http://127.0.0.1:8080".to_string());
println!("target={target}");
println!("hybrid_cache_endpoint={endpoint}");
// Pass 1: warm cache (browser can run as usual)
let (warm_duration, warm_bytes, warm_html_len) = crawl_once(&target, false).await;
println!(
"warm_pass: duration={warm_duration:?}, bytes_transferred={warm_bytes:?}, html_len={warm_html_len}"
);
// Pass 2: enable skip-browser mode, return cached value immediately if available
let (cached_duration, cached_bytes, cached_html_len) = crawl_once(&target, true).await;
println!(
"cached_pass(skip_browser): duration={cached_duration:?}, bytes_transferred={cached_bytes:?}, html_len={cached_html_len}"
);
println!(
"cache_hit_hint: bytes_transferred is commonly None on direct cached returns, and duration usually drops significantly."
);
}