From c27d15642174cb55892bbc11530b8239b41aaf8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Fri, 19 Aug 2022 10:05:13 +0200 Subject: [PATCH] Add a metric containing the size and file count of generated documentation --- collector/src/execute.rs | 72 ++++++++++++++++++++++++++++++++++++++++ site/src/comparison.rs | 8 +++++ 2 files changed, 80 insertions(+) diff --git a/collector/src/execute.rs b/collector/src/execute.rs index 66f99020c..8f42756a8 100644 --- a/collector/src/execute.rs +++ b/collector/src/execute.rs @@ -848,6 +848,12 @@ impl<'a> Processor for BenchProcessor<'a> { if let Some(ref profile) = res.1 { store_artifact_sizes_into_stats(&mut res.0, profile); } + if let Profile::Doc = data.profile { + let doc_dir = data.cwd.join("target/doc"); + if doc_dir.is_dir() { + store_documentation_size_into_stats(&mut res.0, &doc_dir); + } + } match data.scenario { Scenario::Full => { @@ -894,6 +900,44 @@ impl<'a> Processor for BenchProcessor<'a> { } } +fn store_documentation_size_into_stats(stats: &mut Stats, doc_dir: &Path) { + match get_file_count_and_size(doc_dir) { + Ok((count, size)) => { + stats.insert("size:doc_files_count".to_string(), count as f64); + stats.insert("size:doc_bytes".to_string(), size as f64); + } + Err(error) => log::error!( + "Cannot get size of documentation directory {}: {:?}", + doc_dir.display(), + error + ), + } +} + +/// Counts the number of files and the total size of all files within the given `path`. +/// File size is counted as the actual size in bytes, i.e. the size returned by +/// [std::path::Path::metadata]. +/// +/// Returns (file_count, size). +pub fn get_file_count_and_size(path: &Path) -> std::io::Result<(u64, u64)> { + let (count, size) = if path.is_dir() { + let mut file_count = 0; + let mut total_size = 0; + for entry in fs::read_dir(&path)? { + let path = entry?.path(); + let (count, size) = get_file_count_and_size(&path)?; + file_count += count; + total_size += size; + } + (file_count, total_size) + } else if path.is_file() { + (1, path.metadata()?.len()) + } else { + (0, 0) + }; + Ok((count, size)) +} + fn store_artifact_sizes_into_stats(stats: &mut Stats, profile: &SelfProfile) { for artifact in profile.artifact_sizes.iter() { stats @@ -1758,3 +1802,31 @@ pub struct QueryData { pub blocked_time: Duration, pub incremental_load_time: Duration, } + +#[cfg(test)] +mod tests { + use crate::execute::get_file_count_and_size; + use std::path::PathBuf; + + #[test] + fn test_get_file_count_and_size() { + let dir = tempfile::TempDir::new().unwrap(); + let root = dir.path(); + + let write = |path: PathBuf, size: usize| { + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(path, vec![0u8; size].as_slice()).unwrap(); + }; + + write(root.join("a/b/c.rs"), 1024); + write(root.join("a/b/d.rs"), 16); + write(root.join("a/x.rs"), 32); + write(root.join("b/x.rs"), 64); + write(root.join("b/x2.rs"), 64); + write(root.join("x.rs"), 128); + + let (files, size) = get_file_count_and_size(root).unwrap(); + assert_eq!(files, 6); + assert_eq!(size, 1024 + 16 + 32 + 64 + 64 + 128); + } +} diff --git a/site/src/comparison.rs b/site/src/comparison.rs index c9021d3e2..10c227022 100644 --- a/site/src/comparison.rs +++ b/site/src/comparison.rs @@ -231,6 +231,12 @@ pub enum Metric { LlvmBitcodeSize, #[serde(rename = "size:llvm_ir")] LlvmIrSize, + /// Total bytes of a generated documentation directory + #[serde(rename = "size:doc_bytes")] + DocByteSize, + /// Number of files inside a generated documentation directory. + #[serde(rename = "size:doc_files_count")] + DocFilesCount, } impl Metric { @@ -260,6 +266,8 @@ impl Metric { Metric::AssemblyFileSize => "size:assembly_file", Metric::LlvmBitcodeSize => "size:llvm_bitcode", Metric::LlvmIrSize => "size:llvm_ir", + Metric::DocByteSize => "size:doc_bytes", + Metric::DocFilesCount => "size:doc_files_count", } }