From 8d68b76ba01dcd190e037e4d61a94d4c9d568bdd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Jan 2019 16:10:34 +0300 Subject: better stats --- crates/ra_ide_api/src/status.rs | 50 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide_api/src/status.rs b/crates/ra_ide_api/src/status.rs index d499dd878..c184ca892 100644 --- a/crates/ra_ide_api/src/status.rs +++ b/crates/ra_ide_api/src/status.rs @@ -1,3 +1,6 @@ +use std::fmt; + +use ra_syntax::AstNode; use ra_db::{ SourceFileQuery, salsa::{Database, debug::DebugQueryTable}, @@ -6,10 +9,53 @@ use ra_db::{ use crate::db::RootDatabase; pub(crate) fn status(db: &RootDatabase) -> String { - let n_parsed_files = db.query(SourceFileQuery).entries::>().len(); + let file_stats = { + let mut stats = FilesStats::default(); + for entry in db.query(SourceFileQuery).entries::>() { + stats.total += 1; + if let Some(value) = entry.value { + stats.retained += 1; + stats.retained_size = stats + .retained_size + .checked_add(value.syntax().memory_size_of_subtree()) + .unwrap(); + } + } + stats + }; let n_defs = { let interner: &hir::HirInterner = db.as_ref(); interner.len() }; - format!("#n_parsed_files {}\n#n_defs {}\n", n_parsed_files, n_defs) + format!("{}\nn_defs {}\n", file_stats, n_defs) +} + +#[derive(Default)] +struct FilesStats { + total: usize, + retained: usize, + retained_size: usize, +} + +impl fmt::Display for FilesStats { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let (size, suff) = human_bytes(self.retained_size); + write!( + fmt, + "{} parsed_files, {} ({}{}) retained", + self.total, self.retained, size, suff + ) + } +} + +fn human_bytes(bytes: usize) -> (usize, &'static str) { + if bytes < 4096 { + return (bytes, " bytes"); + } + let kb = bytes / 1024; + if kb < 4096 { + return (kb, "kb"); + } + let mb = kb / 1024; + (mb, "mb") } -- cgit v1.2.3