Skip to content

Commit 53fb804

Browse files
authored
Merge pull request #1417 from rust-lang/doc-size
Add a metric containing the size of generated documentation
2 parents cacc2f6 + c27d156 commit 53fb804

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

collector/src/execute.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,12 @@ impl<'a> Processor for BenchProcessor<'a> {
848848
if let Some(ref profile) = res.1 {
849849
store_artifact_sizes_into_stats(&mut res.0, profile);
850850
}
851+
if let Profile::Doc = data.profile {
852+
let doc_dir = data.cwd.join("target/doc");
853+
if doc_dir.is_dir() {
854+
store_documentation_size_into_stats(&mut res.0, &doc_dir);
855+
}
856+
}
851857

852858
match data.scenario {
853859
Scenario::Full => {
@@ -894,6 +900,44 @@ impl<'a> Processor for BenchProcessor<'a> {
894900
}
895901
}
896902

903+
fn store_documentation_size_into_stats(stats: &mut Stats, doc_dir: &Path) {
904+
match get_file_count_and_size(doc_dir) {
905+
Ok((count, size)) => {
906+
stats.insert("size:doc_files_count".to_string(), count as f64);
907+
stats.insert("size:doc_bytes".to_string(), size as f64);
908+
}
909+
Err(error) => log::error!(
910+
"Cannot get size of documentation directory {}: {:?}",
911+
doc_dir.display(),
912+
error
913+
),
914+
}
915+
}
916+
917+
/// Counts the number of files and the total size of all files within the given `path`.
918+
/// File size is counted as the actual size in bytes, i.e. the size returned by
919+
/// [std::path::Path::metadata].
920+
///
921+
/// Returns (file_count, size).
922+
pub fn get_file_count_and_size(path: &Path) -> std::io::Result<(u64, u64)> {
923+
let (count, size) = if path.is_dir() {
924+
let mut file_count = 0;
925+
let mut total_size = 0;
926+
for entry in fs::read_dir(&path)? {
927+
let path = entry?.path();
928+
let (count, size) = get_file_count_and_size(&path)?;
929+
file_count += count;
930+
total_size += size;
931+
}
932+
(file_count, total_size)
933+
} else if path.is_file() {
934+
(1, path.metadata()?.len())
935+
} else {
936+
(0, 0)
937+
};
938+
Ok((count, size))
939+
}
940+
897941
fn store_artifact_sizes_into_stats(stats: &mut Stats, profile: &SelfProfile) {
898942
for artifact in profile.artifact_sizes.iter() {
899943
stats
@@ -1758,3 +1802,31 @@ pub struct QueryData {
17581802
pub blocked_time: Duration,
17591803
pub incremental_load_time: Duration,
17601804
}
1805+
1806+
#[cfg(test)]
1807+
mod tests {
1808+
use crate::execute::get_file_count_and_size;
1809+
use std::path::PathBuf;
1810+
1811+
#[test]
1812+
fn test_get_file_count_and_size() {
1813+
let dir = tempfile::TempDir::new().unwrap();
1814+
let root = dir.path();
1815+
1816+
let write = |path: PathBuf, size: usize| {
1817+
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
1818+
std::fs::write(path, vec![0u8; size].as_slice()).unwrap();
1819+
};
1820+
1821+
write(root.join("a/b/c.rs"), 1024);
1822+
write(root.join("a/b/d.rs"), 16);
1823+
write(root.join("a/x.rs"), 32);
1824+
write(root.join("b/x.rs"), 64);
1825+
write(root.join("b/x2.rs"), 64);
1826+
write(root.join("x.rs"), 128);
1827+
1828+
let (files, size) = get_file_count_and_size(root).unwrap();
1829+
assert_eq!(files, 6);
1830+
assert_eq!(size, 1024 + 16 + 32 + 64 + 64 + 128);
1831+
}
1832+
}

site/src/comparison.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ pub enum Metric {
230230
LlvmBitcodeSize,
231231
#[serde(rename = "size:llvm_ir")]
232232
LlvmIrSize,
233+
/// Total bytes of a generated documentation directory
234+
#[serde(rename = "size:doc_bytes")]
235+
DocByteSize,
236+
/// Number of files inside a generated documentation directory.
237+
#[serde(rename = "size:doc_files_count")]
238+
DocFilesCount,
233239
}
234240

235241
impl Metric {
@@ -259,6 +265,8 @@ impl Metric {
259265
Metric::AssemblyFileSize => "size:assembly_file",
260266
Metric::LlvmBitcodeSize => "size:llvm_bitcode",
261267
Metric::LlvmIrSize => "size:llvm_ir",
268+
Metric::DocByteSize => "size:doc_bytes",
269+
Metric::DocFilesCount => "size:doc_files_count",
262270
}
263271
}
264272

0 commit comments

Comments
 (0)