mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-24 18:21:08 +00:00
Drop system cache for trie benchmarks (#7242)
* add system clear cache * move to shared * rearrange a little-a * add few tricks also * use tempdir for buf as well * Apply suggestions from code review Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
@@ -178,6 +178,7 @@ impl sp_state_machine::Storage<sp_core::Blake2Hasher> for Storage {
|
|||||||
impl core::Benchmark for TrieReadBenchmark {
|
impl core::Benchmark for TrieReadBenchmark {
|
||||||
fn run(&mut self, mode: Mode) -> std::time::Duration {
|
fn run(&mut self, mode: Mode) -> std::time::Duration {
|
||||||
let mut db = self.database.clone();
|
let mut db = self.database.clone();
|
||||||
|
|
||||||
let storage: Arc<dyn sp_state_machine::Storage<sp_core::Blake2Hasher>> =
|
let storage: Arc<dyn sp_state_machine::Storage<sp_core::Blake2Hasher>> =
|
||||||
Arc::new(Storage(db.open(self.database_type)));
|
Arc::new(Storage(db.open(self.database_type)));
|
||||||
|
|
||||||
|
|||||||
@@ -88,6 +88,61 @@ impl BenchPair {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Drop system cache.
|
||||||
|
///
|
||||||
|
/// Will panic if cache drop is impossbile.
|
||||||
|
pub fn drop_system_cache() {
|
||||||
|
#[cfg(target_os = "windows")] {
|
||||||
|
log::warn!(
|
||||||
|
target: "bench-logistics",
|
||||||
|
"Clearing system cache on windows is not supported. Benchmark might totally be wrong.",
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::process::Command::new("sync")
|
||||||
|
.output()
|
||||||
|
.expect("Failed to execute system cache clear");
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")] {
|
||||||
|
log::trace!(target: "bench-logistics", "Clearing system cache...");
|
||||||
|
std::process::Command::new("echo")
|
||||||
|
.args(&["3", ">", "/proc/sys/vm/drop_caches", "2>", "/dev/null"])
|
||||||
|
.output()
|
||||||
|
.expect("Failed to execute system cache clear");
|
||||||
|
|
||||||
|
let temp = tempfile::tempdir().expect("Failed to spawn tempdir");
|
||||||
|
let temp_file_path = format!("of={}/buf", temp.path().to_string_lossy());
|
||||||
|
|
||||||
|
// this should refill write cache with 2GB of garbage
|
||||||
|
std::process::Command::new("dd")
|
||||||
|
.args(&["if=/dev/urandom", &temp_file_path, "bs=64M", "count=32"])
|
||||||
|
.output()
|
||||||
|
.expect("Failed to execute dd for cache clear");
|
||||||
|
|
||||||
|
// remove tempfile of previous command
|
||||||
|
std::process::Command::new("rm")
|
||||||
|
.arg(&temp_file_path)
|
||||||
|
.output()
|
||||||
|
.expect("Failed to remove temp file");
|
||||||
|
|
||||||
|
std::process::Command::new("sync")
|
||||||
|
.output()
|
||||||
|
.expect("Failed to execute system cache clear");
|
||||||
|
|
||||||
|
log::trace!(target: "bench-logistics", "Clearing system cache done!");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")] {
|
||||||
|
log::trace!(target: "bench-logistics", "Clearing system cache...");
|
||||||
|
if let Err(err) = std::process::Command::new("purge").output() {
|
||||||
|
log::error!("purge error {:?}: ", err);
|
||||||
|
panic!("Could not clear system cache. Run under sudo?");
|
||||||
|
}
|
||||||
|
log::trace!(target: "bench-logistics", "Clearing system cache done!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Pre-initialized benchmarking database.
|
/// Pre-initialized benchmarking database.
|
||||||
///
|
///
|
||||||
/// This is prepared database with genesis and keyring
|
/// This is prepared database with genesis and keyring
|
||||||
@@ -124,6 +179,13 @@ impl Clone for BenchDb {
|
|||||||
&fs_extra::dir::CopyOptions::new(),
|
&fs_extra::dir::CopyOptions::new(),
|
||||||
).expect("Copy of seed database is ok");
|
).expect("Copy of seed database is ok");
|
||||||
|
|
||||||
|
// We clear system cache after db clone but before any warmups.
|
||||||
|
// This populates system cache with some data unrelated to actual
|
||||||
|
// data we will be quering further under benchmark (like what
|
||||||
|
// would have happened in real system that queries random entries
|
||||||
|
// from database).
|
||||||
|
drop_system_cache();
|
||||||
|
|
||||||
BenchDb { keyring, directory_guard: Guard(dir), database_type }
|
BenchDb { keyring, directory_guard: Guard(dir), database_type }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user