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

Commit

Permalink
Migrate existing benchmark tests to new framework
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Aug 10, 2021
1 parent f20f3a9 commit ebef2cf
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 40 deletions.
25 changes: 0 additions & 25 deletions test/bench.js

This file was deleted.

68 changes: 53 additions & 15 deletions test/benchmark.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const {itBench, setBenchOpts} = require("@dapplion/benchmark");
const sha256 = require("../lib/index");

// As of Aug 05 2021
// hash
// ✓ digestTwoHashObjects 50023 times 17.64902 ops/s 56.66037 ms/op - 1059 runs 60.0 s
// ✓ digest64 50023 times 16.99958 ops/s 58.82497 ms/op - 1020 runs 60.0 s
// ✓ hashObjectToByteArray 50023 times 646.1493 ops/s 1.547630 ms/op - 38649 runs 60.0 s
// ✓ byteArrayToHashObject 50023 times 554.0949 ops/s 1.804745 ms/op - 33179 runs 60.0 s

describe("hash", () => {
// Aug 10 2021
// digestTwoHashObjects vs digest64 vs digest
// ✓ digestTwoHashObjects 50023 times 19.17366 ops/s 52.15488 ms/op - 1151 runs 60.0 s
// ✓ digest64 50023 times 18.21352 ops/s 54.90425 ms/op - 1093 runs 60.0 s
// ✓ digest 50023 times 10.60461 ops/s 94.29865 ms/op - 637 runs 60.1 s
describe("digestTwoHashObjects vs digest64 vs digest", () => {
setBenchOpts({
maxMs: 100 * 1000,
minMs: 60 * 1000,
Expand All @@ -32,26 +30,38 @@ describe("hash", () => {
for (let j = 0; j < iterations; j++) sha256.default.digest64(input);
});

itBench(`hashObjectToByteArray ${iterations} times`, () => {
const byteArr = new Uint8Array(32);
for (let j = 0; j < iterations; j++) sha256.hashObjectToByteArray(obj1, byteArr, 0);
itBench(`digest ${iterations} times`, () => {
for (let j = 0; j < iterations; j++) sha256.default.digest(input);
});

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

describe("digest different Buffers", () => {
const randomBuffer = (length) => Buffer.from(Array.from({length}, () => Math.round(Math.random() * 255)));

setBenchOpts({
maxMs: 10 * 1000,
minMs: 5 * 1000,
runs: 512,
});

for (const length of [32, 64, 128, 256, 512, 1024]) {
const buffer = randomBuffer(length);
itBench(`input length ${length}`, () => {
sha256.default.digest(buffer);
});
}
});

/**
* time java: 2968 336927.2237196765 hashes/sec
* time apache: 1025 975609.7560975610 hashes/sec
*
* As of Aug 04 2021
* 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", () => {
describe("hash - compare to java", () => {
// java statistic for same test: https://gist.github.com/scoroberts/a60d61a2cc3afba1e8813b338ecd1501
setBenchOpts({
maxMs: 100 * 1000,
Expand All @@ -66,3 +76,31 @@ describe("hash - compare to java", () => {
for (let i=0; i<iterations; i++) sha256.default.digest(input);
})
});

// Aug 10 2021
// utils
// ✓ hashObjectToByteArray 50023 times 685.6641 ops/s 1.458440 ms/op - 41081 runs 60.0 s
// ✓ byteArrayToHashObject 50023 times 580.6237 ops/s 1.722286 ms/op - 34771 runs 60.0 s
describe("utils", () => {
setBenchOpts({
maxMs: 100 * 1000,
minMs: 60 * 1000,
runs: 512,
});

const input1 = "gajindergajindergajindergajinder";
const buffer1 = Buffer.from(input1, "utf-8");
const obj1 = sha256.byteArrayToHashObject(buffer1);

// total number of time running hash for 200000 balances
const iterations = 50023;

itBench(`hashObjectToByteArray ${iterations} times`, () => {
const byteArr = new Uint8Array(32);
for (let j = 0; j < iterations; j++) sha256.hashObjectToByteArray(obj1, byteArr, 0);
});

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

0 comments on commit ebef2cf

Please sign in to comment.