Debian Codegraph @

packageurl_bench_test.go

raw symbols SCIP
1 package packageurl
2
3 import (
4 "testing"
5 )
6
7 // Sample purls of varying complexity
8 var (
9 simplePurl = "pkg:npm/[email protected]"
10 namespacePurl = "pkg:maven/org.apache.commons/[email protected]"
11 qualifiersPurl = "pkg:npm/%40angular/[email protected]?repository_url=https://registry.npmjs.org"
12 complexPurl = "pkg:rpm/fedora/[email protected]?arch=i386&distro=fedora-25&repository_url=http://example.com"
13 subpathPurl = "pkg:github/package-url/purl-spec@244fd47e07d1004f0aed9c#src/main/java"
14 fullPurl = "pkg:deb/debian/[email protected]?arch=amd64&distro=stretch&repository_url=http://deb.debian.org#subpath/to/file"
15 )
16
17 // Pre-parsed PackageURL structs for ToString benchmarks
18 var (
19 simplePackageURL = PackageURL{
20 Type: "npm",
21 Name: "lodash",
22 Version: "4.17.21",
23 }
24 namespacePackageURL = PackageURL{
25 Type: "maven",
26 Namespace: "org.apache.commons",
27 Name: "commons-lang3",
28 Version: "3.12.0",
29 }
30 qualifiersPackageURL = PackageURL{
31 Type: "npm",
32 Namespace: "@angular",
33 Name: "core",
34 Version: "16.0.0",
35 Qualifiers: Qualifiers{
36 {Key: "repository_url", Value: "https://registry.npmjs.org"},
37 },
38 }
39 complexPackageURL = PackageURL{
40 Type: "rpm",
41 Namespace: "fedora",
42 Name: "curl",
43 Version: "7.50.3-1.fc25",
44 Qualifiers: Qualifiers{
45 {Key: "arch", Value: "i386"},
46 {Key: "distro", Value: "fedora-25"},
47 {Key: "repository_url", Value: "http://example.com"},
48 },
49 }
50 fullPackageURL = PackageURL{
51 Type: "deb",
52 Namespace: "debian",
53 Name: "dpkg",
54 Version: "1.19.0.4",
55 Qualifiers: Qualifiers{
56 {Key: "arch", Value: "amd64"},
57 {Key: "distro", Value: "stretch"},
58 {Key: "repository_url", Value: "http://deb.debian.org"},
59 },
60 Subpath: "subpath/to/file",
61 }
62 )
63
64 // FromString benchmarks - parsing
65 func BenchmarkFromString_Simple(b *testing.B) {
66 for i := 0; i < b.N; i++ {
67 _, _ = FromString(simplePurl)
68 }
69 }
70
71 func BenchmarkFromString_Namespace(b *testing.B) {
72 for i := 0; i < b.N; i++ {
73 _, _ = FromString(namespacePurl)
74 }
75 }
76
77 func BenchmarkFromString_Qualifiers(b *testing.B) {
78 for i := 0; i < b.N; i++ {
79 _, _ = FromString(qualifiersPurl)
80 }
81 }
82
83 func BenchmarkFromString_Complex(b *testing.B) {
84 for i := 0; i < b.N; i++ {
85 _, _ = FromString(complexPurl)
86 }
87 }
88
89 func BenchmarkFromString_Subpath(b *testing.B) {
90 for i := 0; i < b.N; i++ {
91 _, _ = FromString(subpathPurl)
92 }
93 }
94
95 func BenchmarkFromString_Full(b *testing.B) {
96 for i := 0; i < b.N; i++ {
97 _, _ = FromString(fullPurl)
98 }
99 }
100
101 // ToString benchmarks - serialization
102 func BenchmarkToString_Simple(b *testing.B) {
103 p := simplePackageURL
104 b.ResetTimer()
105 for i := 0; i < b.N; i++ {
106 _ = p.ToString()
107 }
108 }
109
110 func BenchmarkToString_Namespace(b *testing.B) {
111 p := namespacePackageURL
112 b.ResetTimer()
113 for i := 0; i < b.N; i++ {
114 _ = p.ToString()
115 }
116 }
117
118 func BenchmarkToString_Qualifiers(b *testing.B) {
119 p := qualifiersPackageURL
120 b.ResetTimer()
121 for i := 0; i < b.N; i++ {
122 _ = p.ToString()
123 }
124 }
125
126 func BenchmarkToString_Complex(b *testing.B) {
127 p := complexPackageURL
128 b.ResetTimer()
129 for i := 0; i < b.N; i++ {
130 _ = p.ToString()
131 }
132 }
133
134 func BenchmarkToString_Full(b *testing.B) {
135 p := fullPackageURL
136 b.ResetTimer()
137 for i := 0; i < b.N; i++ {
138 _ = p.ToString()
139 }
140 }
141
142 // Normalize benchmarks
143 func BenchmarkNormalize_Simple(b *testing.B) {
144 for i := 0; i < b.N; i++ {
145 p := simplePackageURL
146 _ = p.Normalize()
147 }
148 }
149
150 func BenchmarkNormalize_Complex(b *testing.B) {
151 for i := 0; i < b.N; i++ {
152 p := complexPackageURL
153 _ = p.Normalize()
154 }
155 }
156
157 func BenchmarkNormalize_Full(b *testing.B) {
158 for i := 0; i < b.N; i++ {
159 p := fullPackageURL
160 _ = p.Normalize()
161 }
162 }
163
164 // Roundtrip benchmarks (parse + serialize)
165 func BenchmarkRoundtrip_Simple(b *testing.B) {
166 for i := 0; i < b.N; i++ {
167 p, _ := FromString(simplePurl)
168 _ = p.ToString()
169 }
170 }
171
172 func BenchmarkRoundtrip_Complex(b *testing.B) {
173 for i := 0; i < b.N; i++ {
174 p, _ := FromString(complexPurl)
175 _ = p.ToString()
176 }
177 }
178
179 func BenchmarkRoundtrip_Full(b *testing.B) {
180 for i := 0; i < b.N; i++ {
181 p, _ := FromString(fullPurl)
182 _ = p.ToString()
183 }
184 }
185
186 // Qualifier operations
187 func BenchmarkQualifiersFromMap_Small(b *testing.B) {
188 m := map[string]string{
189 "arch": "amd64",
190 }
191 b.ResetTimer()
192 for i := 0; i < b.N; i++ {
193 _ = QualifiersFromMap(m)
194 }
195 }
196
197 func BenchmarkQualifiersFromMap_Medium(b *testing.B) {
198 m := map[string]string{
199 "arch": "amd64",
200 "distro": "debian-10",
201 "repository_url": "http://example.com",
202 }
203 b.ResetTimer()
204 for i := 0; i < b.N; i++ {
205 _ = QualifiersFromMap(m)
206 }
207 }
208
209 func BenchmarkQualifiersFromMap_Large(b *testing.B) {
210 m := map[string]string{
211 "arch": "amd64",
212 "distro": "debian-10",
213 "repository_url": "http://example.com",
214 "checksum": "sha256:abc123",
215 "vcs_url": "git+https://github.com/foo/bar",
216 "download_url": "https://example.com/download",
217 }
218 b.ResetTimer()
219 for i := 0; i < b.N; i++ {
220 _ = QualifiersFromMap(m)
221 }
222 }
223
224 func BenchmarkQualifiers_Map_Small(b *testing.B) {
225 q := Qualifiers{{Key: "arch", Value: "amd64"}}
226 b.ResetTimer()
227 for i := 0; i < b.N; i++ {
228 _ = q.Map()
229 }
230 }
231
232 func BenchmarkQualifiers_Map_Medium(b *testing.B) {
233 q := Qualifiers{
234 {Key: "arch", Value: "amd64"},
235 {Key: "distro", Value: "debian-10"},
236 {Key: "repository_url", Value: "http://example.com"},
237 }
238 b.ResetTimer()
239 for i := 0; i < b.N; i++ {
240 _ = q.Map()
241 }
242 }
243
244 func BenchmarkQualifiers_Map_Large(b *testing.B) {
245 q := Qualifiers{
246 {Key: "arch", Value: "amd64"},
247 {Key: "distro", Value: "debian-10"},
248 {Key: "repository_url", Value: "http://example.com"},
249 {Key: "checksum", Value: "sha256:abc123"},
250 {Key: "vcs_url", Value: "git+https://github.com/foo/bar"},
251 {Key: "download_url", Value: "https://example.com/download"},
252 }
253 b.ResetTimer()
254 for i := 0; i < b.N; i++ {
255 _ = q.Map()
256 }
257 }
258
259 // Qualifier String/Normalize
260 func BenchmarkQualifiers_String(b *testing.B) {
261 q := complexPackageURL.Qualifiers
262 b.ResetTimer()
263 for i := 0; i < b.N; i++ {
264 _ = q.String()
265 }
266 }
267
268 func BenchmarkQualifiers_Normalize(b *testing.B) {
269 for i := 0; i < b.N; i++ {
270 q := Qualifiers{
271 {Key: "Arch", Value: "amd64"},
272 {Key: "DISTRO", Value: "debian-10"},
273 {Key: "repository_url", Value: "http://example.com"},
274 }
275 _ = q.Normalize()
276 }
277 }
278
279 // Validation benchmarks
280 func BenchmarkValidQualifierKey(b *testing.B) {
281 keys := []string{"arch", "repository_url", "vcs_url", "checksum.sha256"}
282 b.ResetTimer()
283 for i := 0; i < b.N; i++ {
284 for _, k := range keys {
285 _ = validQualifierKey(k)
286 }
287 }
288 }
289
290 func BenchmarkValidType(b *testing.B) {
291 types := []string{"npm", "maven", "pypi", "golang", "deb", "rpm"}
292 b.ResetTimer()
293 for i := 0; i < b.N; i++ {
294 for _, t := range types {
295 _ = validType(t)
296 }
297 }
298 }