|
| 1 | +use std::{ |
| 2 | + fmt::Write, |
| 3 | + path::{Path, PathBuf}, |
| 4 | +}; |
| 5 | + |
| 6 | +use flate2::{write::GzEncoder, Compression}; |
| 7 | +use humansize::format_size; |
| 8 | +use indexmap::IndexMap; |
| 9 | +use rustc_hash::FxBuildHasher; |
| 10 | +use swc::{config::JsMinifyOptions, BoolOrDataConfig, Compiler}; |
| 11 | +use swc_common::{FileName, GLOBALS}; |
| 12 | +use swc_ecma_utils::parallel::{Parallel, ParallelExt}; |
| 13 | +use testing::NormalizedOutput; |
| 14 | +use walkdir::WalkDir; |
| 15 | + |
| 16 | +struct FileSize { |
| 17 | + original_size: usize, |
| 18 | + compressed_size: usize, |
| 19 | + gzipped_size: usize, |
| 20 | +} |
| 21 | + |
| 22 | +#[test] |
| 23 | +fn bench_libs() { |
| 24 | + let dir = PathBuf::from("../swc_ecma_minifier/benches/full"); |
| 25 | + |
| 26 | + let files = expand_dirs(&dir); |
| 27 | + |
| 28 | + let result = calc_size_par(&files); |
| 29 | + let output = format_result(result); |
| 30 | + |
| 31 | + NormalizedOutput::from(output) |
| 32 | + .compare_to_file("tests/libs-size.snapshot.md") |
| 33 | + .unwrap(); |
| 34 | +} |
| 35 | + |
| 36 | +fn format_result(mut result: IndexMap<String, FileSize, FxBuildHasher>) -> String { |
| 37 | + let mut output = String::new(); |
| 38 | + |
| 39 | + // Sort by file name |
| 40 | + result.sort_by_cached_key(|file_name, _| file_name.clone()); |
| 41 | + |
| 42 | + writeln!( |
| 43 | + output, |
| 44 | + "| File | Original Size | Compressed Size | Gzipped Size |" |
| 45 | + ) |
| 46 | + .unwrap(); |
| 47 | + writeln!(output, "| --- | --- | --- | --- |").unwrap(); |
| 48 | + |
| 49 | + for (file_name, file_size) in result { |
| 50 | + writeln!( |
| 51 | + output, |
| 52 | + "| {} | {} | {} | {} |", |
| 53 | + file_name, |
| 54 | + format_size(file_size.original_size, humansize::BINARY), |
| 55 | + format_size(file_size.compressed_size, humansize::BINARY), |
| 56 | + format_size(file_size.gzipped_size, humansize::BINARY) |
| 57 | + ) |
| 58 | + .unwrap(); |
| 59 | + } |
| 60 | + |
| 61 | + output |
| 62 | +} |
| 63 | + |
| 64 | +fn calc_size_par(files: &[PathBuf]) -> IndexMap<String, FileSize, FxBuildHasher> { |
| 65 | + GLOBALS.set(&Default::default(), || { |
| 66 | + let mut worker = Worker::default(); |
| 67 | + |
| 68 | + worker.maybe_par(0, files, |worker, path| { |
| 69 | + let file_name = path.file_name().unwrap().to_str().unwrap(); |
| 70 | + let src = std::fs::read_to_string(path).unwrap(); |
| 71 | + let file_size = run(&src); |
| 72 | + |
| 73 | + worker.result.insert(file_name.to_string(), file_size); |
| 74 | + }); |
| 75 | + |
| 76 | + worker.result |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +/// Return the whole input files as abolute path. |
| 81 | +fn expand_dirs(dir: &Path) -> Vec<PathBuf> { |
| 82 | + WalkDir::new(dir) |
| 83 | + .into_iter() |
| 84 | + .filter_map(Result::ok) |
| 85 | + .filter_map(|entry| { |
| 86 | + if entry.metadata().map(|v| v.is_file()).unwrap_or(false) { |
| 87 | + Some(entry.into_path()) |
| 88 | + } else { |
| 89 | + None |
| 90 | + } |
| 91 | + }) |
| 92 | + .filter(|path| path.extension().map(|ext| ext == "js").unwrap_or(false)) |
| 93 | + .collect::<Vec<_>>() |
| 94 | +} |
| 95 | + |
| 96 | +#[derive(Default)] |
| 97 | +struct Worker { |
| 98 | + result: IndexMap<String, FileSize, FxBuildHasher>, |
| 99 | +} |
| 100 | + |
| 101 | +impl Parallel for Worker { |
| 102 | + fn create(&self) -> Self { |
| 103 | + Worker { |
| 104 | + ..Default::default() |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + fn merge(&mut self, other: Self) { |
| 109 | + self.result.extend(other.result); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +fn run(src: &str) -> FileSize { |
| 114 | + testing::run_test2(false, |cm, handler| { |
| 115 | + let c = Compiler::new(cm.clone()); |
| 116 | + let fm = cm.new_source_file(FileName::Anon.into(), src.into()); |
| 117 | + |
| 118 | + let mut config = JsMinifyOptions::default(); |
| 119 | + config.format.comments = BoolOrDataConfig::from_bool(false); |
| 120 | + |
| 121 | + let output = c.minify(fm, &handler, &config, Default::default()).unwrap(); |
| 122 | + |
| 123 | + let code = output.code; |
| 124 | + let gzipped_size = gzip_size(code.as_bytes()); |
| 125 | + |
| 126 | + Ok(FileSize { |
| 127 | + original_size: src.len(), |
| 128 | + compressed_size: code.len(), |
| 129 | + gzipped_size, |
| 130 | + }) |
| 131 | + }) |
| 132 | + .unwrap() |
| 133 | +} |
| 134 | + |
| 135 | +fn gzip_size(data: &[u8]) -> usize { |
| 136 | + use std::io::Write; |
| 137 | + |
| 138 | + let mut encoder = GzEncoder::new(Vec::new(), Compression::best()); |
| 139 | + encoder.write_all(data).unwrap(); |
| 140 | + encoder.finish().unwrap().len() |
| 141 | +} |
0 commit comments