From 88055e37559bc32723fb75be20534d3ade818aeb Mon Sep 17 00:00:00 2001 From: Rogelio Guzman Date: Thu, 18 Oct 2018 09:19:11 -0700 Subject: [PATCH] Change to an async implementation and display feedback --- package.json | 4 ++- src/index.js | 78 ++++++++++++++++++++++++++++++++++++++-------------- yarn.lock | 47 +++++++++++++++++++++++++++++-- 3 files changed, 105 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 82dce87..5080a5b 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,8 @@ }, "homepage": "https://github.com/rogeliog/jest-benchmark", "dependencies": { - "execa": "^1.0.0" + "ansi-escapes": "^3.1.0", + "execa": "^1.0.0", + "ora": "^3.0.0" } } diff --git a/src/index.js b/src/index.js index cf95e1c..c442c6e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,20 +1,44 @@ -const execa = require('execa'); +import execa from 'execa'; +import ansiEscapes from 'ansi-escapes'; +import ora from 'ora'; const NUM_RUNS = process.env.NUM_RUNS || 10; const JEST_BIN = process.env.JEST_BIN || 'node_modules/.bin/jest'; const args = process.argv.slice(2); -const times = (n, fn) => new Array(n).fill('').map(fn); -const runTests = () => { +const printOnCurrentLine = text => { + process.stdout.write(ansiEscapes.eraseLine); + process.stdout.write(ansiEscapes.cursorLeft); + process.stdout.write(text); +}; + +const runJest = async () => + execa.stdout(JEST_BIN, [...args, '--useStderr', '--json']); + +const runTests = async ({ + times, + results = [], + onTestRunCompleted = () => {}, +}) => { + if (times <= 0) { + return results; + } try { - const { stdout } = execa.sync(JEST_BIN, [...args, '--useStderr', '--json']); + const stdout = await runJest(); const end = +new Date(); const { startTime } = JSON.parse(stdout); - return (end - startTime) / 1000; + const duration = (end - startTime) / 1000; + + onTestRunCompleted({ times }); + + return runTests({ + times: times - 1, + results: results.concat([duration]), + onTestRunCompleted, + }); } catch (e) { console.log(e); - console.log(` jest-benchmarks is still really bad at reporing errors - Make sure that all the tests that you want to are passing @@ -24,23 +48,35 @@ jest-benchmarks is still really bad at reporing errors } }; -console.log('Warming up cache...'); +const executeBenchmark = async () => { + const cacheSpinner = ora('Warming up cache').start(); + // warm up cache + await runTests({ times: 2 }); + cacheSpinner.succeed(); -// warm up cache -times(2, runTests); + const testRunsSpinner = ora(`Test runs completed: 0/${NUM_RUNS}`).start(); + const results = await runTests({ + times: NUM_RUNS, + onTestRunCompleted: ({ times }) => { + testRunsSpinner.text = `Test runs completed: ${NUM_RUNS - + times + + 1}/${NUM_RUNS}`; + }, + }); -console.log('Running tests...'); + testRunsSpinner.succeed(); -const results = times(NUM_RUNS, runTests); + const totalTime = results.reduce((sum, r) => sum + r, 0); + const average = (totalTime / NUM_RUNS).toFixed(3); + const max = results.reduce((max, r) => (r > max ? r : max), 0); + const min = results.reduce((min, r) => (r < min ? r : min), Infinity); -const totalTime = results.reduce((sum, r) => sum + r, 0); -const average = (totalTime / NUM_RUNS).toFixed(3); -const max = results.reduce((max, r) => (r > max ? r : max), 0); -const min = results.reduce((min, r) => (r < min ? r : min), Infinity); + console.log(); + console.log(`Command: jest ${args.join(' ')}`); + console.log(`Number of runs: ${NUM_RUNS}`); + console.log(`Average: ${average}s`); + console.log(`Max: ${min}s`); + console.log(`Min: ${max}s`); +}; -console.log(); -console.log(`Command: jest ${args.join(' ')}`); -console.log(`Number of runs: ${NUM_RUNS}`); -console.log(`Average: ${average}s`); -console.log(`Max: ${min}s`); -console.log(`Min: ${max}s`); +executeBenchmark(); diff --git a/yarn.lock b/yarn.lock index 2605807..a01cd0c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -679,7 +679,7 @@ amdefine@>=0.0.4: resolved "https://registry.yarnpkg.com/amdefine/-/amdefine-1.0.1.tgz#4a5282ac164729e93619bcfd3ad151f817ce91f5" integrity sha1-SlKCrBZHKek2Gbz9OtFR+BfOkfU= -ansi-escapes@^3.0.0: +ansi-escapes@^3.0.0, ansi-escapes@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-3.1.0.tgz#f73207bb81207d75fd6c83f125af26eea378ca30" integrity sha512-UgAb8H9D41AQnu/PbWlCofQVcnV4Gs2bBJi9eZPxfU/hgglFh3SMDMENRIqdr7H6XFnXdoknctFByVsCOotTVw== @@ -1203,7 +1203,7 @@ chalk@^1.1.3: strip-ansi "^3.0.0" supports-color "^2.0.0" -chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0, chalk@^2.4.1: +chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0, chalk@^2.3.1, chalk@^2.4.1: version "2.4.1" resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.1.tgz#18c49ab16a037b6eb0152cc83e3471338215b66e" integrity sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ== @@ -1269,6 +1269,11 @@ cli-cursor@^2.1.0: dependencies: restore-cursor "^2.0.0" +cli-spinners@^1.1.0: + version "1.3.1" + resolved "https://registry.yarnpkg.com/cli-spinners/-/cli-spinners-1.3.1.tgz#002c1990912d0d59580c93bd36c056de99e4259a" + integrity sha512-1QL4544moEsDVH9T/l6Cemov/37iv1RtoKf7NJ04A60+4MREXNfx/QvavbH6QoGdsD4N4Mwy49cmaINR/o2mdg== + cli-width@^2.0.0: version "2.2.0" resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.0.tgz#ff19ede8a9a5e579324147b0c11f0fbcbabed639" @@ -1292,6 +1297,11 @@ cliui@^4.0.0: strip-ansi "^4.0.0" wrap-ansi "^2.0.0" +clone@^1.0.2: + version "1.0.4" + resolved "https://registry.yarnpkg.com/clone/-/clone-1.0.4.tgz#da309cc263df15994c688ca902179ca3c7cd7c7e" + integrity sha1-2jCcwmPfFZlMaIypAheco8fNfH4= + co@^4.6.0: version "4.6.0" resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184" @@ -1465,6 +1475,13 @@ default-require-extensions@^1.0.0: dependencies: strip-bom "^2.0.0" +defaults@^1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/defaults/-/defaults-1.0.3.tgz#c656051e9817d9ff08ed881477f3fe4019f3ef7d" + integrity sha1-xlYFHpgX2f8I7YgUd/P+QBnz730= + dependencies: + clone "^1.0.2" + define-properties@^1.1.2: version "1.1.3" resolved "https://registry.yarnpkg.com/define-properties/-/define-properties-1.1.3.tgz#cf88da6cbee26fe6db7094f61d870cbd84cee9f1" @@ -3335,6 +3352,13 @@ lodash@^4.13.1, lodash@^4.14.0, lodash@^4.17.10, lodash@^4.17.4, lodash@^4.17.5: resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d" integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg== +log-symbols@^2.2.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-2.2.0.tgz#5740e1c5d6f0dfda4ad9323b5332107ef6b4c40a" + integrity sha512-VeIAFslyIerEJLXHziedo2basKbMKtTw3vfn5IzG0XTjhAVEJyNHnL2p7vc+wBDSdQuUpNw3M2u6xb9QsAY5Eg== + dependencies: + chalk "^2.0.1" + longest@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097" @@ -3762,6 +3786,18 @@ optionator@^0.8.1, optionator@^0.8.2: type-check "~0.3.2" wordwrap "~1.0.0" +ora@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/ora/-/ora-3.0.0.tgz#8179e3525b9aafd99242d63cc206fd64732741d0" + integrity sha512-LBS97LFe2RV6GJmXBi6OKcETKyklHNMV0xw7BtsVn2MlsgsydyZetSCbCANr+PFLmDyv4KV88nn0eCKza665Mg== + dependencies: + chalk "^2.3.1" + cli-cursor "^2.1.0" + cli-spinners "^1.1.0" + log-symbols "^2.2.0" + strip-ansi "^4.0.0" + wcwidth "^1.0.1" + os-homedir@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/os-homedir/-/os-homedir-1.0.2.tgz#ffbc4988336e0e833de0c168c7ef152121aa7fb3" @@ -4949,6 +4985,13 @@ watch@~0.10.0: resolved "https://registry.yarnpkg.com/watch/-/watch-0.10.0.tgz#77798b2da0f9910d595f1ace5b0c2258521f21dc" integrity sha1-d3mLLaD5kQ1ZXxrOWwwiWFIfIdw= +wcwidth@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/wcwidth/-/wcwidth-1.0.1.tgz#f0b0dcf915bc5ff1528afadb2c0e17b532da2fe8" + integrity sha1-8LDc+RW8X/FSivrbLA4XtTLaL+g= + dependencies: + defaults "^1.0.3" + webidl-conversions@^4.0.2: version "4.0.2" resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad"