Skip to content

Commit 5ca3f4a

Browse files
authored
chore: add benchmarks (#161)
Reference: - #71 - #72 - #75 - #76 Started by #160
1 parent 05ce2c5 commit 5ca3f4a

6 files changed

Lines changed: 210 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@
3131
!/tap-snapshots/
3232
!/test/
3333
!/tsconfig.json
34+
!benchmarks/
3435
tap-testdir*/

benchmarks/match.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const Benchmark = require('benchmark')
2+
const ssri = require('..')
3+
const suite = new Benchmark.Suite()
4+
const fs = require('fs')
5+
const crypto = require('crypto')
6+
7+
const TEST_DATA = fs.readFileSync(__filename)
8+
const STATIC_DATA = 'static content'
9+
10+
function hash (data, algorithm) {
11+
return crypto.createHash(algorithm).update(data).digest('base64')
12+
}
13+
14+
const testSha = hash(TEST_DATA, 'sha512')
15+
const testIntegrity = `sha512-${testSha}`
16+
const testParsed = ssri.parse(testIntegrity, { single: true })
17+
18+
const staticSha = hash(STATIC_DATA, 'sha512')
19+
const staticIntegrity = `sha512-${staticSha}`
20+
const staticParsed = ssri.parse(staticIntegrity, { single: true })
21+
22+
suite
23+
.add('match to self', function () {
24+
testParsed.match(testParsed)
25+
})
26+
.add('match to other', function () {
27+
testParsed.match(staticParsed)
28+
})
29+
.on('cycle', function (event) {
30+
console.log(String(event.target))
31+
})
32+
.run({ async: false })

benchmarks/parse.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// From https://github.com/npm/ssri/pull/71
2+
const Benchmark = require('benchmark')
3+
const ssri = require('..')
4+
const suite = new Benchmark.Suite()
5+
const fs = require('fs')
6+
const crypto = require('crypto')
7+
8+
const TEST_DATA = fs.readFileSync(__filename)
9+
10+
function hash (data, algorithm) {
11+
return crypto.createHash(algorithm).update(data).digest('base64')
12+
}
13+
14+
const sha = hash(TEST_DATA, 'sha512')
15+
const integrity = `sha512-${sha}`
16+
const parsed = ssri.parse(integrity, { single: true })
17+
18+
suite
19+
.add('ssri.parse(base64, { single: true })', function () {
20+
ssri.parse(integrity, { single: true })
21+
})
22+
.add('ssri.parse(base64, { single: true, strict: true })', function () {
23+
ssri.parse(integrity, { single: true, strict: true })
24+
})
25+
.add('ssri.parse(parsed, { single: true })', function () {
26+
ssri.parse(parsed, { single: true })
27+
})
28+
.add('ssri.parse(parsed, { single: true, strict: true })', function () {
29+
ssri.parse(parsed, { single: true, strict: true })
30+
})
31+
.on('cycle', function (event) {
32+
console.log(String(event.target))
33+
})
34+
.run({ async: false })

benchmarks/stream.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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 })

benchmarks/to-string.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// From https://github.com/npm/ssri/pull/75
2+
const Benchmark = require('benchmark')
3+
const ssri = require('..')
4+
const suite = new Benchmark.Suite()
5+
6+
const integrity = `sha512-WrLorGiX4iEWOOOaJSiCrmDIamA47exH+Bz7tVwIPb4sCU8w4iNqGCqYuspMMeU5pgz/sU7koP5u8W3RCUojGw== sha256-Qhx213Vjr6GRSEawEL0WTzlb00whAuXpngy5zxc8HYc=`
7+
const parsed = ssri.parse(integrity)
8+
const parsedStrict = ssri.parse(integrity, { strict: true })
9+
10+
suite
11+
.add('parsed.toString()', function () {
12+
parsed.toString()
13+
})
14+
.add('parsedStrict.toString()', function () {
15+
parsedStrict.toString()
16+
})
17+
.on('cycle', function (event) {
18+
console.log(String(event.target))
19+
})
20+
.run({ async: false })

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"devDependencies": {
5454
"@npmcli/eslint-config": "^6.0.0",
5555
"@npmcli/template-oss": "4.28.1",
56+
"benchmark": "^2.1.4",
5657
"tap": "^16.0.1"
5758
},
5859
"engines": {
@@ -61,6 +62,9 @@
6162
"templateOSS": {
6263
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
6364
"version": "4.28.1",
64-
"publish": "true"
65+
"publish": "true",
66+
"allowPaths": [
67+
"benchmarks/"
68+
]
6569
}
6670
}

0 commit comments

Comments
 (0)