|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common.js'); |
| 4 | + |
| 5 | +const bench = common.createBenchmark(main, { |
| 6 | + op: [ |
| 7 | + 'normalizeAlgorithm-string', |
| 8 | + 'normalizeAlgorithm-dict', |
| 9 | + 'webidl-dict', |
| 10 | + 'webidl-algorithm-identifier-string', |
| 11 | + 'webidl-algorithm-identifier-object', |
| 12 | + 'webidl-dict-enforce-range', |
| 13 | + 'webidl-dict-ensure-sha', |
| 14 | + 'webidl-dict-null', |
| 15 | + ], |
| 16 | + n: [1e6], |
| 17 | +}, { flags: ['--expose-internals'] }); |
| 18 | + |
| 19 | +function main({ n, op }) { |
| 20 | + const { normalizeAlgorithm } = require('internal/crypto/util'); |
| 21 | + |
| 22 | + switch (op) { |
| 23 | + case 'normalizeAlgorithm-string': { |
| 24 | + // String shortcut + null dictionary (cheapest path). |
| 25 | + bench.start(); |
| 26 | + for (let i = 0; i < n; i++) |
| 27 | + normalizeAlgorithm('SHA-256', 'digest'); |
| 28 | + bench.end(n); |
| 29 | + break; |
| 30 | + } |
| 31 | + case 'normalizeAlgorithm-dict': { |
| 32 | + // Object input with a dictionary type and no BufferSource members. |
| 33 | + const alg = { name: 'ECDSA', hash: 'SHA-256' }; |
| 34 | + bench.start(); |
| 35 | + for (let i = 0; i < n; i++) |
| 36 | + normalizeAlgorithm(alg, 'sign'); |
| 37 | + bench.end(n); |
| 38 | + break; |
| 39 | + } |
| 40 | + case 'webidl-dict': { |
| 41 | + // WebIDL dictionary converter in isolation. |
| 42 | + const webidl = require('internal/crypto/webidl'); |
| 43 | + const input = { name: 'AES-GCM', iv: new Uint8Array(12) }; |
| 44 | + const opts = { prefix: 'test', context: 'test' }; |
| 45 | + bench.start(); |
| 46 | + for (let i = 0; i < n; i++) |
| 47 | + webidl.converters.AeadParams(input, opts); |
| 48 | + bench.end(n); |
| 49 | + break; |
| 50 | + } |
| 51 | + case 'webidl-algorithm-identifier-string': { |
| 52 | + // Exercises converters.AlgorithmIdentifier string path. |
| 53 | + const webidl = require('internal/crypto/webidl'); |
| 54 | + const opts = { prefix: 'test', context: 'test' }; |
| 55 | + bench.start(); |
| 56 | + for (let i = 0; i < n; i++) |
| 57 | + webidl.converters.AlgorithmIdentifier('SHA-256', opts); |
| 58 | + bench.end(n); |
| 59 | + break; |
| 60 | + } |
| 61 | + case 'webidl-algorithm-identifier-object': { |
| 62 | + // Exercises converters.AlgorithmIdentifier object path. |
| 63 | + const webidl = require('internal/crypto/webidl'); |
| 64 | + const input = { name: 'SHA-256' }; |
| 65 | + const opts = { prefix: 'test', context: 'test' }; |
| 66 | + bench.start(); |
| 67 | + for (let i = 0; i < n; i++) |
| 68 | + webidl.converters.AlgorithmIdentifier(input, opts); |
| 69 | + bench.end(n); |
| 70 | + break; |
| 71 | + } |
| 72 | + case 'webidl-dict-enforce-range': { |
| 73 | + // Exercises [EnforceRange] integer dictionary members. |
| 74 | + const webidl = require('internal/crypto/webidl'); |
| 75 | + const input = { |
| 76 | + name: 'RSASSA-PKCS1-v1_5', |
| 77 | + modulusLength: 2048, |
| 78 | + publicExponent: new Uint8Array([1, 0, 1]), |
| 79 | + }; |
| 80 | + const opts = { prefix: 'test', context: 'test' }; |
| 81 | + bench.start(); |
| 82 | + for (let i = 0; i < n; i++) |
| 83 | + webidl.converters.RsaKeyGenParams(input, opts); |
| 84 | + bench.end(n); |
| 85 | + break; |
| 86 | + } |
| 87 | + case 'webidl-dict-ensure-sha': { |
| 88 | + // Exercises ensureSHA on a hash member. |
| 89 | + const webidl = require('internal/crypto/webidl'); |
| 90 | + const input = { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' }; |
| 91 | + const opts = { prefix: 'test', context: 'test' }; |
| 92 | + bench.start(); |
| 93 | + for (let i = 0; i < n; i++) |
| 94 | + webidl.converters.RsaHashedImportParams(input, opts); |
| 95 | + bench.end(n); |
| 96 | + break; |
| 97 | + } |
| 98 | + case 'webidl-dict-null': { |
| 99 | + // Exercises the null/undefined path in createDictionaryConverter(). |
| 100 | + const webidl = require('internal/crypto/webidl'); |
| 101 | + const opts = { prefix: 'test', context: 'test' }; |
| 102 | + bench.start(); |
| 103 | + for (let i = 0; i < n; i++) |
| 104 | + webidl.converters.JsonWebKey(undefined, opts); |
| 105 | + bench.end(n); |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments