Skip to content

Commit 309873e

Browse files
committed
refactor: replace API_KEY with SERPAPI_KEY in examples and tests; improve memory statistics reporting in benchmarks
1 parent 83c7f7a commit 309873e

23 files changed

+78
-67
lines changed

benches/search_query.rs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,90 +43,104 @@ fn benchmark_search_query(c: &mut Criterion) {
4343
let rt = Runtime::new().unwrap();
4444
let client = Arc::new(setup_client());
4545
let params = setup_search_params();
46-
46+
4747
// Collect memory statistics separately using Arc<Mutex<>>
4848
let memory_deltas: Arc<Mutex<Vec<usize>>> = Arc::new(Mutex::new(Vec::new()));
4949
let peak_memory: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));
50-
50+
5151
let mut group = c.benchmark_group("search_query");
5252
group.sample_size(10); // Execute 10 times
5353
group.measurement_time(std::time::Duration::from_secs(120)); // Allow up to 120s for API calls
54-
54+
5555
let client_clone = Arc::clone(&client);
5656
let params_clone = params.clone();
5757
let memory_deltas_clone = Arc::clone(&memory_deltas);
5858
let peak_memory_clone = Arc::clone(&peak_memory);
59-
59+
6060
group.bench_function("search_with_parsing", |b| {
6161
let client = Arc::clone(&client_clone);
6262
let params = params_clone.clone();
6363
let memory_deltas = Arc::clone(&memory_deltas_clone);
6464
let peak_memory = Arc::clone(&peak_memory_clone);
65-
65+
6666
b.to_async(&rt).iter(|| {
6767
let client = Arc::clone(&client);
6868
let params = params.clone();
6969
let memory_deltas = Arc::clone(&memory_deltas);
7070
let peak_memory = Arc::clone(&peak_memory);
71-
71+
7272
async move {
7373
// Track memory before
7474
let mem_before = memory_stats::memory_stats()
7575
.map(|m| m.physical_mem)
7676
.unwrap_or(0);
77-
77+
7878
// Perform search using the shared client
7979
let result = client.search(black_box(params)).await;
80-
80+
8181
// CPU-intensive operation: parse and access JSON
8282
if let Ok(json) = result {
8383
let _organic_results = json["organic_results"].as_array();
8484
let _local_results = json["local_results"]["places"].as_array();
8585
let _search_metadata = json["search_metadata"].as_object();
86-
86+
8787
// Track memory after
8888
let mem_after = memory_stats::memory_stats()
8989
.map(|m| m.physical_mem)
9090
.unwrap_or(0);
91-
91+
9292
let mem_delta = mem_after.saturating_sub(mem_before);
93-
93+
9494
// Store memory statistics
9595
if let Ok(mut deltas) = memory_deltas.lock() {
9696
deltas.push(mem_delta);
9797
}
9898
if let Ok(mut peak) = peak_memory.lock() {
9999
*peak = (*peak).max(mem_after);
100100
}
101-
101+
102102
black_box((json, mem_delta))
103103
} else {
104104
black_box((serde_json::Value::Null, 0usize))
105105
}
106106
}
107107
});
108108
});
109-
109+
110110
group.finish();
111-
111+
112112
// Report memory statistics
113113
let deltas = memory_deltas.lock().unwrap();
114114
let peak = peak_memory.lock().unwrap();
115-
115+
116116
if !deltas.is_empty() {
117117
let mean_delta: f64 = deltas.iter().sum::<usize>() as f64 / deltas.len() as f64;
118-
let variance: f64 = deltas.iter()
118+
let variance: f64 = deltas
119+
.iter()
119120
.map(|&x| {
120121
let diff = x as f64 - mean_delta;
121122
diff * diff
122123
})
123-
.sum::<f64>() / deltas.len() as f64;
124+
.sum::<f64>()
125+
/ deltas.len() as f64;
124126
let std_dev = variance.sqrt();
125-
127+
126128
println!("\n=== Memory Usage Statistics ===");
127-
println!("Memory Delta (mean): {:.2} KB ({:.2} MB)", mean_delta / 1024.0, mean_delta / (1024.0 * 1024.0));
128-
println!("Memory Delta (std dev): {:.2} KB ({:.2} MB)", std_dev / 1024.0, std_dev / (1024.0 * 1024.0));
129-
println!("Peak Memory: {:.2} KB ({:.2} MB)", *peak as f64 / 1024.0, *peak as f64 / (1024.0 * 1024.0));
129+
println!(
130+
"Memory Delta (mean): {:.2} KB ({:.2} MB)",
131+
mean_delta / 1024.0,
132+
mean_delta / (1024.0 * 1024.0)
133+
);
134+
println!(
135+
"Memory Delta (std dev): {:.2} KB ({:.2} MB)",
136+
std_dev / 1024.0,
137+
std_dev / (1024.0 * 1024.0)
138+
);
139+
println!(
140+
"Peak Memory: {:.2} KB ({:.2} MB)",
141+
*peak as f64 / 1024.0,
142+
*peak as f64 / (1024.0 * 1024.0)
143+
);
130144
println!("Samples: {}", deltas.len());
131145
println!("===============================\n");
132146
}

examples/apple_app_store_search.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1111
// ```bash
1212
// export API_key="paste_your_private_api_key"
1313
// ```
14-
let api_key = match env::var_os("API_KEY") {
14+
let api_key = match env::var_os("SERPAPI_KEY") {
1515
Some(v) => v.into_string().unwrap(),
16-
None => panic!("$API_KEY environment variable is not set!"),
16+
None => panic!("$SERPAPI_KEY environment variable is not set!"),
1717
};
1818

1919
println!("let's initiliaze the client to search on apple_app_store");

examples/baidu_search.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1111
// ```bash
1212
// export API_key="paste_your_private_api_key"
1313
// ```
14-
let api_key = match env::var_os("API_KEY") {
14+
let api_key = match env::var_os("SERPAPI_KEY") {
1515
Some(v) => v.into_string().unwrap(),
16-
None => panic!("$API_KEY environment variable is not set!"),
16+
None => panic!("$SERPAPI_KEY environment variable is not set!"),
1717
};
1818

1919
println!("let's initiliaze the client to search on baidu");

examples/bing_search.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1111
// ```bash
1212
// export API_key="paste_your_private_api_key"
1313
// ```
14-
let api_key = match env::var_os("API_KEY") {
14+
let api_key = match env::var_os("SERPAPI_KEY") {
1515
Some(v) => v.into_string().unwrap(),
16-
None => panic!("$API_KEY environment variable is not set!"),
16+
None => panic!("$SERPAPI_KEY environment variable is not set!"),
1717
};
1818

1919
println!("let's initiliaze the client to search on bing");

examples/duckduckgo_search.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1111
// ```bash
1212
// export API_key="paste_your_private_api_key"
1313
// ```
14-
let api_key = match env::var_os("API_KEY") {
14+
let api_key = match env::var_os("SERPAPI_KEY") {
1515
Some(v) => v.into_string().unwrap(),
16-
None => panic!("$API_KEY environment variable is not set!"),
16+
None => panic!("$SERPAPI_KEY environment variable is not set!"),
1717
};
1818

1919
println!("let's initiliaze the client to search on duckduckgo");

examples/ebay_search.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1111
// ```bash
1212
// export API_key="paste_your_private_api_key"
1313
// ```
14-
let api_key = match env::var_os("API_KEY") {
14+
let api_key = match env::var_os("SERPAPI_KEY") {
1515
Some(v) => v.into_string().unwrap(),
16-
None => panic!("$API_KEY environment variable is not set!"),
16+
None => panic!("$SERPAPI_KEY environment variable is not set!"),
1717
};
1818

1919
println!("let's initiliaze the client to search on ebay");

examples/google_autocomplete_search.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1111
// ```bash
1212
// export API_key="paste_your_private_api_key"
1313
// ```
14-
let api_key = match env::var_os("API_KEY") {
14+
let api_key = match env::var_os("SERPAPI_KEY") {
1515
Some(v) => v.into_string().unwrap(),
16-
None => panic!("$API_KEY environment variable is not set!"),
16+
None => panic!("$SERPAPI_KEY environment variable is not set!"),
1717
};
1818

1919
println!("let's initiliaze the client to search on google_autocomplete");

examples/google_events_search.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1111
// ```bash
1212
// export API_key="paste_your_private_api_key"
1313
// ```
14-
let api_key = match env::var_os("API_KEY") {
14+
let api_key = match env::var_os("SERPAPI_KEY") {
1515
Some(v) => v.into_string().unwrap(),
16-
None => panic!("$API_KEY environment variable is not set!"),
16+
None => panic!("$SERPAPI_KEY environment variable is not set!"),
1717
};
1818

1919
println!("let's initiliaze the client to search on google_events");

examples/google_images_search.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1111
// ```bash
1212
// export API_key="paste_your_private_api_key"
1313
// ```
14-
let api_key = match env::var_os("API_KEY") {
14+
let api_key = match env::var_os("SERPAPI_KEY") {
1515
Some(v) => v.into_string().unwrap(),
16-
None => panic!("$API_KEY environment variable is not set!"),
16+
None => panic!("$SERPAPI_KEY environment variable is not set!"),
1717
};
1818

1919
println!("let's initiliaze the client to search on google_images");

examples/google_jobs_search.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1111
// ```bash
1212
// export API_key="paste_your_private_api_key"
1313
// ```
14-
let api_key = match env::var_os("API_KEY") {
14+
let api_key = match env::var_os("SERPAPI_KEY") {
1515
Some(v) => v.into_string().unwrap(),
16-
None => panic!("$API_KEY environment variable is not set!"),
16+
None => panic!("$SERPAPI_KEY environment variable is not set!"),
1717
};
1818

1919
println!("let's initiliaze the client to search on google_jobs");

0 commit comments

Comments
 (0)