suggest/benchmarks/
mod.rs1use std::{
14 path::PathBuf,
15 sync::{
16 atomic::{AtomicU32, Ordering},
17 Mutex,
18 },
19};
20use tempfile::TempDir;
21
22use crate::{SuggestIngestionConstraints, SuggestStore};
23use remote_settings::{RemoteSettingsConfig2, RemoteSettingsContext, RemoteSettingsService};
24
25use std::sync::Arc;
26
27pub mod client;
28pub mod geoname;
29pub mod ingest;
30pub mod query;
31
32pub trait Benchmark {
37 fn benchmarked_code(&self);
39}
40
41pub trait BenchmarkWithInput {
48 type GlobalInput;
51
52 type IterationInput;
54
55 fn global_input(&self) -> Self::GlobalInput;
57
58 fn iteration_input(&self) -> Self::IterationInput;
60
61 fn benchmarked_code(&self, g_input: &Self::GlobalInput, i_input: Self::IterationInput);
63}
64
65fn unique_db_filename() -> String {
66 static COUNTER: AtomicU32 = AtomicU32::new(0);
67 format!("db{}.sqlite", COUNTER.fetch_add(1, Ordering::Relaxed))
68}
69
70fn unique_remote_settings_dir() -> String {
71 static COUNTER: AtomicU32 = AtomicU32::new(0);
72 format!(
73 "remote-settings-{}",
74 COUNTER.fetch_add(1, Ordering::Relaxed)
75 )
76}
77
78static STARTER: Mutex<Option<(TempDir, PathBuf)>> = Mutex::new(None);
82
83fn new_store() -> SuggestStore {
86 let mut starter = STARTER.lock().unwrap();
87 let (starter_dir, starter_db_path) = starter.get_or_insert_with(|| {
88 let temp_dir = tempfile::tempdir().unwrap();
89 let db_path = temp_dir.path().join(unique_db_filename());
90 let remote_settings_dir = temp_dir.path().join(unique_remote_settings_dir());
91 let rs_config = RemoteSettingsConfig2 {
92 bucket_name: None,
93 server: None,
94 app_context: Some(RemoteSettingsContext::default()),
95 };
96 let remote_settings_service = Arc::new(RemoteSettingsService::new(
97 remote_settings_dir.to_string_lossy().to_string(),
98 rs_config,
99 ));
100 let store = SuggestStore::new(&db_path.to_string_lossy(), remote_settings_service);
101 store
102 .ingest(SuggestIngestionConstraints::all_providers())
103 .expect("Error during ingestion");
104 store.checkpoint();
105 (temp_dir, db_path)
106 });
107
108 let db_path = starter_dir.path().join(unique_db_filename());
109 let rs_config = RemoteSettingsConfig2 {
110 bucket_name: None,
111 server: None,
112 app_context: Some(RemoteSettingsContext::default()),
113 };
114 let remote_settings_service = Arc::new(RemoteSettingsService::new("".to_string(), rs_config));
115 std::fs::copy(starter_db_path, &db_path).expect("Error copying starter DB file");
116 SuggestStore::new(&db_path.to_string_lossy(), remote_settings_service)
117}
118
119pub fn cleanup() {
121 *STARTER.lock().unwrap() = None;
122}