-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
benchmark: refactor crypto oneshot #48267
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common.js'); | ||
| const crypto = require('crypto'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const fixtures_keydir = path.resolve(__dirname, '../../test/fixtures/keys/'); | ||
|
|
||
| const keyFixtures = { | ||
| ec: fs.readFileSync(`${fixtures_keydir}/ec_p256_private.pem`).toString(), | ||
| rsa: fs.readFileSync(`${fixtures_keydir}/rsa_private_2048.pem`).toString(), | ||
| ed25519: fs.readFileSync(`${fixtures_keydir}/ed25519_private.pem`).toString(), | ||
| }; | ||
|
|
||
| const data = crypto.randomBytes(256); | ||
|
|
||
| let pems; | ||
| let keyObjects; | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| keyType: ['rsa', 'ec', 'ed25519'], | ||
| mode: ['sync', 'async', 'async-parallel'], | ||
| keyFormat: ['pem', 'der', 'jwk', 'keyObject', 'keyObject.unique'], | ||
| n: [1e3], | ||
| }); | ||
|
|
||
| function measureSync(n, digest, privateKey, keys) { | ||
| bench.start(); | ||
| for (let i = 0; i < n; ++i) { | ||
| crypto.sign( | ||
| digest, | ||
| data, | ||
| privateKey || keys[i]); | ||
| } | ||
| bench.end(n); | ||
| } | ||
|
|
||
| function measureAsync(n, digest, privateKey, keys) { | ||
| let remaining = n; | ||
| function done() { | ||
| if (--remaining === 0) | ||
| bench.end(n); | ||
| else | ||
| one(); | ||
| } | ||
|
|
||
| function one() { | ||
| crypto.sign( | ||
| digest, | ||
| data, | ||
| privateKey || keys[n - remaining], | ||
| done); | ||
| } | ||
| bench.start(); | ||
| one(); | ||
| } | ||
|
|
||
| function measureAsyncParallel(n, digest, privateKey, keys) { | ||
| let remaining = n; | ||
| function done() { | ||
| if (--remaining === 0) | ||
| bench.end(n); | ||
| } | ||
| bench.start(); | ||
| for (let i = 0; i < n; ++i) { | ||
| crypto.sign( | ||
| digest, | ||
| data, | ||
| privateKey || keys[i], | ||
| done); | ||
| } | ||
| } | ||
|
|
||
| function main({ n, mode, keyFormat, keyType }) { | ||
| // "keyObject.unique" allow to compare the result with "keyObject" to | ||
| // assess whether mutexes over the key material impact the operation | ||
| if (keyFormat === 'keyObject.unique' && mode !== 'async-parallel') { | ||
panva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| pems ||= [...Buffer.alloc(n)].map(() => keyFixtures[keyType]); | ||
| keyObjects ||= pems.map(crypto.createPrivateKey); | ||
|
|
||
| let privateKey, keys, digest; | ||
|
|
||
| switch (keyType) { | ||
| case 'rsa': | ||
| case 'ec': | ||
| digest = 'sha256'; | ||
| break; | ||
| case 'ed25519': | ||
| break; | ||
| default: | ||
| throw new Error('not implemented'); | ||
| } | ||
|
|
||
| switch (keyFormat) { | ||
| case 'keyObject': | ||
| privateKey = keyObjects[0]; | ||
| break; | ||
| case 'pem': | ||
| privateKey = pems[0]; | ||
| break; | ||
| case 'jwk': { | ||
| privateKey = { key: keyObjects[0].export({ format: 'jwk' }), format: 'jwk' }; | ||
| break; | ||
| } | ||
| case 'der': { | ||
| privateKey = { key: keyObjects[0].export({ format: 'der', type: 'pkcs8' }), format: 'der', type: 'pkcs8' }; | ||
| break; | ||
| } | ||
| case 'keyObject.unique': | ||
| keys = keyObjects; | ||
| break; | ||
| default: | ||
| throw new Error('not implemented'); | ||
| } | ||
|
|
||
| switch (mode) { | ||
| case 'sync': | ||
| measureSync(n, digest, privateKey, keys); | ||
| break; | ||
| case 'async': | ||
| measureAsync(n, digest, privateKey, keys); | ||
| break; | ||
| case 'async-parallel': | ||
| measureAsyncParallel(n, digest, privateKey, keys); | ||
| break; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.