Skip to content
This repository has been archived by the owner on Mar 18, 2022. It is now read-only.

Commit

Permalink
Add benchmark tests
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Aug 10, 2021
1 parent 7135e1e commit 4f474d2
Show file tree
Hide file tree
Showing 5 changed files with 918 additions and 283 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"test": "yarn run test:as-ci && yarn run test:unit",
"test:unit": "yarn run test:unit:node && yarn run test:unit:browser",
"test:unit:node": "mocha -r .babel-register test/*.spec.js",
"test:perf": "mocha -r .babel-register test/perf.test.js",
"benchmark:local": "node ./node_modules/.bin/benchmark 'test/benchmark.test.js' --local",
"test:unit:browser": "karma start --single-run --browsers ChromeHeadless,FirefoxHeadless karma.config.js",
"test:ci": "yarn test:as-ci",
"test:as": "asp --nortrace --verbose",
Expand Down Expand Up @@ -59,7 +59,8 @@
"karma-firefox-launcher": "^1.2.0",
"karma-mocha": "^1.3.0",
"karma-webpack": "^4.0.2",
"mocha": "^6.2.2",
"@dapplion/benchmark": "^0.1.6",
"mocha": "^8.3.0",
"webpack": "^4.39.3",
"webpack-cli": "^3.3.7"
}
Expand Down
57 changes: 57 additions & 0 deletions test/benchmark.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const {itBench, setBenchOpts} = require("@dapplion/benchmark");
const {byteArrToObj} = require("./utils");
const sha256 = require("../lib/index");

// As of Aug 04 2021
// hash
// ✓ digestObjects 50023 times 18.37785 ops/s 54.41333 ms/op - 1103 runs 60.0 s
// ✓ digest64 50023 times 17.54145 ops/s 57.00782 ms/op - 1053 runs 60.0 s
describe("hash", () => {
setBenchOpts({
maxMs: 100 * 1000,
minMs: 60 * 1000,
runs: 512,
});

const input = Buffer.from("gajindergajindergajindergajindergajindergajindergajindergajinder", "utf8");
const input1 = "gajindergajindergajindergajinder";
const input2 = "gajindergajindergajindergajinder";
const buffer1 = Buffer.from(input1, "utf-8");
const buffer2 = Buffer.from(input2, "utf-8");
const obj1 = byteArrToObj(buffer1);
const obj2 = byteArrToObj(buffer2);
// total number of time running hash for 200000 balances
const iterations = 50023;
itBench(`digestObjects ${iterations} times`, () => {
for (let j = 0; j < iterations; j++) sha256.default.digestObjects(obj1, obj2);
});

itBench(`digest64 ${iterations} times`, () => {
for (let j = 0; j < iterations; j++) sha256.default.digest64(input);
});

});

/**
* time java: 2968 336927.2237196765 hashes/sec
* time apache: 1025 975609.7560975610 hashes/sec
*
* As of Aug 04 2021
* digest 1000000 times 0.8279731 ops/s 1.207769 s/op - 82 runs 100 s
* => we are at 8279731 hashes/sec
*/
describe("hash - compare to java", () => {
// java statistic for same test: https://gist.github.com/scoroberts/a60d61a2cc3afba1e8813b338ecd1501
setBenchOpts({
maxMs: 100 * 1000,
minMs: 60 * 1000,
runs: 512,
});

const iterations = 1000000;
const input = Buffer.from("lwkjt23uy45pojsdf;lnwo45y23po5i;lknwe;lknasdflnqw3uo5", "utf8");

itBench(`digest ${iterations} times`, () => {
for (let i=0; i<iterations; i++) sha256.default.digest(input);
})
});
49 changes: 0 additions & 49 deletions test/perf.test.js

This file was deleted.

9 changes: 6 additions & 3 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* This function contains multiple same procedures but we intentionally
* do it step by step to improve performance a bit.
**/
export function objToByteArr(obj, byteArr, offset) {
function objToByteArr(obj, byteArr, offset) {
let tmp;
for (let index = 0; index < 8; index++) {
switch (index) {
Expand Down Expand Up @@ -45,7 +45,7 @@
* This function contains multiple same procedures but we intentionally
* do it step by step to improve performance a bit.
**/
export function byteArrToObj(byteArr) {
function byteArrToObj(byteArr) {
let h0, h1, h2, h3, h4, h5, h6, h7;

for (let index = 0; index < 8; index++) {
Expand Down Expand Up @@ -92,4 +92,7 @@ export function byteArrToObj(byteArr) {
h6,
h7,
};;
}
}

exports.objToByteArr = objToByteArr;
exports.byteArrToObj = byteArrToObj;
Loading

0 comments on commit 4f474d2

Please sign in to comment.