From 49408299d22ba594b24f20a8c752920ad4721ca0 Mon Sep 17 00:00:00 2001 From: Wilson Lin Date: Mon, 20 Jan 2020 11:12:51 +1100 Subject: [PATCH] Fix and refactor bench scripts --- bench/bench.js | 31 ------------------------------- bench/build.sh | 10 +--------- bench/minifiers.js | 2 +- bench/run.js | 23 ++++++++++++++++++++++- java/build.sh | 2 +- nodejs/build.sh | 43 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 68 insertions(+), 43 deletions(-) create mode 100644 nodejs/build.sh diff --git a/bench/bench.js b/bench/bench.js index 9aec8dc..17f2204 100644 --- a/bench/bench.js +++ b/bench/bench.js @@ -40,37 +40,6 @@ const fromEntries = entries => { return obj; }; -const sizes = {}; -const setSize = (program, test, result) => { - if (!sizes[test]) { - sizes[test] = { - original: { - absolute: tests.find(t => t.name === test).contentAsString.length, - relative: 1, - }, - }; - } - const original = sizes[test].original.absolute; - sizes[test][program] = { - absolute: result, - relative: result / original, - }; -}; - -// Run once to set sizes. -for (const t of tests) { - for (const m of Object.keys(minifiers)) { - try { - setSize(m, t.name, minifiers[m](t.contentAsString, t.contentAsBuffer).length); - } catch (err) { - console.error(`Failed to run ${m} on test ${t.name}:`); - console.error(err); - process.exit(1); - } - } -} -results.writeSizeResults(sizes); - const runTest = test => new Promise((resolve, reject) => { // Run JS libraries. const suite = new benchmark.Suite(); diff --git a/bench/build.sh b/bench/build.sh index ad8df7b..eb22460 100755 --- a/bench/build.sh +++ b/bench/build.sh @@ -4,16 +4,8 @@ set -e pushd "$(dirname "$0")" -nodejs_cargo_toml="../nodejs/native/Cargo.toml" +bash ../nodejs/build.sh -if [ -f "$nodejs_cargo_toml.orig" ]; then - echo 'Not altering Node.js Cargo.toml file' -else - cp "$nodejs_cargo_toml" "$nodejs_cargo_toml.orig" -fi -sed -i 's%^hyperbuild = .*$%hyperbuild = { path = "../.." }%' "$nodejs_cargo_toml" -HYPERBUILD_NODEJS_SKIP_BIN_DOWNLOAD=1 npm rebuild hyperbuild -mv "$nodejs_cargo_toml.orig" "$nodejs_cargo_toml" pushd hyperbuild-bench cargo build --release popd diff --git a/bench/minifiers.js b/bench/minifiers.js index 9bd1904..832a9ee 100644 --- a/bench/minifiers.js +++ b/bench/minifiers.js @@ -3,7 +3,7 @@ const hyperbuild = require("hyperbuild"); const minimize = require("minimize"); module.exports = { - 'hyperbuild-nodejs': (_, buffer) => hyperbuild.minify_in_place(Buffer.from(buffer)), + 'hyperbuild-nodejs': (_, buffer) => hyperbuild.minifyInPlace(Buffer.from(buffer)), 'html-minifier': content => htmlMinifier.minify(content, { collapseBooleanAttributes: true, collapseInlineTagWhitespace: true, diff --git a/bench/run.js b/bench/run.js index c8d3635..e8f040c 100644 --- a/bench/run.js +++ b/bench/run.js @@ -2,14 +2,34 @@ const fs = require('fs'); const mkdirp = require('mkdirp'); const path = require('path'); const minifiers = require('./minifiers'); +const results = require('./results'); const tests = require('./tests'); +const sizes = {}; +const setSize = (program, test, result) => { + if (!sizes[test]) { + sizes[test] = { + original: { + absolute: tests.find(t => t.name === test).contentAsBuffer.length, + relative: 1, + }, + }; + } + const original = sizes[test].original.absolute; + sizes[test][program] = { + absolute: result, + relative: result / original, + }; +}; + for (const t of tests) { for (const m of Object.keys(minifiers)) { try { + const min = minifiers[m](t.contentAsString, t.contentAsBuffer); + setSize(m, t.name, min.length); const minPath = path.join(__dirname, 'min', m, `${t.name}.html`); mkdirp.sync(path.dirname(minPath)); - fs.writeFileSync(minPath, minifiers[m](t.contentAsString, t.contentAsBuffer)); + fs.writeFileSync(minPath, min); } catch (err) { console.error(`Failed to run ${m} on test ${t.name}:`); console.error(err); @@ -17,3 +37,4 @@ for (const t of tests) { } } } +results.writeSizeResults(sizes); diff --git a/java/build.sh b/java/build.sh index 68a73d6..3e1130a 100755 --- a/java/build.sh +++ b/java/build.sh @@ -39,8 +39,8 @@ if [ -f Cargo.toml.orig ]; then echo 'Not altering Java Cargo.toml file' else cp Cargo.toml Cargo.toml.orig + sed -i 's%^hyperbuild = .*$%hyperbuild = { path = ".." }%' Cargo.toml fi -sed -i 's%^hyperbuild = .*$%hyperbuild = { path = ".." }%' Cargo.toml cargo build $rust_build_arg mv Cargo.toml.orig Cargo.toml cp target/rust/$rust_build_dir/libhyperbuild_java.$ext src/main/resources/$os_name-x86_64.nativelib diff --git a/nodejs/build.sh b/nodejs/build.sh new file mode 100644 index 0000000..1a55caa --- /dev/null +++ b/nodejs/build.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +set -e + +# Builds hyperbuild-nodejs with native module for local testing. +# Built package will only run current platform and Node.js version. + +pushd "$(dirname "$0")" + +npm install + +rm -rf dist +npx tsc + +if [ -f native/Cargo.toml.orig ]; then + echo 'Not altering Node.js Cargo.toml file' +else + cp native/Cargo.toml native/Cargo.toml.orig + sed -i 's%^hyperbuild = .*$%hyperbuild = { path = "../.." }%' native/Cargo.toml +fi +npx neon build --release +mv native/Cargo.toml.orig native/Cargo.toml + +if [[ "$OSTYPE" == "linux-gnu" ]]; then + os_name="linux" +elif [[ "$OSTYPE" == "darwin"* ]]; then + os_name="macos" +elif [[ "$OSTYPE" == "cygwin" ]]; then + os_name="windows" +elif [[ "$OSTYPE" == "msys" ]]; then + os_name="windows" +elif [[ "$OSTYPE" == "win32" ]]; then + os_name="windows" +else + echo "Unknown OS" + exit 1 +fi + +node_version=$(node -e 'console.log(process.versions.node.split(".")[0])') + +mv native/index.node "dist/$os_name-x86_64-node$node_version.node" + +popd