Debian Codegraph @

spdxexp/benchmark_validate_licenses_test.go

raw symbols SCIP
1 package spdxexp
2
3 import (
4 "fmt"
5 "testing"
6 )
7
8 type validateLicensesBenchmarkScenario struct {
9 name string
10 testLicenses []string
11 }
12
13 var validateLicensesBenchmarkScenarios = []validateLicensesBenchmarkScenario{
14 // Scenario order is used as-is in the summary table.
15 {"MIT--exact", []string{"MIT"}},
16 {"mit--caseinsensitive", []string{"mit"}},
17 {"mit--extra-space", []string{" MIT "}},
18 {"Apache-2.0--active-early", []string{"Apache-2.0"}},
19 {"Zed--active-end", []string{"Zed"}},
20 {"MIT AND Apache-2.0--complex", []string{"MIT", "Apache-2.0"}},
21 {"MIT AND Apache-2.0 OR Zed--complex", []string{"MIT", "Apache-2.0", "Zed"}},
22 {"BSD-2-Clause-FreeBSD--deprecated", []string{"BSD-2-Clause-FreeBSD"}},
23 {"GPL-2.0-or-later--range", []string{"GPL-2.0-or-later"}},
24 {"Apache-1.0+--plus-range", []string{"Apache-1.0+"}},
25 {"LicenseRef-scancode-adobe-postscript", []string{"LicenseRef-scancode-adobe-postscript"}},
26 {"DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2", []string{"DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2"}},
27 }
28
29 func BenchmarkValidateLicenses(b *testing.B) {
30 for _, scenario := range validateLicensesBenchmarkScenarios {
31 scenario := scenario
32 b.Run(scenario.name, func(b *testing.B) {
33 benchmarkValidateLicensesScenario(b, scenario.testLicenses)
34 })
35 }
36 }
37
38 func benchmarkValidateLicensesScenario(b *testing.B, licenses []string) {
39 b.ReportAllocs()
40 b.ResetTimer()
41
42 for i := 0; i < b.N; i++ {
43 valid, invalidLicenses := ValidateLicenses(licenses)
44 if !valid || len(invalidLicenses) != 0 {
45 b.Fatalf("ValidateLicenses(%v) returned valid=%v invalid=%v", licenses, valid, invalidLicenses)
46 }
47 }
48 }
49
50 func computeValidateLicensesBenchmarkTableRows(repeats int) []benchmarkTableRow {
51 rows := make([]benchmarkTableRow, 0, len(validateLicensesBenchmarkScenarios))
52
53 for _, scenario := range validateLicensesBenchmarkScenarios {
54 scenario := scenario
55 avg := runBenchmarkNsAvg(repeats, func(b *testing.B) {
56 b.ReportAllocs()
57 b.ResetTimer()
58 for i := 0; i < b.N; i++ {
59 valid, invalidLicenses := ValidateLicenses(scenario.testLicenses)
60 if !valid || len(invalidLicenses) != 0 {
61 panic(fmt.Sprintf("ValidateLicenses scenario %q failed: valid=%v invalid=%v", scenario.name, valid, invalidLicenses))
62 }
63 }
64 })
65 rows = append(rows, benchmarkTableRow{label: scenario.name, nsOpAvg: avg})
66 }
67
68 return rows
69 }