Skip to content

feat: Add perf benchmark tests (with breaking changes due to ESM updates) #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .benchrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Benchmark configuration
threshold: 3
maxMs: 60000
minRuns: 10
triggerGC: false
sort: true
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@ typings/
# next.js build output
.next

lib
lib

# Tests artifacts
benchmark_data/
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"description": "Typescript key management tool that works in the browser",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"packageManager": "yarn@1.22.22",
"type": "module",
"scripts": {
"test": "yarn run test:node && yarn run test:web",
"test:node": "mocha -r ts-node/register test/**/*.spec.ts ",
Expand All @@ -13,7 +15,8 @@
"check-types": "tsc --noEmit",
"prepublishOnly": "yarn run build",
"prebuild": "yarn run clean",
"build": "tsc -p tsconfig.build.json --outDir lib"
"build": "tsc -p tsconfig.build.json --outDir lib",
"benchmark": "NODE_OPTIONS='--max_old_space_size=4096 --loader=ts-node/esm' benchmark --config .benchrc.yaml 'test/perf/**/*.test.ts'"
},
"repository": {
"type": "git",
Expand All @@ -33,6 +36,7 @@
},
"homepage": "https://github.com/ChainSafe/bls-keygen#readme",
"devDependencies": {
"@chainsafe/benchmark": "^1.2.3",
"@types/bn.js": "^4.11.5",
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.5",
Expand All @@ -52,13 +56,13 @@
"karma-webpack": "^4.0.2",
"mocha": "^6.1.4",
"ts-loader": "^6.2.1",
"ts-node": "^8.3.0",
"ts-node": "^10.9.2",
"typescript": "^3.5.1",
"webpack": "^4.41.2"
},
"dependencies": {
"@chainsafe/bls-hd-key": "^0.3.0",
"@noble/hashes": "^1.0.0",
"@scure/bip39": "^1.0.0"
"@scure/bip39": "^1.2.1"
}
}
59 changes: 59 additions & 0 deletions test/perf/keygen.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, bench } from "@chainsafe/benchmark";
import {
generateRandomSecretKey,
deriveKeyFromMnemonic,
deriveKeyFromEntropy,
deriveKeyFromMaster,
deriveEth2ValidatorKeys,
} from "../../src";

// Sample mnemonic and entropy
const mnemonic = "impact exit example acquire drastic cement usage float mesh source private bulb twenty guitar neglect";
const path = "m/12381/3600/0/0";
const entropy = Uint8Array.from([
0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90,
0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x23,
0x45, 0x67, 0x89, 0x01, 0x23, 0x45, 0x67, 0x89,
0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x23, 0x45,
]);

const masterFromMnemonic = deriveKeyFromMnemonic(mnemonic);
const masterFromEntropy = deriveKeyFromEntropy(entropy);

describe("bls-keygen benchmarks", () => {
bench("generate random secret key", () => {
generateRandomSecretKey();
});

bench("derive master key from mnemonic", () => {
deriveKeyFromMnemonic(mnemonic);
});

bench("derive child key from mnemonic with path", () => {
deriveKeyFromMnemonic(mnemonic, path);
});

bench("derive master key from entropy", () => {
deriveKeyFromEntropy(entropy);
});

bench("derive child key from entropy with path", () => {
deriveKeyFromEntropy(entropy, path);
});

bench("derive child key from master key (mnemonic) with path", () => {
deriveKeyFromMaster(masterFromMnemonic, path);
});

bench("derive child key from master key (entropy) with path", () => {
deriveKeyFromMaster(masterFromEntropy, path);
});

bench("derive eth2 validator keys from mnemonic master key - index 0", () => {
deriveEth2ValidatorKeys(masterFromMnemonic, 0);
});

bench("derive eth2 validator keys from entropy master key - index 1", () => {
deriveEth2ValidatorKeys(masterFromEntropy, 1);
});
});
13 changes: 9 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
/* Basic Options */
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /*Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"target": "ESNext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "ESNext", /*Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"pretty": true,
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
Expand Down Expand Up @@ -35,7 +35,7 @@
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
Expand All @@ -56,5 +56,10 @@
},
"exclude": [
"node_modules"
]
],
"include": ["test", "src"],
"ts-node": {
"esm": true,
"experimentalSpecifierResolution": "node"
}
}
Loading