-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbench_test.go
More file actions
303 lines (282 loc) · 8.75 KB
/
Copy pathbench_test.go
File metadata and controls
303 lines (282 loc) · 8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package sbom
import (
"encoding/json"
"fmt"
"io"
"testing"
)
const benchmarkPackageCount = 840
var (
benchmarkDocument *SBOM
benchmarkError error
)
var benchFixtures = []struct {
name string
path string
}{
{"CycloneDX/minimal", "cyclonedx/minimal.cdx.json"},
{"CycloneDX/alpine", "cyclonedx/alpine.cdx.json"},
{"CycloneDX/laravel", "cyclonedx/laravel.cdx.json"},
{"CycloneDX/nginx", "cyclonedx/nginx.cdx.json"},
{"CycloneDX/juice-shop", "cyclonedx/juice-shop.cdx.json"},
{"SPDX/minimal", "spdx/minimal.spdx.json"},
{"SPDX/alpine", "spdx/alpine.spdx.json"},
{"SPDX/nginx", "spdx/nginx.spdx.json"},
}
func BenchmarkParse(b *testing.B) {
for _, f := range benchFixtures {
benchmarkParse(b, f.name, readFixture(b, f.path))
}
}
func BenchmarkParseLarge(b *testing.B) {
benchmarkParse(b, "CycloneDX/840-packages", readFixture(b, "cyclonedx/juice-shop.cdx.json"))
benchmarkParse(b, "SPDX/large", readFixture(b, "spdx/nginx.spdx.json"))
}
func BenchmarkParseCycloneDXComponents(b *testing.B) {
benchmarkParse(b, "flat", benchmarkCycloneDX(b, benchmarkPackageCount, false, 0, 0))
benchmarkParse(b, "nested", benchmarkCycloneDX(b, benchmarkPackageCount, true, 0, 0))
}
func BenchmarkParseDenseDependencyGraph(b *testing.B) {
benchmarkParse(b, "CycloneDX/256-by-32", benchmarkCycloneDX(b, 256, false, 32, 0))
}
func BenchmarkParseSPDXEnvelopes(b *testing.B) {
data := readFixture(b, "spdx/nginx.spdx.json")
github := append(append([]byte(`{"sbom":`), data...), '}')
intoto := append([]byte(`{"predicateType":"https://spdx.dev/Document","predicate":`), data...)
intoto = append(intoto, '}')
benchmarkParse(b, "GitHub", github)
benchmarkParse(b, "in-toto", intoto)
}
func BenchmarkParseDuplicatePackageIdentities(b *testing.B) {
benchmarkParse(b, "CycloneDX", benchmarkCycloneDX(b, benchmarkPackageCount, false, 0, 2))
benchmarkParse(b, "SPDX", benchmarkSPDX(b, benchmarkPackageCount, 2))
}
func BenchmarkEncodeLarge(b *testing.B) {
benchmarks := []struct {
name string
path string
format Format
}{
{"CycloneDX/JSON", "cyclonedx/juice-shop.cdx.json", FormatCycloneDXJSON},
{"CycloneDX/XML", "cyclonedx/juice-shop.cdx.json", FormatCycloneDXXML},
{"SPDX/JSON", "spdx/nginx.spdx.json", FormatSPDXJSON},
}
for _, benchmark := range benchmarks {
doc := mustParseBenchmark(b, readFixture(b, benchmark.path))
b.Run(benchmark.name, func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
b.ReportMetric(float64(len(doc.Packages)), "pkgs")
for range b.N {
benchmarkError = Encode(io.Discard, doc, benchmark.format)
}
if benchmarkError != nil {
b.Fatal(benchmarkError)
}
})
}
}
func BenchmarkParseThenEncode(b *testing.B) {
benchmarks := []struct {
name string
path string
format Format
}{
{"CycloneDX/JSON", "cyclonedx/juice-shop.cdx.json", FormatCycloneDXJSON},
{"SPDX/JSON", "spdx/nginx.spdx.json", FormatSPDXJSON},
}
for _, benchmark := range benchmarks {
data := readFixture(b, benchmark.path)
b.Run(benchmark.name, func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(int64(len(data)))
b.ResetTimer()
for range b.N {
benchmarkDocument, benchmarkError = Parse(data)
if benchmarkError == nil {
benchmarkError = Encode(io.Discard, benchmarkDocument, benchmark.format)
}
}
if benchmarkError != nil {
b.Fatal(benchmarkError)
}
})
}
}
func BenchmarkDetect(b *testing.B) {
for _, f := range benchFixtures {
data := readFixture(b, f.path)
b.Run(f.name, func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for range b.N {
_ = Detect(data)
}
})
}
}
func BenchmarkPackagePURL(b *testing.B) {
doc := mustParseBenchmark(b, readFixture(b, "cyclonedx/juice-shop.cdx.json"))
b.Run(fmt.Sprintf("%d-pkgs", len(doc.Packages)), func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for range b.N {
for j := range doc.Packages {
_ = doc.Packages[j].PURL()
}
}
})
}
func benchmarkParse(b *testing.B, name string, data []byte) {
b.Helper()
doc := mustParseBenchmark(b, data)
b.Run(name, func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(int64(len(data)))
b.ResetTimer()
b.ReportMetric(float64(len(doc.Packages)), "pkgs")
b.ReportMetric(float64(len(doc.Relationships)), "rels")
for range b.N {
benchmarkDocument, benchmarkError = Parse(data)
}
if benchmarkError != nil {
b.Fatal(benchmarkError)
}
})
}
func mustParseBenchmark(b testing.TB, data []byte) *SBOM {
b.Helper()
doc, err := Parse(data)
if err != nil {
b.Fatal(err)
}
return doc
}
func benchmarkCycloneDX(b testing.TB, packageCount int, nested bool, dependencyWidth, identities int) []byte {
b.Helper()
bom := cdxBOM{
BOMFormat: "CycloneDX",
SpecVersion: "1.6",
BOMVersion: 1,
Metadata: &cdxMetadata{
Timestamp: "2026-01-01T00:00:00Z",
Component: &cdxComponent{Type: "application", Name: "benchmark", Version: "1.0.0"},
},
}
component := func(i int) cdxComponent {
identity := i
if identities > 0 {
identity %= packageCount / identities
}
return cdxComponent{
BOMRef: fmt.Sprintf("component-%d", i),
Type: "library",
Name: fmt.Sprintf("package-%d", identity),
Version: fmt.Sprintf("1.%d.0", identity%20),
Description: "A package with representative CycloneDX metadata",
Copyright: "Copyright 2026 Example",
Author: "Example Maintainer",
PURL: fmt.Sprintf("pkg:generic/package-%d@1.%d.0", identity, identity%20),
Supplier: &cdxOrgEntity{Name: "Example Org"},
Hashes: []cdxHash{
{Alg: "SHA-256", Content: "c314ca2bdaf4317fb92300e22b3ff9a4494a6e8d4d3c80baab6ad441bf52c390"},
{Alg: "SHA-512", Content: "950319f7d7b8339c29ee29ff9ac69f1ea1453ffc967928c3d36df20477d8b817"},
},
Licenses: []cdxLicense{{License: &cdxLicenseID{ID: "MIT"}}},
ExternalReferences: []cdxExtRef{
{Type: "website", URL: "https://example.com/package"},
{Type: "vcs", URL: "https://example.com/package.git"},
},
Properties: []cdxProperty{
{Name: "benchmark:source", Value: "registry"},
{Name: "benchmark:scope", Value: "runtime"},
},
}
}
if nested {
const roots = 20
bom.Components = make([]cdxComponent, roots)
for root := range roots {
index := root * (packageCount / roots)
bom.Components[root] = component(index)
current := &bom.Components[root]
for offset := 1; offset < packageCount/roots; offset++ {
current.Components = []cdxComponent{component(index + offset)}
current = ¤t.Components[0]
}
}
} else {
bom.Components = make([]cdxComponent, packageCount)
for i := range packageCount {
bom.Components[i] = component(i)
}
}
if dependencyWidth > 0 {
bom.Dependencies = make([]cdxDependency, packageCount)
for i := range packageCount {
dependency := cdxDependency{
Ref: fmt.Sprintf("component-%d", i),
DependsOn: make([]string, dependencyWidth),
}
for j := range dependencyWidth {
dependency.DependsOn[j] = fmt.Sprintf("component-%d", (i+j+1)%packageCount)
}
bom.Dependencies[i] = dependency
}
}
return marshalBenchmarkJSON(b, &bom)
}
func benchmarkSPDX(b testing.TB, packageCount, identities int) []byte {
b.Helper()
doc := spdxDoc{
SPDXVersion: "SPDX-2.3",
SPDXID: spdxDocID,
Name: "benchmark",
DataLicense: "CC0-1.0",
DocumentNamespace: "https://example.com/benchmark",
CreationInfo: &spdxCreationInfo{
Created: "2026-01-01T00:00:00Z",
Creators: []string{"Tool: benchmark", "Organization: Example Org"},
},
Packages: make([]spdxPackage, packageCount),
}
for i := range packageCount {
identity := i
if identities > 0 {
identity %= packageCount / identities
}
doc.Packages[i] = spdxPackage{
SPDXID: fmt.Sprintf("SPDXRef-Package-%d", i),
Name: fmt.Sprintf("package-%d", identity),
VersionInfo: fmt.Sprintf("1.%d.0", identity%20),
DownloadLocation: "https://example.com/package.tar.gz",
Homepage: "https://example.com/package",
PackageFileName: "package.tar.gz",
LicenseConcluded: "MIT",
LicenseDeclared: "MIT",
CopyrightText: "Copyright 2026 Example",
Description: "A package with representative SPDX metadata",
Supplier: "Organization: Example Org",
Originator: "Person: Example Maintainer",
PrimaryPackagePurpose: "LIBRARY",
Checksums: []spdxChecksum{{
Algorithm: "SHA256",
Value: "c314ca2bdaf4317fb92300e22b3ff9a4494a6e8d4d3c80baab6ad441bf52c390",
}},
ExternalRefs: []spdxExtRef{{
Category: "PACKAGE-MANAGER",
Type: "purl",
Locator: fmt.Sprintf("pkg:generic/package-%d@1.%d.0", identity, identity%20),
}},
}
}
return marshalBenchmarkJSON(b, &doc)
}
func marshalBenchmarkJSON(b testing.TB, value any) []byte {
b.Helper()
data, err := json.Marshal(value)
if err != nil {
b.Fatal(err)
}
return data
}