Skip to content

Commit

Permalink
Added benchmark script
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannicalo committed Aug 2, 2021
1 parent 2a9a906 commit 5209d85
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Expand Up @@ -22,6 +22,8 @@ jobs:
uses: coverallsapp/github-action@1.1.3
with:
github-token: ${{secrets.GITHUB_TOKEN}}
- name: Benchmark
run: npm run benchmark
strategy:
matrix:
os:
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -32,6 +32,7 @@
"url": "https://github.com/giovannicalo/node-webp.git"
},
"scripts": {
"benchmark": "node scripts/benchmark",
"build": "cmake-js compile",
"build-debug": "cmake-js compile --debug",
"install": "node scripts/install && npm run build",
Expand Down
35 changes: 35 additions & 0 deletions scripts/benchmark.js
@@ -0,0 +1,35 @@
const { readFileSync } = require("fs");
const { join } = require("path");

const { Format, decode, encode } = require("../source");

const iterations = 1000;

const path = join(__dirname, "..", "test");

const webp = readFileSync(join(path, "512x512.webp"));

const rgba = {
data: readFileSync(join(path, "512x512.rgba")),
format: Format.rgba,
height: 512,
width: 512
};

const benchmark = async (label, method, ...parameters) => {
const start = Date.now();
for (let i = 0; i < iterations; i++) {
await method(...parameters); // eslint-disable-line no-await-in-loop
}
console.log(`${label}: ${((Date.now() - start) / iterations).toFixed(2)}ms`);
};

(async () => {
for (const parameters of [
["WebP => RGBA", decode, webp, Format.rgba],
["WebP => YUV ", decode, webp, Format.yuv],
["RGBA => WebP", encode, rgba, 90]
]) {
await benchmark(...parameters); // eslint-disable-line no-await-in-loop
}
})();

0 comments on commit 5209d85

Please sign in to comment.