Skip to content

Add a metric containing the size of generated documentation #1417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions collector/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
8 changes: 8 additions & 0 deletions site/src/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
}
}

Expand Down