diff --git a/package.json b/package.json index 531b190d..f10e84d3 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,10 @@ "scripts": { "test": "npm run test-node && npm run test-browser && tsc", "test-node": "qunit --require ./test/helpers/test-utils.js --require ./test/helpers/node-test-utils.js test/asserts/", - "test-browser": "grunt build && node test/run.js", + "test-browser": "grunt build && node test/run.js --test", + "perf": "npm run perf-node && npm run perf-browser", "perf-node": "node test/perf/node.js", + "perf-browser": "node test/run.js --benchmark", "lint": "eslint ." }, "contributors": [ diff --git a/test/perf/index.html b/test/perf/index.html new file mode 100644 index 00000000..7dbcb750 --- /dev/null +++ b/test/perf/index.html @@ -0,0 +1,16 @@ + + + + + JSZip Performance + + + + + + + + + \ No newline at end of file diff --git a/test/perf/perf.js b/test/perf/perf.js index ee3355a7..30f00637 100644 --- a/test/perf/perf.js +++ b/test/perf/perf.js @@ -1,26 +1,39 @@ "use strict"; -module.exports = function (type) { - const suite = new Benchmark.Suite(); +(function (root, factory) { + if (typeof module === "object" && module.exports) { + module.exports = factory(); + } else { + root.benchmark = factory(); + } +}(typeof self !== "undefined" ? self : this, function () { + return function (type) { + return new Promise(resolve => { + const suite = new Benchmark.Suite(); - suite - .add("generateAsync", { - defer: true, - async fn(deferred) { - const zip = new JSZip(); + suite + .add(`${type} generateAsync`, { + defer: true, + async fn(deferred) { + const zip = new JSZip(); - for (let i = 0; i < 50; i++) { - zip.file("file_" + i, "R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=", { base64: true, date: new Date(1234123491011) }); - } + for (let i = 0; i < 50; i++) { + zip.file("file_" + i, "R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=", { base64: true, date: new Date(1234123491011) }); + } - await zip.generateAsync({ type }); - deferred.resolve(); - } - }) - .on("cycle", event => { - // Output benchmark result by converting benchmark result to string - console.log(String(event.target)); - }) - .on("complete", () => console.log("Benchmark complete")) - .run({ "async": true }); -}; + await zip.generateAsync({ type }); + deferred.resolve(); + } + }) + .on("cycle", event => { + // Output benchmark result by converting benchmark result to string + console.log(String(event.target)); + }) + .on("complete", () => { + console.log("Benchmark complete"); + resolve(); + }) + .run({ "async": true }); + }); + }; +})); diff --git a/test/run.js b/test/run.js index 1ea63ff4..7300e970 100644 --- a/test/run.js +++ b/test/run.js @@ -80,4 +80,37 @@ async function runTests() { } } -runTests(); +async function waitForBenchmark(page) { + return new Promise(resolve => { + const logs = []; + + page.on("console", async message => { + if (message.text() === "Benchmark complete") { + resolve(logs); + } else { + logs.push(message.text()); + } + }); + }); +} + +async function runBenchmark() { + const results = await runBrowsers(waitForBenchmark, "perf/index.html"); + + for (const [browser, logs] of results) { + for (const log of logs) { + console.log(browser, log); + } + } +} + +switch (process.argv[2]) { +case "--test": + runTests(); + break; +case "--benchmark": + runBenchmark(); + break; +default: + throw new Error(`Unknown argument: ${process.argv[2]}`); +}