Skip to content

Commit

Permalink
A set of benchmarks for @solana/keys (#2356)
Browse files Browse the repository at this point in the history
Node LTS (20)
```
┌─────────┬───────────────────┬──────────┬───────────────────┬──────────┬─────────┐
│ (index) │ Task Name         │ ops/sec  │ Average Time (ns) │ Margin   │ Samples │
├─────────┼───────────────────┼──────────┼───────────────────┼──────────┼─────────┤
│ 0       │ 'generateKeyPair' │ '10,138' │ 98630.77218934878 │ '±2.25%' │ 5070    │
│ 1       │ 'signBytes'       │ '14,355' │ 69658.12245750854 │ '±2.16%' │ 7178    │
│ 2       │ 'verifySignature' │ '6,735'  │ 148476.5546318311 │ '±1.25%' │ 3368    │
└─────────┴───────────────────┴──────────┴───────────────────┴──────────┴─────────┘
```

Node 21
```
┌─────────┬───────────────────┬──────────┬────────────────────┬──────────┬─────────┐
│ (index) │ Task Name         │ ops/sec  │ Average Time (ns)  │ Margin   │ Samples │
├─────────┼───────────────────┼──────────┼────────────────────┼──────────┼─────────┤
│ 0       │ 'generateKeyPair' │ '11,864' │ 84284.08410584877  │ '±2.39%' │ 5933    │
│ 1       │ 'signBytes'       │ '15,353' │ 65131.59020450661  │ '±1.34%' │ 7677    │
│ 2       │ 'verifySignature' │ '6,917'  │ 144570.39404451908 │ '±0.51%' │ 3459    │
└─────────┴───────────────────┴──────────┴────────────────────┴──────────┴─────────┘
```
  • Loading branch information
steveluscher authored Mar 21, 2024
1 parent 125fc15 commit af8265f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/keys/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"web3"
],
"scripts": {
"benchmark": "./src/__benchmarks__/run.ts",
"compile:js": "tsup --config build-scripts/tsup.config.package.ts",
"compile:typedefs": "tsc -p ./tsconfig.declarations.json && node ../../node_modules/@solana/build-scripts/add-js-extension-to-types.mjs",
"dev": "jest -c ../../node_modules/@solana/test-config/jest-dev.config.ts --rootDir . --watch",
Expand Down Expand Up @@ -78,5 +79,8 @@
"path": "./dist/index*.js"
}
]
},
"devDependencies": {
"tinybench": "^2.6.0"
}
}
53 changes: 53 additions & 0 deletions packages/keys/src/__benchmarks__/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env -S pnpx tsx --

import { Bench } from 'tinybench';

import { generateKeyPair, SignatureBytes, signBytes, verifySignature } from '../index';

Object.assign(globalThis, {
__BROWSER__: false,
__DEV__: false,
__NODEJS__: true,
__REACTNATIVE____: false,
});

const bench = new Bench({
throws: true,
});

let keyPair: CryptoKeyPair;
async function generateKeyPairForTest() {
keyPair = await generateKeyPair();
}

let randomBytes: Uint8Array;
function generateRandomBytesForTest() {
randomBytes ||= new Uint8Array(32);
crypto.getRandomValues(randomBytes);
}

let signature: SignatureBytes;
async function generateSignatureForBytes() {
signature = await signBytes(keyPair.privateKey, randomBytes);
}

bench
.add('generateKeyPair', generateKeyPair)
.add('signBytes', async () => await signBytes(keyPair.privateKey, randomBytes), {
async beforeEach() {
generateRandomBytesForTest();
await generateKeyPairForTest();
},
})
.add('verifySignature', async () => await verifySignature(keyPair.publicKey, signature, randomBytes), {
async beforeEach() {
generateRandomBytesForTest();
await generateKeyPairForTest();
await generateSignatureForBytes();
},
});

(async () => {
await bench.run();
console.table(bench.table());
})();
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit af8265f

Please sign in to comment.