From 0bed19e5ed2687c8dbf68288f6c4f63982c3b368 Mon Sep 17 00:00:00 2001 From: Wilson Lin Date: Mon, 9 Aug 2021 20:42:47 +1000 Subject: [PATCH] Allow comparison --- bench/build | 4 +-- bench/run | 4 +-- bench/runners/@minify-html%2Fjs/index.js | 12 ++++++-- bench/runners/README.md | 3 +- bench/runners/html-minifier/index.js | 12 ++++++-- bench/runners/minify-html-onepass/src/main.rs | 25 +++++++++------- bench/runners/minify-html/src/main.rs | 24 ++++++++------- bench/runners/minimize/index.js | 14 +++++++-- debug/diff/.gitignore | 1 + debug/diff/README.md | 2 +- debug/diff/compare | 9 ++++++ debug/diff/compare.sh | 3 -- debug/diff/run | 30 +++++++++++++++++++ 13 files changed, 106 insertions(+), 37 deletions(-) create mode 100644 debug/diff/.gitignore create mode 100755 debug/diff/compare delete mode 100644 debug/diff/compare.sh create mode 100755 debug/diff/run diff --git a/bench/build b/bench/build index f6acb33..2850270 100755 --- a/bench/build +++ b/bench/build @@ -18,6 +18,6 @@ for r in *; do done popd >/dev/null -popd >/dev/null - echo "All done!" + +popd >/dev/null diff --git a/bench/run b/bench/run index 694e604..87e9248 100755 --- a/bench/run +++ b/bench/run @@ -29,6 +29,6 @@ for r in *; do done popd >/dev/null -popd >/dev/null - echo "All done!" + +popd >/dev/null diff --git a/bench/runners/@minify-html%2Fjs/index.js b/bench/runners/@minify-html%2Fjs/index.js index c81d99f..746ff87 100644 --- a/bench/runners/@minify-html%2Fjs/index.js +++ b/bench/runners/@minify-html%2Fjs/index.js @@ -5,6 +5,7 @@ const path = require("path"); const iterations = parseInt(process.env.MHB_ITERATIONS, 10); const inputDir = process.env.MHB_INPUT_DIR; const htmlOnly = process.env.MHB_HTML_ONLY === "1"; +const outputDir = process.env.MHB_OUTPUT_DIR; const minifyHtmlCfg = minifyHtml.createConfiguration({ minify_css: !htmlOnly, @@ -13,12 +14,19 @@ const minifyHtmlCfg = minifyHtml.createConfiguration({ const results = fs.readdirSync(inputDir).map((name) => { const src = fs.readFileSync(path.join(inputDir, name)); + + const out = minifyHtml.minify(src, minifyHtmlCfg); + const len = out.byteLength; + if (outputDir) { + fs.writeFileSync(path.join(outputDir, name), out); + } + const start = process.hrtime.bigint(); - let len; for (let i = 0; i < iterations; i++) { - len = minifyHtml.minify(src, minifyHtmlCfg).byteLength; + minifyHtml.minify(src, minifyHtmlCfg); } const elapsed = process.hrtime.bigint() - start; + return [name, len, iterations, Number(elapsed) / 1_000_000_000]; }); console.log(JSON.stringify(results)); diff --git a/bench/runners/README.md b/bench/runners/README.md index 75eeb4f..52e64bd 100644 --- a/bench/runners/README.md +++ b/bench/runners/README.md @@ -5,5 +5,6 @@ - `MHB_ITERATIONS`: times to run each input. - `MHB_INPUT_DIR`: path to directory containing inputs. Files should be read from this directory and used as the inputs. - `MHB_HTML_ONLY`: if set to `1`, `minify_css` and `minify_js` should be disabled. -- The output should be a JSON array of tuples, where each tuples contains the input name, output size, iterations, and execution time in seconds (as a floating point value). + - `MHB_OUTPUT_DIR`: if set, output minified HTML for each input in this folder. +- The output should be a JSON array of tuples, where each tuples contains the input name, output UTF-8 byte length, iterations, and execution time in seconds (as a floating point value). - The execution time should be measured using high-precision monotonic system clocks where possible. diff --git a/bench/runners/html-minifier/index.js b/bench/runners/html-minifier/index.js index 9e14a9a..cc3e9c4 100644 --- a/bench/runners/html-minifier/index.js +++ b/bench/runners/html-minifier/index.js @@ -6,6 +6,7 @@ const path = require("path"); const iterations = parseInt(process.env.MHB_ITERATIONS, 10); const inputDir = process.env.MHB_INPUT_DIR; const htmlOnly = process.env.MHB_HTML_ONLY === "1"; +const outputDir = process.env.MHB_OUTPUT_DIR; const esbuildCss = (code) => esbuild.transformSync(code, { @@ -47,12 +48,19 @@ const htmlMinifierCfg = { const results = fs.readdirSync(inputDir).map((name) => { const src = fs.readFileSync(path.join(inputDir, name), "utf8"); + + const out = htmlMinifier.minify(src, htmlMinifierCfg); + const len = Buffer.from(out, "utf8").length; + if (outputDir) { + fs.writeFileSync(path.join(outputDir, name), out); + } + const start = process.hrtime.bigint(); - let len; for (let i = 0; i < iterations; i++) { - len = htmlMinifier.minify(src, htmlMinifierCfg).length; + htmlMinifier.minify(src, htmlMinifierCfg); } const elapsed = process.hrtime.bigint() - start; + return [name, len, iterations, Number(elapsed) / 1_000_000_000]; }); console.log(JSON.stringify(results)); diff --git a/bench/runners/minify-html-onepass/src/main.rs b/bench/runners/minify-html-onepass/src/main.rs index b95ee74..5f28895 100644 --- a/bench/runners/minify-html-onepass/src/main.rs +++ b/bench/runners/minify-html-onepass/src/main.rs @@ -14,8 +14,7 @@ fn main() { .ok() .filter(|v| v == "1") .is_some(); - - let tests = fs::read_dir(input_dir).unwrap().map(|d| d.unwrap()); + let output_dir = env::var("MHB_OUTPUT_DIR").ok(); let mut results: Vec<(String, usize, usize, f64)> = Vec::new(); let cfg = Cfg { @@ -23,21 +22,25 @@ fn main() { minify_js: !html_only, }; - for t in tests { + for t in fs::read_dir(input_dir).unwrap().map(|d| d.unwrap()) { let source = fs::read(t.path()).unwrap(); + let input_name = t.file_name().into_string().unwrap(); + + let mut output = source.to_vec(); + let len = in_place(&mut output, &cfg).expect("failed to minify"); + output.truncate(len); + if let Some(output_dir) = &output_dir { + fs::write(format!("{}/{}", output_dir, input_name), output).unwrap(); + }; + let start = Instant::now(); - let mut len = 0; for _ in 0..iterations { let mut data = source.to_vec(); - len = in_place(&mut data, &cfg).expect("failed to minify"); + let _ = in_place(&mut data, &cfg).expect("failed to minify"); } let elapsed = start.elapsed().as_secs_f64(); - results.push(( - t.file_name().into_string().unwrap(), - len, - iterations, - elapsed, - )); + + results.push((input_name, len, iterations, elapsed)); } serde_json::to_writer(stdout(), &results).unwrap(); diff --git a/bench/runners/minify-html/src/main.rs b/bench/runners/minify-html/src/main.rs index ccfe9fe..ce1ad56 100644 --- a/bench/runners/minify-html/src/main.rs +++ b/bench/runners/minify-html/src/main.rs @@ -14,8 +14,7 @@ fn main() { .ok() .filter(|v| v == "1") .is_some(); - - let tests = fs::read_dir(input_dir).unwrap().map(|d| d.unwrap()); + let output_dir = env::var("MHB_OUTPUT_DIR").ok(); let mut results: Vec<(String, usize, usize, f64)> = Vec::new(); let mut cfg = Cfg::new(); @@ -24,20 +23,23 @@ fn main() { cfg.minify_js = true; }; - for t in tests { + for t in fs::read_dir(input_dir).unwrap().map(|d| d.unwrap()) { let source = fs::read(t.path()).unwrap(); + let input_name = t.file_name().into_string().unwrap(); + + let output = minify(&source, &cfg); + let len = output.len(); + if let Some(output_dir) = &output_dir { + fs::write(format!("{}/{}", output_dir, input_name), output).unwrap(); + }; + let start = Instant::now(); - let mut len = 0; for _ in 0..iterations { - len = minify(&source, &cfg).len(); + let _ = minify(&source, &cfg); } let elapsed = start.elapsed().as_secs_f64(); - results.push(( - t.file_name().into_string().unwrap(), - len, - iterations, - elapsed, - )); + + results.push((input_name, len, iterations, elapsed)); } serde_json::to_writer(stdout(), &results).unwrap(); diff --git a/bench/runners/minimize/index.js b/bench/runners/minimize/index.js index f2d7069..4aa5628 100644 --- a/bench/runners/minimize/index.js +++ b/bench/runners/minimize/index.js @@ -6,6 +6,7 @@ const path = require("path"); const iterations = parseInt(process.env.MHB_ITERATIONS, 10); const inputDir = process.env.MHB_INPUT_DIR; const htmlOnly = process.env.MHB_HTML_ONLY === "1"; +const outputDir = process.env.MHB_OUTPUT_DIR; const jsMime = new Set([ undefined, @@ -58,14 +59,23 @@ const jsCssPlugin = { const plugins = htmlOnly ? [] : [jsCssPlugin]; +const minifier = new minimize({ plugins }); + const results = fs.readdirSync(inputDir).map((name) => { const src = fs.readFileSync(path.join(inputDir, name), "utf8"); + + const out = minifier.parse(src); + const len = Buffer.from(out, "utf8").length; + if (outputDir) { + fs.writeFileSync(path.join(outputDir, name), out); + } + const start = process.hrtime.bigint(); - let len; for (let i = 0; i < iterations; i++) { - len = new minimize({ plugins }).parse(src).length; + minifier.parse(src); } const elapsed = process.hrtime.bigint() - start; + return [name, len, iterations, Number(elapsed) / 1_000_000_000]; }); console.log(JSON.stringify(results)); diff --git a/debug/diff/.gitignore b/debug/diff/.gitignore new file mode 100644 index 0000000..dee5053 --- /dev/null +++ b/debug/diff/.gitignore @@ -0,0 +1 @@ +/outputs/ diff --git a/debug/diff/README.md b/debug/diff/README.md index 104cf4a..26a3cb1 100644 --- a/debug/diff/README.md +++ b/debug/diff/README.md @@ -1 +1 @@ -[compare.sh](./compare.sh) is a useful script for viewing a character-by-character diff between the minified outputs of minify-html and html-minifier for a specific test. Pass the test's file name as the first argument. +[compare](./compare) is a useful script for viewing a character-by-character diff between the minified outputs of minify-html and html-minifier for a specific input. Pass the input's file name as the first argument. diff --git a/debug/diff/compare b/debug/diff/compare new file mode 100755 index 0000000..36c85c0 --- /dev/null +++ b/debug/diff/compare @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +set -Eeuxo pipefail + +pushd "$(dirname "$0")" >/dev/null + +git --no-pager diff --no-index --word-diff=color --word-diff-regex=. "outputs/html-minifier/$1" "outputs/minify-html/$1" | less + +popd >/dev/null diff --git a/debug/diff/compare.sh b/debug/diff/compare.sh deleted file mode 100644 index fd963c0..0000000 --- a/debug/diff/compare.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash - -git --no-pager diff --no-index --word-diff=color --word-diff-regex=. "min/html-minifier/$1.html" "min/@minify-html/js/$1.html" | less diff --git a/debug/diff/run b/debug/diff/run new file mode 100755 index 0000000..cda35ba --- /dev/null +++ b/debug/diff/run @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +set -Eeuo pipefail + +shopt -s nullglob + +pushd "$(dirname "$0")" >/dev/null + +input_dir="$PWD/../../bench/inputs" +output_dir="$PWD/outputs" + +rm -rf "$output_dir" +mkdir -p "$output_dir" + +pushd "../../bench/runners" >/dev/null +for r in *; do + if [ ! -d "$r" ]; then + continue + fi + mkdir -p "$output_dir/$r" + echo "Running $r..." + pushd "$r" >/dev/null + MHB_ITERATIONS=1 MHB_INPUT_DIR="$input_dir" MHB_OUTPUT_DIR="$output_dir/$r" RUST_BACKTRACE=1 ./run >/dev/null + popd >/dev/null +done +popd >/dev/null + +echo "All done!" + +popd >/dev/null