|
| 1 | +// From https://github.com/npm/ssri/pull/71 |
| 2 | +const Benchmark = require('benchmark') |
| 3 | +// const wtf = require("wtfnode"); |
| 4 | +// wtf.init(); |
| 5 | +const ssri = require('..') |
| 6 | +const suite = new Benchmark.Suite() |
| 7 | +const crypto = require('crypto') |
| 8 | +const { Readable } = require('stream') |
| 9 | + |
| 10 | +const largeText = 'a'.repeat(64).repeat(100) |
| 11 | +const largeTextSplitted = largeText.split('') |
| 12 | + |
| 13 | +const tinyText = 'a'.repeat(64) |
| 14 | +const tinyTextSplitted = tinyText.split('') |
| 15 | + |
| 16 | +const getStream = (text) => Readable.from(text) |
| 17 | + |
| 18 | +function hash (data, algorithm) { |
| 19 | + return crypto.createHash(algorithm).update(data).digest('base64') |
| 20 | +} |
| 21 | + |
| 22 | +const largeIntegrity = `sha512-${hash(largeText, 'sha512')}` |
| 23 | +const tinyIntegrity = `sha512-${hash(tinyText, 'sha512')}` |
| 24 | + |
| 25 | +suite |
| 26 | + .add('ssri.fromStream(stream, largeIntegrity)', { |
| 27 | + defer: true, |
| 28 | + fn: function (deferred) { |
| 29 | + const stream = getStream(largeTextSplitted) |
| 30 | + |
| 31 | + return ssri.fromStream(stream, largeIntegrity).then(() => deferred.resolve()) |
| 32 | + }, |
| 33 | + }) |
| 34 | + .add('ssri.fromStream(stream, tinyIntegrity)', { |
| 35 | + defer: true, |
| 36 | + fn: function (deferred) { |
| 37 | + const stream = getStream(tinyTextSplitted) |
| 38 | + |
| 39 | + return ssri.fromStream(stream, tinyIntegrity).then(() => deferred.resolve()) |
| 40 | + }, |
| 41 | + }) |
| 42 | + .add('ssri.checkStream(stream, largeIntegrity)', { |
| 43 | + defer: true, |
| 44 | + fn: function (deferred) { |
| 45 | + const stream = getStream(largeTextSplitted) |
| 46 | + |
| 47 | + return ssri.checkStream(stream, largeIntegrity).then(() => deferred.resolve()) |
| 48 | + }, |
| 49 | + }) |
| 50 | + .add('ssri.checkStream(stream, tinyIntegrity)', { |
| 51 | + defer: true, |
| 52 | + fn: function (deferred) { |
| 53 | + const stream = getStream(tinyTextSplitted) |
| 54 | + |
| 55 | + return ssri.checkStream(stream, tinyIntegrity).then(() => deferred.resolve()) |
| 56 | + }, |
| 57 | + }) |
| 58 | + .add('ssri.checkStream(stream, largeIntegrity, { single: true })', { |
| 59 | + defer: true, |
| 60 | + fn: function (deferred) { |
| 61 | + const stream = getStream(largeTextSplitted) |
| 62 | + |
| 63 | + return ssri.checkStream(stream, largeIntegrity, { |
| 64 | + single: true, |
| 65 | + }).then(() => deferred.resolve()) |
| 66 | + }, |
| 67 | + }) |
| 68 | + .add('ssri.checkStream(stream, tinyIntegrity, { single: true })', { |
| 69 | + defer: true, |
| 70 | + fn: function (deferred) { |
| 71 | + const stream = getStream(tinyTextSplitted) |
| 72 | + |
| 73 | + return ssri.checkStream(stream, tinyIntegrity, { |
| 74 | + single: true, |
| 75 | + }).then(() => deferred.resolve()) |
| 76 | + }, |
| 77 | + }) |
| 78 | + .add('ssri + createHash (largeIntegrity)', { |
| 79 | + defer: true, |
| 80 | + fn: function (deferred) { |
| 81 | + const stream = getStream(largeTextSplitted) |
| 82 | + const parsed = ssri.parse(largeIntegrity, { single: true }) |
| 83 | + const h = crypto.createHash(parsed.algorithm) |
| 84 | + |
| 85 | + stream.pipe(h) |
| 86 | + stream.on('end', () => { |
| 87 | + const digest = h.digest('base64') |
| 88 | + |
| 89 | + if (parsed.digest !== digest) { |
| 90 | + throw new Error('Integrity check failed') |
| 91 | + } |
| 92 | + deferred.resolve() |
| 93 | + }) |
| 94 | + }, |
| 95 | + }) |
| 96 | + .add('ssri + createHash (tinyIntegrity)', { |
| 97 | + defer: true, |
| 98 | + fn: function (deferred) { |
| 99 | + const stream = getStream(tinyTextSplitted) |
| 100 | + const parsed = ssri.parse(tinyIntegrity, { single: true }) |
| 101 | + const h = crypto.createHash(parsed.algorithm) |
| 102 | + |
| 103 | + stream.pipe(h) |
| 104 | + stream.on('end', () => { |
| 105 | + const digest = h.digest('base64') |
| 106 | + |
| 107 | + if (parsed.digest !== digest) { |
| 108 | + throw new Error('Integrity check failed') |
| 109 | + } |
| 110 | + deferred.resolve() |
| 111 | + }) |
| 112 | + }, |
| 113 | + }) |
| 114 | + .on('cycle', function (event) { |
| 115 | + console.log(String(event.target)) |
| 116 | + // wtf.dump(); |
| 117 | + }) |
| 118 | + .run({ async: false }) |
0 commit comments