| 13 | }); |
| 14 | |
| 15 | function main({ api, type, len, algo, n }) { |
| 16 | if (api === 'stream' && /^v0\.[0-8]\./.test(process.version)) { |
| 17 | console.error('Crypto streams not available until v0.10'); |
| 18 | // Use the legacy, just so that we can compare them. |
| 19 | api = 'legacy'; |
| 20 | } |
| 21 | |
| 22 | let message; |
| 23 | let encoding; |
| 24 | switch (type) { |
| 25 | case 'asc': |
| 26 | message = 'a'.repeat(len); |
| 27 | encoding = 'ascii'; |
| 28 | break; |
| 29 | case 'utf': |
| 30 | message = 'ü'.repeat(len / 2); |
| 31 | encoding = 'utf8'; |
| 32 | break; |
| 33 | case 'buf': |
| 34 | message = Buffer.alloc(len, 'b'); |
| 35 | break; |
| 36 | default: |
| 37 | throw new Error(`unknown message type: ${type}`); |
| 38 | } |
| 39 | |
| 40 | const fn = api === 'stream' ? streamWrite : legacyWrite; |
| 41 | |
| 42 | bench.start(); |
| 43 | fn(algo, message, encoding, n, len); |
| 44 | } |
| 45 | |
| 46 | function legacyWrite(algo, message, encoding, n, len) { |
| 47 | const written = n * len; |