Skip to content

Commit

Permalink
Add perf/benchmark for browser
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuk committed Jun 23, 2022
1 parent 8d07f3c commit 6f2474d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 23 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
16 changes: 16 additions & 0 deletions test/perf/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JSZip Performance</title>
</head>
<body>
<script src="../../node_modules/benchmark/node_modules/lodash/lodash.js"></script>
<script src="../../node_modules/benchmark/benchmark.js"></script>
<script src="../../dist/jszip.js"></script>
<script src="./perf.js"></script>
<script>
benchmark("arraybuffer");
</script>
</body>
</html>
55 changes: 34 additions & 21 deletions test/perf/perf.js
Original file line number Diff line number Diff line change
@@ -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 });
});
};
}));
35 changes: 34 additions & 1 deletion test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]}`);
}

0 comments on commit 6f2474d

Please sign in to comment.