@@ -17,7 +17,19 @@ if (!hasOpenSSL(3) ||
1717 process . exit ( 0 ) ;
1818}
1919
20- const isTest = process . argv . includes ( '--test' ) ;
20+ const operations = [
21+ 'get-macs-cold' ,
22+ 'get-macs-warm' ,
23+ 'create-cold' ,
24+ 'create-warm' ,
25+ 'hmac-lifecycle' ,
26+ 'mac-lifecycle' ,
27+ 'mac-stream-lifecycle' ,
28+ 'update' ,
29+ 'stream' ,
30+ 'final-buffer' ,
31+ 'final-hex' ,
32+ ] ;
2133const configurations = {
2234 'hmac-sha256' : {
2335 algorithm : 'HMAC' ,
@@ -32,24 +44,12 @@ const configurations = {
3244} ;
3345
3446const bench = common . createBenchmark ( main , {
35- operation : [
36- 'get-macs-cold' ,
37- 'get-macs-warm' ,
38- 'create-cold' ,
39- 'create-warm' ,
40- 'create-hmac' ,
41- 'update' ,
42- 'stream' ,
43- ] ,
47+ operation : operations ,
4448 algorithm : Object . keys ( configurations ) ,
4549 length : [ 0 , 64 , 4096 ] ,
4650 n : [ 1 , 10_000 , 20_000 , 500_000 ] ,
4751} , {
4852 combinationFilter ( { operation, algorithm, length, n } ) {
49- if ( isTest ) {
50- return algorithm === 'hmac-sha256' && length === 0 && n === 1 ;
51- }
52-
5353 if ( operation === 'get-macs-cold' ) {
5454 return algorithm === 'hmac-sha256' && length === 0 && n === 1 ;
5555 }
@@ -60,11 +60,25 @@ const bench = common.createBenchmark(main, {
6060 return length === 0 && n === 1 ;
6161 if ( operation === 'create-warm' )
6262 return length === 0 && n === 20_000 ;
63- if ( operation === 'create-hmac' && algorithm !== 'hmac-sha256' )
64- return false ;
65- return n === 10_000 ;
63+ if ( operation === 'hmac-lifecycle' ) {
64+ return algorithm === 'hmac-sha256' && n === 10_000 ;
65+ }
66+ if ( operation === 'mac-lifecycle' ||
67+ operation === 'mac-stream-lifecycle' ) {
68+ return n === 10_000 ;
69+ }
70+ if ( operation === 'update' || operation === 'stream' ) {
71+ return length === 64 && n === 500_000 ;
72+ }
73+ if ( operation === 'final-buffer' || operation === 'final-hex' ) {
74+ return algorithm === 'hmac-sha256' &&
75+ length === 64 &&
76+ n === 20_000 ;
77+ }
78+ return false ;
6679 } ,
6780 test : {
81+ operation : [ 'get-macs-cold' , 'create-cold' ] ,
6882 algorithm : [ 'hmac-sha256' ] ,
6983 length : [ 0 ] ,
7084 n : [ 1 ] ,
@@ -88,15 +102,27 @@ function main({ operation, algorithm, length, n }) {
88102 case 'create-warm' :
89103 measureCreate ( configuration , n , true ) ;
90104 break ;
91- case 'create-hmac' :
92- measureCreateHmac ( configuration , data , n ) ;
105+ case 'hmac-lifecycle' :
106+ measureHmacLifecycle ( configuration , data , n ) ;
107+ break ;
108+ case 'mac-lifecycle' :
109+ measureMacLifecycle ( configuration , data , n ) ;
110+ break ;
111+ case 'mac-stream-lifecycle' :
112+ measureMacStreamLifecycle ( configuration , data , n ) ;
93113 break ;
94114 case 'update' :
95115 measureUpdate ( configuration , data , n ) ;
96116 break ;
97117 case 'stream' :
98118 measureStream ( configuration , data , n ) ;
99119 break ;
120+ case 'final-buffer' :
121+ measureFinal ( configuration , data , n ) ;
122+ break ;
123+ case 'final-hex' :
124+ measureFinal ( configuration , data , n , 'hex' ) ;
125+ break ;
100126 default :
101127 throw new Error ( `unknown operation: ${ operation } ` ) ;
102128 }
@@ -128,7 +154,7 @@ function measureCreate({ algorithm, key, options }, n, warm) {
128154 assert . strictEqual ( typeof contexts [ n - 1 ] , 'object' ) ;
129155}
130156
131- function measureCreateHmac ( { key, options } , data , n ) {
157+ function measureHmacLifecycle ( { key, options } , data , n ) {
132158 createHmac ( options . digest , key ) . update ( data ) . digest ( ) ;
133159
134160 let result ;
@@ -140,7 +166,7 @@ function measureCreateHmac({ key, options }, data, n) {
140166 assert ( Buffer . isBuffer ( result ) ) ;
141167}
142168
143- function measureUpdate ( { algorithm, key, options } , data , n ) {
169+ function measureMacLifecycle ( { algorithm, key, options } , data , n ) {
144170 createMac ( algorithm , key , options ) . update ( data ) . final ( ) ;
145171
146172 let result ;
@@ -152,7 +178,7 @@ function measureUpdate({ algorithm, key, options }, data, n) {
152178 assert ( Buffer . isBuffer ( result ) ) ;
153179}
154180
155- function measureStream ( { algorithm, key, options } , data , n ) {
181+ function measureMacStreamLifecycle ( { algorithm, key, options } , data , n ) {
156182 const warmup = createMac ( algorithm , key , options ) ;
157183 warmup . end ( data ) ;
158184 warmup . read ( ) ;
@@ -168,3 +194,61 @@ function measureStream({ algorithm, key, options }, data, n) {
168194
169195 assert ( Buffer . isBuffer ( result ) ) ;
170196}
197+
198+ function measureUpdate ( { algorithm, key, options } , data , n ) {
199+ const warmup = createMac ( algorithm , key , options ) ;
200+ warmup . update ( data ) . final ( ) ;
201+
202+ const context = createMac ( algorithm , key , options ) ;
203+ bench . start ( ) ;
204+ for ( let i = 0 ; i < n ; ++ i )
205+ context . update ( data ) ;
206+ bench . end ( n ) ;
207+
208+ assert ( Buffer . isBuffer ( context . final ( ) ) ) ;
209+ }
210+
211+ function measureStream ( { algorithm, key, options } , data , n ) {
212+ const warmup = createMac ( algorithm , key , options ) ;
213+ warmup . end ( data ) ;
214+ warmup . read ( ) ;
215+
216+ const context = createMac ( algorithm , key , options ) ;
217+ bench . start ( ) ;
218+ for ( let i = 0 ; i < n ; ++ i )
219+ context . write ( data ) ;
220+ bench . end ( n ) ;
221+
222+ context . end ( ) ;
223+ assert ( Buffer . isBuffer ( context . read ( ) ) ) ;
224+ }
225+
226+ function measureFinal ( { algorithm, key, options } , data , n , encoding ) {
227+ const warmup = createMac ( algorithm , key , options ) . update ( data ) ;
228+ if ( encoding === undefined )
229+ warmup . final ( ) ;
230+ else
231+ warmup . final ( encoding ) ;
232+
233+ const contexts = new Array ( n ) ;
234+ for ( let i = 0 ; i < n ; ++ i )
235+ contexts [ i ] = createMac ( algorithm , key , options ) . update ( data ) ;
236+
237+ let result ;
238+ if ( encoding === undefined ) {
239+ bench . start ( ) ;
240+ for ( let i = 0 ; i < n ; ++ i )
241+ result = contexts [ i ] . final ( ) ;
242+ bench . end ( n ) ;
243+ } else {
244+ bench . start ( ) ;
245+ for ( let i = 0 ; i < n ; ++ i )
246+ result = contexts [ i ] . final ( encoding ) ;
247+ bench . end ( n ) ;
248+ }
249+
250+ if ( encoding === undefined )
251+ assert ( Buffer . isBuffer ( result ) ) ;
252+ else
253+ assert . strictEqual ( typeof result , 'string' ) ;
254+ }
0 commit comments