Repository navigation
Expand file tree
/
Copy pathnormalize_benchmark_test.go
More file actions
166 lines (151 loc) · 3.57 KB
/
Copy pathnormalize_benchmark_test.go
File metadata and controls
166 lines (151 loc) · 3.57 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
package spdx
import (
"encoding/json"
"os"
"sort"
"testing"
)
var (
benchmarkNormalized string
benchmarkExpression Expression
benchmarkErr error
benchmarkValid bool
)
func BenchmarkNormalizeCanonicalIdentifiers(b *testing.B) {
benchmarkNormalizeCorpus(b, []string{
"MIT",
"Apache-2.0",
"BSD-3-Clause",
"ISC",
"MPL-2.0",
"GPL-3.0-only",
"LGPL-2.1-or-later",
"CC0-1.0",
"Unlicense",
"Zlib",
})
}
func BenchmarkNormalizeCommonInformalNames(b *testing.B) {
benchmarkNormalizeCorpus(b, []string{
"MIT License",
"Apache 2.0",
"GPL v3",
"GNU General Public License v2",
"BSD 3-Clause",
"Mozilla Public License 2.0",
"Eclipse Public License 2.0",
"Public Domain",
"CC BY 4.0",
"Boost",
})
}
func BenchmarkNormalizeInvalidValues(b *testing.B) {
benchmarkNormalizeCorpus(b, []string{
"",
"UNKNOWN-LICENSE",
"FAKEYLICENSE",
"NOT-A-LICENSE",
"commercial proprietary terms",
"license information unavailable",
})
}
func BenchmarkNormalizeExpressions(b *testing.B) {
expressions := []string{
"MIT",
"MIT OR Apache-2.0",
"GPL-2.0-only WITH Classpath-exception-2.0",
"MIT OR GPL-2.0-only AND Apache-2.0",
"(Apache 2 OR MIT License) AND GPL v3",
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, expression := range expressions {
benchmarkNormalized, benchmarkErr = NormalizeExpression(expression)
}
}
}
func BenchmarkNormalizeRepeatedValues(b *testing.B) {
inputs := make([]string, 1000)
for i := range inputs {
inputs[i] = "Apache License 2.0"
}
benchmarkNormalizeCorpus(b, inputs)
}
func BenchmarkNormalizeMostlyUniqueRealWorld(b *testing.B) {
data, err := os.ReadFile("real_licenses.json")
if err != nil {
b.Skip("real_licenses.json not found")
}
licenses := make(map[string]int)
if err := json.Unmarshal(data, &licenses); err != nil {
b.Fatalf("parse real-world licenses: %v", err)
}
inputs := make([]string, 0, len(licenses))
for license := range licenses {
inputs = append(inputs, license)
}
sort.Strings(inputs)
benchmarkNormalizeCorpus(b, inputs)
}
func BenchmarkNormalizePaths(b *testing.B) {
benchmarks := []struct {
name string
input string
}{
{name: "ExactMatch", input: "Apache-2.0"},
{name: "Transform", input: "Apache 2.0"},
{name: "Transposition", input: "MIT License"},
{name: "Fallback", input: "licensed under the BSD terms"},
}
for _, benchmark := range benchmarks {
b.Run(benchmark.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
benchmarkNormalized, benchmarkErr = Normalize(benchmark.input)
}
})
}
}
func BenchmarkParseCanonicalExpressions(b *testing.B) {
expressions := []string{
"MIT",
"MIT OR Apache-2.0",
"GPL-2.0-only WITH Classpath-exception-2.0",
"MIT OR (GPL-2.0-only AND Apache-2.0)",
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, expression := range expressions {
benchmarkExpression, benchmarkErr = Parse(expression)
}
}
}
func BenchmarkValidateExpressions(b *testing.B) {
expressions := []string{
"MIT",
"MIT OR Apache-2.0",
"GPL-2.0-only WITH Classpath-exception-2.0",
"MIT OR (GPL-2.0-only AND Apache-2.0)",
"invalid-license",
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, expression := range expressions {
benchmarkValid = Valid(expression)
}
}
}
func benchmarkNormalizeCorpus(b *testing.B, inputs []string) {
b.Helper()
b.ReportAllocs()
b.ResetTimer()
b.ReportMetric(float64(len(inputs)), "inputs/op")
for i := 0; i < b.N; i++ {
for _, input := range inputs {
benchmarkNormalized, benchmarkErr = Normalize(input)
}
}
}